|
| 1 | +package com.faforever.api.data; |
| 2 | + |
| 3 | +import com.faforever.api.AbstractIntegrationTest; |
| 4 | +import com.faforever.api.data.domain.GroupPermission; |
| 5 | +import com.faforever.api.security.OAuthScope; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.springframework.http.HttpHeaders; |
| 8 | +import org.springframework.test.context.jdbc.Sql; |
| 9 | +import org.springframework.test.context.jdbc.Sql.ExecutionPhase; |
| 10 | + |
| 11 | +import static com.faforever.api.data.JsonApiMediaType.JSON_API_MEDIA_TYPE; |
| 12 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 13 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; |
| 14 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 15 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 16 | + |
| 17 | +@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/truncateTables.sql") |
| 18 | +@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepDefaultData.sql") |
| 19 | +public class UserGroupTest extends AbstractIntegrationTest { |
| 20 | + private static final String testPatch = """ |
| 21 | + { |
| 22 | + "data": { |
| 23 | + "type": "userGroup", |
| 24 | + "id": "3", |
| 25 | + "attributes": { |
| 26 | + "public": false |
| 27 | + }, |
| 28 | + "relationships": { |
| 29 | + "members": { |
| 30 | + "data": [{ |
| 31 | + "type": "player", |
| 32 | + "id": "1" |
| 33 | + }, { |
| 34 | + "type": "player", |
| 35 | + "id": "2" |
| 36 | + } |
| 37 | + ] |
| 38 | + }, |
| 39 | + "permissions": { |
| 40 | + "data": [{ |
| 41 | + "type": "groupPermission", |
| 42 | + "id": "17" |
| 43 | + }, { |
| 44 | + "type": "groupPermission", |
| 45 | + "id": "19" |
| 46 | + } |
| 47 | + ] |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + """; |
| 53 | + |
| 54 | + private static final String testPost = """ |
| 55 | + { |
| 56 | + "data": { |
| 57 | + "type": "userGroup", |
| 58 | + "attributes": { |
| 59 | + "technicalName": "faf_test", |
| 60 | + "nameKey": "faf.test", |
| 61 | + "public": false |
| 62 | + }, |
| 63 | + "relationships": { |
| 64 | + "members": { |
| 65 | + "data": [] |
| 66 | + }, |
| 67 | + "permissions": { |
| 68 | + "data": [{ |
| 69 | + "type": "groupPermission", |
| 70 | + "id": "17" |
| 71 | + }, { |
| 72 | + "type": "groupPermission", |
| 73 | + "id": "19" |
| 74 | + } |
| 75 | + ] |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + """; |
| 81 | + |
| 82 | + @Test |
| 83 | + public void canSeePublicGroupWithoutScope() throws Exception { |
| 84 | + mockMvc.perform(get("/data/userGroup/4") |
| 85 | + .with(getOAuthTokenWithTestUser(NO_SCOPE, NO_AUTHORITIES))) |
| 86 | + .andExpect(status().isOk()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void cannotSeePrivateGroupWithoutScope() throws Exception { |
| 91 | + mockMvc.perform(get("/data/userGroup/5") |
| 92 | + .with(getOAuthTokenWithTestUser(NO_SCOPE, NO_AUTHORITIES))) |
| 93 | + .andExpect(status().isForbidden()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void cannotSeePrivateGroupWithoutRole() throws Exception { |
| 98 | + mockMvc.perform(get("/data/userGroup/5") |
| 99 | + .with(getOAuthTokenWithTestUser(OAuthScope._READ_SENSIBLE_USERDATA, NO_AUTHORITIES))) |
| 100 | + .andExpect(status().isForbidden()); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void canSeePrivateGroupWithScopeAndRole() throws Exception { |
| 105 | + mockMvc.perform(get("/data/userGroup/5") |
| 106 | + .with(getOAuthTokenWithTestUser(OAuthScope._READ_SENSIBLE_USERDATA, GroupPermission.ROLE_READ_USER_GROUP))) |
| 107 | + .andExpect(status().isOk()); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void cannotCreateUserGroupWithoutScope() throws Exception { |
| 112 | + mockMvc.perform(post("/data/userGroup") |
| 113 | + .with(getOAuthTokenWithTestUser(NO_SCOPE, GroupPermission.ROLE_READ_USER_GROUP)) |
| 114 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 115 | + .content(testPost)) |
| 116 | + .andExpect(status().isForbidden()); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void cannotCreateUserGroupWithoutRole() throws Exception { |
| 121 | + mockMvc.perform(post("/data/userGroup") |
| 122 | + .with(getOAuthTokenWithTestUser(OAuthScope._ADMINISTRATIVE_ACTION, NO_AUTHORITIES)) |
| 123 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 124 | + .content(testPost)) |
| 125 | + .andExpect(status().isForbidden()); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void canCreateUserGroupWithScopeAndRole() throws Exception { |
| 130 | + mockMvc.perform(post("/data/userGroup") |
| 131 | + .with(getOAuthTokenWithTestUser(OAuthScope._ADMINISTRATIVE_ACTION, GroupPermission.ROLE_WRITE_USER_GROUP)) |
| 132 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 133 | + .content(testPost)) |
| 134 | + .andExpect(status().isCreated()); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void cannotUpdateUserGroupWithoutScope() throws Exception { |
| 139 | + mockMvc.perform(patch("/data/userGroup/3") |
| 140 | + .with(getOAuthTokenWithTestUser(NO_SCOPE, GroupPermission.ROLE_WRITE_USER_GROUP)) |
| 141 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 142 | + .content(testPatch)) |
| 143 | + .andExpect(status().isForbidden()); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void cannotUpdateUserGroupWithoutRole() throws Exception { |
| 148 | + mockMvc.perform(patch("/data/userGroup/3") |
| 149 | + .with(getOAuthTokenWithTestUser(OAuthScope._ADMINISTRATIVE_ACTION, NO_AUTHORITIES)) |
| 150 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 151 | + .content(testPatch)) |
| 152 | + .andExpect(status().isForbidden()); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void canUpdateUserGroupWithScopeAndRole() throws Exception { |
| 157 | + mockMvc.perform(patch("/data/userGroup/3") |
| 158 | + .with(getOAuthTokenWithTestUser(OAuthScope._ADMINISTRATIVE_ACTION, GroupPermission.ROLE_WRITE_USER_GROUP)) |
| 159 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 160 | + .content(testPatch)) |
| 161 | + .andExpect(status().isNoContent()); |
| 162 | + } |
| 163 | +} |
0 commit comments