| Michael Hanl | cf9c6ea | 2016-02-06 15:04:13 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.service.full; |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 2 | |
| margaretha | 843c463 | 2017-10-25 19:16:12 +0200 | [diff] [blame] | 3 | import static org.junit.Assert.assertEquals; |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 4 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 5 | import java.util.Map; |
| 6 | |
| margaretha | 843c463 | 2017-10-25 19:16:12 +0200 | [diff] [blame] | 7 | import org.junit.Ignore; |
| 8 | import org.junit.Test; |
| 9 | |
| 10 | import com.fasterxml.jackson.databind.JsonNode; |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame^] | 11 | import com.sun.jersey.api.client.ClientHandlerException; |
| margaretha | 843c463 | 2017-10-25 19:16:12 +0200 | [diff] [blame] | 12 | import com.sun.jersey.api.client.ClientResponse; |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame^] | 13 | import com.sun.jersey.api.client.UniformInterfaceException; |
| margaretha | 843c463 | 2017-10-25 19:16:12 +0200 | [diff] [blame] | 14 | |
| 15 | import de.ids_mannheim.korap.config.Attributes; |
| 16 | import de.ids_mannheim.korap.config.TestHelper; |
| 17 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 18 | import de.ids_mannheim.korap.security.auth.BasicHttpAuth; |
| 19 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 20 | import de.ids_mannheim.korap.web.service.FastJerseyTest; |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 21 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 22 | /** |
| 23 | * @author hanl |
| 24 | * @date 23/09/2015 |
| 25 | */ |
| margaretha | 843c463 | 2017-10-25 19:16:12 +0200 | [diff] [blame] | 26 | @Ignore |
| Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 27 | // todo: in combination with other tests, causes failures! |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 28 | public class OAuth2EndpointTest extends FastJerseyTest { |
| 29 | |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 30 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 31 | public void initMethod () throws KustvaktException { |
| margaretha | 843c463 | 2017-10-25 19:16:12 +0200 | [diff] [blame] | 32 | // helper().setupAccount(); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 35 | |
| Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 36 | @Test |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame^] | 37 | public void testAuthorizeClient () throws ClientHandlerException, UniformInterfaceException, KustvaktException { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 38 | String auth = BasicHttpAuth.encode(helper().getUser().getUsername(), |
| 39 | (String) TestHelper.getUserCredentials().get(Attributes.PASSWORD)); |
| 40 | ClientResponse response = resource().path(getAPIVersion()).path("oauth2") |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 41 | .path("register") |
| 42 | .queryParam("redirect_url", "korap.ids-mannheim.de/redirect") |
| Michael Hanl | e803191 | 2016-06-24 14:36:17 +0200 | [diff] [blame] | 43 | .queryParam("application_name", "Kustvakt test") |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 44 | .header("Host", "korap.ids-mannheim.de") |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 45 | .header(Attributes.AUTHORIZATION, auth) |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 46 | .post(ClientResponse.class); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 47 | |
| 48 | JsonNode node = JsonUtils.readTree(response.getEntity(String.class)); |
| Michael Hanl | e803191 | 2016-06-24 14:36:17 +0200 | [diff] [blame] | 49 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 50 | |
| 51 | assertEquals(ClientResponse.Status.OK.getStatusCode(), |
| 52 | response.getStatus()); |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 55 | |
| Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 56 | @Test |
| 57 | @Ignore |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame^] | 58 | public void testRevokeClient () throws ClientHandlerException, UniformInterfaceException, KustvaktException { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 59 | ClientResponse response = resource().path(getAPIVersion()).path("oauth2") |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 60 | .path("register") |
| 61 | .queryParam("redirect_url", "korap.ids-mannheim.de/redirect") |
| 62 | .header("Host", "korap.ids-mannheim.de") |
| 63 | .post(ClientResponse.class); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 64 | assertEquals(ClientResponse.Status.OK.getStatusCode(), |
| 65 | response.getStatus()); |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 66 | JsonNode node = JsonUtils.readTree(response.getEntity(String.class)); |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 67 | |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 70 | |
| Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 71 | @Test |
| 72 | @Ignore |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame^] | 73 | public void authenticate () throws KustvaktException { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 74 | 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 Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 77 | .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 Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 82 | assertEquals(ClientResponse.Status.OK.getStatusCode(), |
| 83 | response.getStatus()); |
| 84 | |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 85 | 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 Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 91 | response = resource().path(getAPIVersion()).path("oauth2").path("authorize") |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 92 | .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 Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 96 | // .header(Attributes.AUTHORIZATION, enc) |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 97 | .header("Content-Type", "application/x-www-form-urlencoded") |
| 98 | .post(ClientResponse.class); |
| 99 | |
| 100 | e = response.getEntity(String.class); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 101 | assertEquals(ClientResponse.Status.OK.getStatusCode(), |
| 102 | response.getStatus()); |
| 103 | |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 104 | node = JsonUtils.readTree(e); |
| 105 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 106 | response = resource().path(getAPIVersion()).path("oauth2").path("authorize") |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 107 | .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 Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 112 | assertEquals(ClientResponse.Status.OK.getStatusCode(), |
| 113 | response.getStatus()); |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 116 | } |