blob: 9997c10675888b69dc9dd1dca25cfdd21475feb0 [file] [log] [blame]
margaretha541b8cc2018-01-10 13:02:46 +01001package de.ids_mannheim.korap.web.controller;
Michael Hanlcf9c6ea2016-02-06 15:04:13 +01002
margaretha843c4632017-10-25 19:16:12 +02003import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertNotEquals;
6import static org.junit.Assert.assertNotNull;
7
8import org.joda.time.DateTime;
9import org.junit.BeforeClass;
10import org.junit.Ignore;
11import org.junit.Test;
12
Michael Hanl2c3b0b12016-07-01 18:30:12 +020013import com.fasterxml.jackson.databind.JsonNode;
14import com.sun.jersey.api.client.ClientResponse;
margaretha843c4632017-10-25 19:16:12 +020015
margaretha56e8e552017-12-05 16:31:21 +010016import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler;
Michael Hanl2c3b0b12016-07-01 18:30:12 +020017import de.ids_mannheim.korap.config.Attributes;
Michael Hanldaf86602016-05-12 14:31:52 +020018import de.ids_mannheim.korap.config.TestHelper;
19import de.ids_mannheim.korap.exceptions.KustvaktException;
Michael Hanl2c3b0b12016-07-01 18:30:12 +020020import de.ids_mannheim.korap.exceptions.StatusCodes;
Michael Hanldaf86602016-05-12 14:31:52 +020021import de.ids_mannheim.korap.user.User;
Michael Hanl2c3b0b12016-07-01 18:30:12 +020022import de.ids_mannheim.korap.utils.JsonUtils;
23import de.ids_mannheim.korap.utils.TimeUtils;
margaretha541b8cc2018-01-10 13:02:46 +010024import de.ids_mannheim.korap.web.FastJerseyTest;
Michael Hanl2c3b0b12016-07-01 18:30:12 +020025
Michael Hanlcf9c6ea2016-02-06 15:04:13 +010026/**
margaretha843c4632017-10-25 19:16:12 +020027 * EM: fix tests. New DB does not save users.
Michael Hanlcf9c6ea2016-02-06 15:04:13 +010028 * @author hanl
29 * @date 24/09/2015
30 */
margaretha843c4632017-10-25 19:16:12 +020031@Ignore
margaretha541b8cc2018-01-10 13:02:46 +010032public class AuthenticationControllerTest extends FastJerseyTest {
Michael Hanlcf9c6ea2016-02-06 15:04:13 +010033
Michael Hanl2c3b0b12016-07-01 18:30:12 +020034 private static String[] credentials;
margaretha4b5c1412017-11-15 20:55:04 +010035
Michael Hanldaf86602016-05-12 14:31:52 +020036 @BeforeClass
Michael Hanl8abaf9e2016-05-23 16:46:35 +020037 public static void configure () throws Exception {
Michael Hanl2c3b0b12016-07-01 18:30:12 +020038 credentials = new String[2];
39 credentials[0] = (String) TestHelper.getUserCredentials().get(Attributes.USERNAME);
40 credentials[1] = (String) TestHelper.getUserCredentials().get(Attributes.PASSWORD);
Michael Hanldaf86602016-05-12 14:31:52 +020041 }
42
43
44 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +020045 public void initMethod () throws KustvaktException {
Michael Hanldaf86602016-05-12 14:31:52 +020046 helper().setupAccount();
47 }
48
Michael Hanl8abaf9e2016-05-23 16:46:35 +020049
Michael Hanldaf86602016-05-12 14:31:52 +020050 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020051 public void testBasicHttp () {
Michael Hanldaf86602016-05-12 14:31:52 +020052 User user = helper().getUser();
Michael Hanlc0ed00f2016-06-23 14:33:10 +020053 }
54
55 @Test
margaretha894a7d72017-11-08 19:24:20 +010056 public void testSessionToken() throws KustvaktException {
margaretha064eb6f2018-07-10 18:33:01 +020057 String auth = HttpAuthorizationHandler.createBasicAuthorizationHeaderValue(
margaretha4b5c1412017-11-15 20:55:04 +010058 credentials[0], credentials[1]);
margarethaf18298b2017-09-14 22:14:32 +020059 ClientResponse response = resource().path("auth")
Michael Hanl2c3b0b12016-07-01 18:30:12 +020060 .path("sessionToken").header(Attributes.AUTHORIZATION, auth)
61 .get(ClientResponse.class);
62 assertEquals(ClientResponse.Status.OK.getStatusCode(),
63 response.getStatus());
64 String en = response.getEntity(String.class);
65 JsonNode node = JsonUtils.readTree(en);
66 assertNotNull(node);
67
68 String token = node.path("token").asText();
69 String token_type = node.path("token_type").asText();
70 String expiration = node.path("expires").asText();
71 DateTime ex = TimeUtils.getTime(expiration);
72 assertNotEquals("", token);
73 assertNotEquals("", token_type);
74 assertFalse(TimeUtils.isExpired(ex.getMillis()));
75
margarethaf18298b2017-09-14 22:14:32 +020076 response = resource().path("user")
Michael Hanl2c3b0b12016-07-01 18:30:12 +020077 .path("info").header(Attributes.AUTHORIZATION, token_type + " "+ token)
78 .get(ClientResponse.class);
79 en = response.getEntity(String.class);
80
81 assertEquals(ClientResponse.Status.OK.getStatusCode(),
82 response.getStatus());
margarethaf18298b2017-09-14 22:14:32 +020083
84 response = resource().path("auth")
85 .path("logout").header(Attributes.AUTHORIZATION, token_type + " "+ token)
86 .get(ClientResponse.class);
87
88 assertEquals(ClientResponse.Status.OK.getStatusCode(),
89 response.getStatus());
Michael Hanl2c3b0b12016-07-01 18:30:12 +020090 }
91
92 @Test
margaretha894a7d72017-11-08 19:24:20 +010093 public void testSessionTokenExpire() throws KustvaktException {
margaretha064eb6f2018-07-10 18:33:01 +020094 String auth = HttpAuthorizationHandler.createBasicAuthorizationHeaderValue(
margaretha4b5c1412017-11-15 20:55:04 +010095 credentials[0], credentials[1]);
margarethaf18298b2017-09-14 22:14:32 +020096 ClientResponse response = resource().path("auth")
Michael Hanl2c3b0b12016-07-01 18:30:12 +020097 .path("sessionToken").header(Attributes.AUTHORIZATION, auth)
98 .get(ClientResponse.class);
99 assertEquals(ClientResponse.Status.OK.getStatusCode(),
100 response.getStatus());
101 String en = response.getEntity(String.class);
102 JsonNode node = JsonUtils.readTree(en);
103 assertNotNull(node);
104
105 String token = node.path("token").asText();
106 String token_type = node.path("token_type").asText();
107 String expiration = node.path("expires").asText();
108 DateTime ex = TimeUtils.getTime(expiration);
109 assertNotEquals("", token);
110 assertNotEquals("", token_type);
111
112 while (true) {
113 if (TimeUtils.isExpired(ex.getMillis()))
114 break;
115 }
margarethaf18298b2017-09-14 22:14:32 +0200116 response = resource().path("user")
Michael Hanl2c3b0b12016-07-01 18:30:12 +0200117 .path("info").header(Attributes.AUTHORIZATION, token_type + " "+ token)
118 .get(ClientResponse.class);
119 en = response.getEntity(String.class);
120 node = JsonUtils.readTree(en);
121 assertNotNull(node);
122
123 assertEquals(StatusCodes.BAD_CREDENTIALS, node.at("/errors/0/0").asInt());
124 assertEquals(ClientResponse.Status.UNAUTHORIZED.getStatusCode(),
125 response.getStatus());
126 }
127
128
margarethaf18298b2017-09-14 22:14:32 +0200129// @Test
130// public void testBlockingFilterFail() {
131//
132// }
133//
134//
135// @Test
136// public void testBasicLogout () {
137//
138// }
139//
140//
141// @Test
142// public void testSessionTokenLogin () {
143//
144// }
145//
146//
147// @Test
148// public void testSessionTokenLogout () {
149//
150// }
151//
152//
153// @Test
154// public void testOpenIDLogin () {
155//
156// }
157//
158//
159// @Test
160// public void testOpenIDLogout () {
161//
162// }
163//
164//
165// // -- are these even right? auth - authorization
166// @Test
167// public void testOAuth2Login () {
168//
169// }
170//
171//
172// @Test
173// public void testOAuth2Logout () {
174//
175// }
Michael Hanlcb2d3f92016-06-02 17:34:06 +0200176
Michael Hanlcf9c6ea2016-02-06 15:04:13 +0100177 //todo: test basicauth via secure connection
178
Michael Hanlcf9c6ea2016-02-06 15:04:13 +0100179}