blob: d7bcde277494d09504ef1415f6761868edda5e27 [file] [log] [blame]
margarethacf306d32018-05-30 19:45:35 +02001package de.ids_mannheim.korap.web.controller;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertNotNull;
5
margarethacf306d32018-05-30 19:45:35 +02006import java.io.IOException;
margarethacf306d32018-05-30 19:45:35 +02007
margarethab1081b12018-07-03 23:35:01 +02008import javax.ws.rs.core.MultivaluedMap;
margaretha20f31232018-07-09 17:49:39 +02009import javax.ws.rs.core.Response.Status;
margarethab1081b12018-07-03 23:35:01 +020010
11import org.apache.http.entity.ContentType;
margarethacf306d32018-05-30 19:45:35 +020012import org.junit.Test;
13
14import com.fasterxml.jackson.databind.JsonNode;
15import com.google.common.net.HttpHeaders;
16import com.sun.jersey.api.client.ClientResponse;
margarethab1081b12018-07-03 23:35:01 +020017import com.sun.jersey.core.util.MultivaluedMapImpl;
margarethacf306d32018-05-30 19:45:35 +020018
margaretha064eb6f2018-07-10 18:33:01 +020019import de.ids_mannheim.korap.authentication.http.TransferEncoding;
margarethacf306d32018-05-30 19:45:35 +020020import de.ids_mannheim.korap.config.Attributes;
21import de.ids_mannheim.korap.config.SpringJerseyTest;
22import de.ids_mannheim.korap.exceptions.KustvaktException;
23import de.ids_mannheim.korap.exceptions.StatusCodes;
margaretha20f31232018-07-09 17:49:39 +020024import de.ids_mannheim.korap.oauth2.constant.OAuth2Scope;
margarethacf306d32018-05-30 19:45:35 +020025import de.ids_mannheim.korap.utils.JsonUtils;
26
27public class OAuth2AccessTokenTest extends SpringJerseyTest {
28
margaretha49cb6882018-07-04 04:19:54 +020029 private String requestToken () throws KustvaktException {
30 MultivaluedMap<String, String> form = new MultivaluedMapImpl();
31 form.add("grant_type", "password");
32 form.add("client_id", "fCBbQkAyYzI4NzUxMg");
33 form.add("client_secret", "secret");
34 form.add("username", "dory");
35 form.add("password", "password");
margarethab1081b12018-07-03 23:35:01 +020036
margaretha49cb6882018-07-04 04:19:54 +020037 ClientResponse response = resource().path("oauth2").path("token")
38 .header(HttpHeaders.CONTENT_TYPE,
39 ContentType.APPLICATION_FORM_URLENCODED)
40 .entity(form).post(ClientResponse.class);
margarethab1081b12018-07-03 23:35:01 +020041
margaretha49cb6882018-07-04 04:19:54 +020042 String entity = response.getEntity(String.class);
43 JsonNode node = JsonUtils.readTree(entity);
44 return node.at("/access_token").asText();
margarethab1081b12018-07-03 23:35:01 +020045 }
margaretha064eb6f2018-07-10 18:33:01 +020046
margaretha20f31232018-07-09 17:49:39 +020047 @Test
margaretha064eb6f2018-07-10 18:33:01 +020048 public void testListVCScope () throws KustvaktException {
margaretha20f31232018-07-09 17:49:39 +020049 MultivaluedMap<String, String> form = new MultivaluedMapImpl();
50 form.add("grant_type", "password");
51 form.add("client_id", "fCBbQkAyYzI4NzUxMg");
52 form.add("client_secret", "secret");
53 form.add("username", "dory");
54 form.add("password", "password");
55 form.add("scope", OAuth2Scope.VC_INFO.toString());
56
57 ClientResponse response = resource().path("oauth2").path("token")
margaretha064eb6f2018-07-10 18:33:01 +020058 .header(HttpHeaders.AUTHORIZATION,
59 "Bearer" + TransferEncoding
60 .encodeBase64("fCBbQkAyYzI4NzUxMg", "secret"))
margaretha20f31232018-07-09 17:49:39 +020061 .header(HttpHeaders.CONTENT_TYPE,
62 ContentType.APPLICATION_FORM_URLENCODED)
63 .entity(form).post(ClientResponse.class);
64
65 String entity = response.getEntity(String.class);
66 JsonNode node = JsonUtils.readTree(entity);
67 String token = node.at("/access_token").asText();
margaretha064eb6f2018-07-10 18:33:01 +020068
margaretha20f31232018-07-09 17:49:39 +020069 response = resource().path("vc").path("list")
70 .header(Attributes.AUTHORIZATION, "Bearer " + token)
71 .get(ClientResponse.class);
72
73 assertEquals(Status.OK.getStatusCode(), response.getStatus());
74 entity = response.getEntity(String.class);
75 node = JsonUtils.readTree(entity);
76 assertEquals(4, node.size());
77 }
margarethacf306d32018-05-30 19:45:35 +020078
79 @Test
margaretha064eb6f2018-07-10 18:33:01 +020080 public void testListVCScopeNotAuthorized ()
81 throws KustvaktException, IOException {
82 String accessToken = requestToken();
margarethacf306d32018-05-30 19:45:35 +020083 ClientResponse response = resource().path("vc").path("list")
margaretha064eb6f2018-07-10 18:33:01 +020084 .header(Attributes.AUTHORIZATION, "Bearer " + accessToken)
margarethacf306d32018-05-30 19:45:35 +020085 .get(ClientResponse.class);
86
margaretha20f31232018-07-09 17:49:39 +020087 assertEquals(ClientResponse.Status.UNAUTHORIZED.getStatusCode(),
88 response.getStatus());
margarethacf306d32018-05-30 19:45:35 +020089 String entity = response.getEntity(String.class);
90 JsonNode node = JsonUtils.readTree(entity);
margaretha20f31232018-07-09 17:49:39 +020091 assertEquals(StatusCodes.AUTHORIZATION_FAILED,
92 node.at("/errors/0/0").asInt());
93 assertEquals("Scope vc_info is not authorized",
94 node.at("/errors/0/1").asText());
margaretha064eb6f2018-07-10 18:33:01 +020095
margaretha0a45be12018-07-12 15:06:30 +020096 testListVCAccessBearerNotAuthorize(accessToken);
margaretha064eb6f2018-07-10 18:33:01 +020097 testSearchWithOAuth2Token(accessToken);
margarethacf306d32018-05-30 19:45:35 +020098 }
99
margaretha0a45be12018-07-12 15:06:30 +0200100 private void testListVCAccessBearerNotAuthorize (String accessToken)
101 throws KustvaktException {
102 ClientResponse response =
103 resource().path("vc").path("access").path("list")
104 .header(Attributes.AUTHORIZATION,
105 "Bearer " + accessToken)
106 .get(ClientResponse.class);
107 String entity = response.getEntity(String.class);
108 assertEquals(ClientResponse.Status.UNAUTHORIZED.getStatusCode(),
109 response.getStatus());
110 JsonNode node = JsonUtils.readTree(entity);
111 assertEquals(StatusCodes.AUTHORIZATION_FAILED,
112 node.at("/errors/0/0").asInt());
113 assertEquals("Token type Bearer is not allowed",
114 node.at("/errors/0/1").asText());
115 }
116
margaretha064eb6f2018-07-10 18:33:01 +0200117 private void testSearchWithOAuth2Token (String accessToken)
margarethacf306d32018-05-30 19:45:35 +0200118 throws KustvaktException, IOException {
119 ClientResponse response = resource().path("search")
120 .queryParam("q", "Wasser").queryParam("ql", "poliqarp")
margaretha064eb6f2018-07-10 18:33:01 +0200121 .header(Attributes.AUTHORIZATION, "Bearer " + accessToken)
margarethacf306d32018-05-30 19:45:35 +0200122 .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
123 .get(ClientResponse.class);
124
margaretha20f31232018-07-09 17:49:39 +0200125 String ent = response.getEntity(String.class);
margaretha064eb6f2018-07-10 18:33:01 +0200126
margarethacf306d32018-05-30 19:45:35 +0200127 assertEquals(ClientResponse.Status.OK.getStatusCode(),
128 response.getStatus());
margaretha064eb6f2018-07-10 18:33:01 +0200129
margarethacf306d32018-05-30 19:45:35 +0200130 JsonNode node = JsonUtils.readTree(ent);
131 assertNotNull(node);
132 assertEquals(25, node.at("/matches").size());
133 }
134
135 @Test
136 public void testSearchWithUnknownToken ()
137 throws KustvaktException, IOException {
138 ClientResponse response = resource().path("search")
139 .queryParam("q", "Wasser").queryParam("ql", "poliqarp")
140 .header(Attributes.AUTHORIZATION,
141 "Bearer ljsa8tKNRSczJhk20öhq92zG8z350")
142 .get(ClientResponse.class);
143
144 assertEquals(ClientResponse.Status.UNAUTHORIZED.getStatusCode(),
145 response.getStatus());
146
147 String ent = response.getEntity(String.class);
148 JsonNode node = JsonUtils.readTree(ent);
149 assertEquals(StatusCodes.INVALID_ACCESS_TOKEN,
150 node.at("/errors/0/0").asInt());
margarethaa2ce63d2018-06-28 10:11:43 +0200151 assertEquals("Access token is not found",
152 node.at("/errors/0/1").asText());
margarethacf306d32018-05-30 19:45:35 +0200153 }
margarethacf306d32018-05-30 19:45:35 +0200154}