| margaretha | cf306d3 | 2018-05-30 19:45:35 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.controller; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertNotNull; |
| 5 | |
| 6 | import java.io.BufferedReader; |
| 7 | import java.io.IOException; |
| 8 | import java.io.InputStream; |
| 9 | import java.io.InputStreamReader; |
| 10 | |
| 11 | import org.junit.BeforeClass; |
| 12 | import org.junit.Test; |
| 13 | |
| 14 | import com.fasterxml.jackson.databind.JsonNode; |
| 15 | import com.google.common.net.HttpHeaders; |
| 16 | import com.sun.jersey.api.client.ClientResponse; |
| 17 | import com.sun.jersey.api.client.ClientResponse.Status; |
| 18 | |
| 19 | import de.ids_mannheim.korap.config.Attributes; |
| 20 | import de.ids_mannheim.korap.config.SpringJerseyTest; |
| 21 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 22 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 23 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 24 | |
| 25 | public class OAuth2AccessTokenTest extends SpringJerseyTest { |
| 26 | |
| 27 | // test access token for username: dory |
| 28 | private static String testAccessToken; |
| 29 | |
| 30 | @BeforeClass |
| 31 | public static void init () throws IOException { |
| 32 | InputStream is = OAuth2AccessTokenTest.class.getClassLoader() |
| 33 | .getResourceAsStream("test-oauth2.token"); |
| 34 | |
| 35 | try (BufferedReader reader = |
| 36 | new BufferedReader(new InputStreamReader(is));) { |
| 37 | testAccessToken = reader.readLine(); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | @Test |
| 42 | public void testListVC () throws KustvaktException { |
| 43 | ClientResponse response = resource().path("vc").path("list") |
| 44 | .header(Attributes.AUTHORIZATION, "Bearer " + testAccessToken) |
| 45 | .get(ClientResponse.class); |
| 46 | |
| 47 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 48 | String entity = response.getEntity(String.class); |
| 49 | JsonNode node = JsonUtils.readTree(entity); |
| 50 | assertEquals(4, node.size()); |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | public void testSearchWithOAuth2Token () |
| 55 | throws KustvaktException, IOException { |
| 56 | ClientResponse response = resource().path("search") |
| 57 | .queryParam("q", "Wasser").queryParam("ql", "poliqarp") |
| 58 | .header(Attributes.AUTHORIZATION, "Bearer " + testAccessToken) |
| 59 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 60 | .get(ClientResponse.class); |
| 61 | |
| 62 | assertEquals(ClientResponse.Status.OK.getStatusCode(), |
| 63 | response.getStatus()); |
| 64 | |
| 65 | String ent = response.getEntity(String.class); |
| 66 | JsonNode node = JsonUtils.readTree(ent); |
| 67 | assertNotNull(node); |
| 68 | assertEquals(25, node.at("/matches").size()); |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void testSearchWithUnknownToken () |
| 73 | throws KustvaktException, IOException { |
| 74 | ClientResponse response = resource().path("search") |
| 75 | .queryParam("q", "Wasser").queryParam("ql", "poliqarp") |
| 76 | .header(Attributes.AUTHORIZATION, |
| 77 | "Bearer ljsa8tKNRSczJhk20öhq92zG8z350") |
| 78 | .get(ClientResponse.class); |
| 79 | |
| 80 | assertEquals(ClientResponse.Status.UNAUTHORIZED.getStatusCode(), |
| 81 | response.getStatus()); |
| 82 | |
| 83 | String ent = response.getEntity(String.class); |
| 84 | JsonNode node = JsonUtils.readTree(ent); |
| 85 | assertEquals(StatusCodes.INVALID_ACCESS_TOKEN, |
| 86 | node.at("/errors/0/0").asInt()); |
| 87 | assertEquals("Access token is not found", node.at("/errors/0/1").asText()); |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void testSearchWithExpiredToken () |
| 92 | throws KustvaktException, IOException { |
| 93 | ClientResponse response = resource().path("search") |
| 94 | .queryParam("q", "Wasser").queryParam("ql", "poliqarp") |
| 95 | .header(Attributes.AUTHORIZATION, |
| 96 | "Bearer fia0123ikBWn931470H8s5gRqx7Moc4p") |
| 97 | .get(ClientResponse.class); |
| 98 | |
| 99 | String ent = response.getEntity(String.class); |
| 100 | |
| 101 | assertEquals(ClientResponse.Status.UNAUTHORIZED.getStatusCode(), |
| 102 | response.getStatus()); |
| 103 | |
| 104 | JsonNode node = JsonUtils.readTree(ent); |
| 105 | assertEquals(StatusCodes.EXPIRED, node.at("/errors/0/0").asInt()); |
| 106 | assertEquals("Access token is expired", node.at("/errors/0/1").asText()); |
| 107 | } |
| 108 | } |