blob: 807ee1f9eb168cda90468b32f623835a29f1e801 [file] [log] [blame]
Michael Hanlcf9c6ea2016-02-06 15:04:13 +01001package de.ids_mannheim.korap.web.service.full;
Michael Hanle25dea22015-09-24 19:37:56 +02002
margaretha843c4632017-10-25 19:16:12 +02003import static org.junit.Assert.assertEquals;
Michael Hanl482f30d2015-09-25 12:39:46 +02004
Michael Hanlc0ed00f2016-06-23 14:33:10 +02005import java.util.Map;
6
margaretha843c4632017-10-25 19:16:12 +02007import org.junit.Ignore;
8import org.junit.Test;
9
10import com.fasterxml.jackson.databind.JsonNode;
margaretha894a7d72017-11-08 19:24:20 +010011import com.sun.jersey.api.client.ClientHandlerException;
margaretha843c4632017-10-25 19:16:12 +020012import com.sun.jersey.api.client.ClientResponse;
margaretha894a7d72017-11-08 19:24:20 +010013import com.sun.jersey.api.client.UniformInterfaceException;
margaretha843c4632017-10-25 19:16:12 +020014
15import de.ids_mannheim.korap.config.Attributes;
16import de.ids_mannheim.korap.config.TestHelper;
17import de.ids_mannheim.korap.exceptions.KustvaktException;
18import de.ids_mannheim.korap.security.auth.BasicHttpAuth;
19import de.ids_mannheim.korap.utils.JsonUtils;
20import de.ids_mannheim.korap.web.service.FastJerseyTest;
Michael Hanlc0ed00f2016-06-23 14:33:10 +020021
Michael Hanle25dea22015-09-24 19:37:56 +020022/**
23 * @author hanl
24 * @date 23/09/2015
25 */
margaretha843c4632017-10-25 19:16:12 +020026@Ignore
Michael Hanl25aac542016-02-01 18:16:44 +010027// todo: in combination with other tests, causes failures!
Michael Hanl482f30d2015-09-25 12:39:46 +020028public class OAuth2EndpointTest extends FastJerseyTest {
29
Michael Hanldaf86602016-05-12 14:31:52 +020030 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +020031 public void initMethod () throws KustvaktException {
margaretha843c4632017-10-25 19:16:12 +020032// helper().setupAccount();
Michael Hanldaf86602016-05-12 14:31:52 +020033 }
34
Michael Hanl8abaf9e2016-05-23 16:46:35 +020035
Michael Hanl25aac542016-02-01 18:16:44 +010036 @Test
margaretha894a7d72017-11-08 19:24:20 +010037 public void testAuthorizeClient () throws ClientHandlerException, UniformInterfaceException, KustvaktException {
Michael Hanlc0ed00f2016-06-23 14:33:10 +020038 String auth = BasicHttpAuth.encode(helper().getUser().getUsername(),
39 (String) TestHelper.getUserCredentials().get(Attributes.PASSWORD));
40 ClientResponse response = resource().path(getAPIVersion()).path("oauth2")
Michael Hanl482f30d2015-09-25 12:39:46 +020041 .path("register")
42 .queryParam("redirect_url", "korap.ids-mannheim.de/redirect")
Michael Hanle8031912016-06-24 14:36:17 +020043 .queryParam("application_name", "Kustvakt test")
Michael Hanl482f30d2015-09-25 12:39:46 +020044 .header("Host", "korap.ids-mannheim.de")
Michael Hanlc0ed00f2016-06-23 14:33:10 +020045 .header(Attributes.AUTHORIZATION, auth)
Michael Hanl482f30d2015-09-25 12:39:46 +020046 .post(ClientResponse.class);
Michael Hanlc0ed00f2016-06-23 14:33:10 +020047
48 JsonNode node = JsonUtils.readTree(response.getEntity(String.class));
Michael Hanle8031912016-06-24 14:36:17 +020049
Michael Hanlc0ed00f2016-06-23 14:33:10 +020050
51 assertEquals(ClientResponse.Status.OK.getStatusCode(),
52 response.getStatus());
Michael Hanlf21773f2015-10-16 23:02:31 +020053 }
54
Michael Hanl8abaf9e2016-05-23 16:46:35 +020055
Michael Hanl25aac542016-02-01 18:16:44 +010056 @Test
57 @Ignore
margaretha894a7d72017-11-08 19:24:20 +010058 public void testRevokeClient () throws ClientHandlerException, UniformInterfaceException, KustvaktException {
Michael Hanlc0ed00f2016-06-23 14:33:10 +020059 ClientResponse response = resource().path(getAPIVersion()).path("oauth2")
Michael Hanl19390652016-01-16 11:01:24 +010060 .path("register")
61 .queryParam("redirect_url", "korap.ids-mannheim.de/redirect")
62 .header("Host", "korap.ids-mannheim.de")
63 .post(ClientResponse.class);
Michael Hanlc0ed00f2016-06-23 14:33:10 +020064 assertEquals(ClientResponse.Status.OK.getStatusCode(),
65 response.getStatus());
Michael Hanl19390652016-01-16 11:01:24 +010066 JsonNode node = JsonUtils.readTree(response.getEntity(String.class));
Michael Hanlf21773f2015-10-16 23:02:31 +020067
Michael Hanl482f30d2015-09-25 12:39:46 +020068 }
69
Michael Hanl8abaf9e2016-05-23 16:46:35 +020070
Michael Hanl25aac542016-02-01 18:16:44 +010071 @Test
72 @Ignore
margaretha894a7d72017-11-08 19:24:20 +010073 public void authenticate () throws KustvaktException {
Michael Hanlc0ed00f2016-06-23 14:33:10 +020074 Map<String, Object> cred = TestHelper.getUserCredentials();
75 String enc = BasicHttpAuth.encode((String) cred.get(Attributes.USERNAME), (String) cred.get(Attributes.PASSWORD));
76 ClientResponse response = resource().path(getAPIVersion()).path("oauth2")
Michael Hanl19390652016-01-16 11:01:24 +010077 .path("register")
78 .queryParam("redirect_url", "korap.ids-mannheim.de/redirect")
79 .header("Host", "korap.ids-mannheim.de")
80 .header(Attributes.AUTHORIZATION, enc)
81 .post(ClientResponse.class);
Michael Hanlc0ed00f2016-06-23 14:33:10 +020082 assertEquals(ClientResponse.Status.OK.getStatusCode(),
83 response.getStatus());
84
Michael Hanl19390652016-01-16 11:01:24 +010085 String e = response.getEntity(String.class);
86 JsonNode node = JsonUtils.readTree(e);
87
88 String cl_s = node.path("client_secret").asText();
89 String cl_id = node.path("client_id").asText();
90
Michael Hanlc0ed00f2016-06-23 14:33:10 +020091 response = resource().path(getAPIVersion()).path("oauth2").path("authorize")
Michael Hanl19390652016-01-16 11:01:24 +010092 .queryParam("client_id", cl_id)
93 .queryParam("client_secret", cl_s)
94 .queryParam("response_type", "code")
95 .queryParam("redirect_uri", "korap.ids-mannheim.de/redirect")
Michael Hanl25aac542016-02-01 18:16:44 +010096 // .header(Attributes.AUTHORIZATION, enc)
Michael Hanl19390652016-01-16 11:01:24 +010097 .header("Content-Type", "application/x-www-form-urlencoded")
98 .post(ClientResponse.class);
99
100 e = response.getEntity(String.class);
Michael Hanlc0ed00f2016-06-23 14:33:10 +0200101 assertEquals(ClientResponse.Status.OK.getStatusCode(),
102 response.getStatus());
103
Michael Hanl19390652016-01-16 11:01:24 +0100104 node = JsonUtils.readTree(e);
105
Michael Hanlc0ed00f2016-06-23 14:33:10 +0200106 response = resource().path(getAPIVersion()).path("oauth2").path("authorize")
Michael Hanl19390652016-01-16 11:01:24 +0100107 .queryParam("code", node.path("authorization_code").asText())
108 .queryParam("grant_type", "authorization_code")
109 .queryParam("client_id", cl_id)
110 .queryParam("client_secret", cl_s).post(ClientResponse.class);
111
Michael Hanlc0ed00f2016-06-23 14:33:10 +0200112 assertEquals(ClientResponse.Status.OK.getStatusCode(),
113 response.getStatus());
Michael Hanl19390652016-01-16 11:01:24 +0100114 }
115
Michael Hanle25dea22015-09-24 19:37:56 +0200116}