blob: 1acf4c2b739dad69ecada00beb8e8040f7a7142d [file] [log] [blame]
Michael Hanlcf9c6ea2016-02-06 15:04:13 +01001package de.ids_mannheim.korap.web.service.full;
2
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;
margaretha4b5c1412017-11-15 20:55:04 +010012import org.springframework.beans.factory.annotation.Autowired;
margaretha843c4632017-10-25 19:16:12 +020013
Michael Hanl2c3b0b12016-07-01 18:30:12 +020014import com.fasterxml.jackson.databind.JsonNode;
15import com.sun.jersey.api.client.ClientResponse;
margaretha843c4632017-10-25 19:16:12 +020016
margaretha4b5c1412017-11-15 20:55:04 +010017import de.ids_mannheim.korap.authentication.framework.HttpAuthorizationHandler;
18import de.ids_mannheim.korap.authentication.framework.TransferEncoding;
Michael Hanl2c3b0b12016-07-01 18:30:12 +020019import de.ids_mannheim.korap.config.Attributes;
margaretha4b5c1412017-11-15 20:55:04 +010020import de.ids_mannheim.korap.config.AuthenticationType;
Michael Hanldaf86602016-05-12 14:31:52 +020021import de.ids_mannheim.korap.config.TestHelper;
22import de.ids_mannheim.korap.exceptions.KustvaktException;
Michael Hanl2c3b0b12016-07-01 18:30:12 +020023import de.ids_mannheim.korap.exceptions.StatusCodes;
Michael Hanldaf86602016-05-12 14:31:52 +020024import de.ids_mannheim.korap.user.User;
Michael Hanl2c3b0b12016-07-01 18:30:12 +020025import de.ids_mannheim.korap.utils.JsonUtils;
26import de.ids_mannheim.korap.utils.TimeUtils;
Michael Hanlcf9c6ea2016-02-06 15:04:13 +010027import de.ids_mannheim.korap.web.service.FastJerseyTest;
Michael Hanl2c3b0b12016-07-01 18:30:12 +020028
Michael Hanlcf9c6ea2016-02-06 15:04:13 +010029/**
margaretha843c4632017-10-25 19:16:12 +020030 * EM: fix tests. New DB does not save users.
Michael Hanlcf9c6ea2016-02-06 15:04:13 +010031 * @author hanl
32 * @date 24/09/2015
33 */
margaretha843c4632017-10-25 19:16:12 +020034@Ignore
Michael Hanlcf9c6ea2016-02-06 15:04:13 +010035public class AuthServiceTest extends FastJerseyTest {
36
margaretha4b5c1412017-11-15 20:55:04 +010037 @Autowired
38 HttpAuthorizationHandler handler;
39
Michael Hanl2c3b0b12016-07-01 18:30:12 +020040 private static String[] credentials;
margaretha4b5c1412017-11-15 20:55:04 +010041
42
Michael Hanldaf86602016-05-12 14:31:52 +020043 @BeforeClass
Michael Hanl8abaf9e2016-05-23 16:46:35 +020044 public static void configure () throws Exception {
Michael Hanl2c3b0b12016-07-01 18:30:12 +020045 credentials = new String[2];
46 credentials[0] = (String) TestHelper.getUserCredentials().get(Attributes.USERNAME);
47 credentials[1] = (String) TestHelper.getUserCredentials().get(Attributes.PASSWORD);
Michael Hanldaf86602016-05-12 14:31:52 +020048 }
49
50
51 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +020052 public void initMethod () throws KustvaktException {
Michael Hanldaf86602016-05-12 14:31:52 +020053 helper().setupAccount();
54 }
55
Michael Hanl8abaf9e2016-05-23 16:46:35 +020056
Michael Hanldaf86602016-05-12 14:31:52 +020057 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020058 public void testBasicHttp () {
Michael Hanldaf86602016-05-12 14:31:52 +020059 User user = helper().getUser();
Michael Hanlc0ed00f2016-06-23 14:33:10 +020060 }
61
62 @Test
margaretha894a7d72017-11-08 19:24:20 +010063 public void testSessionToken() throws KustvaktException {
margaretha4b5c1412017-11-15 20:55:04 +010064 String auth = handler.createAuthorizationHeader(AuthenticationType.SESSION,
65 credentials[0], credentials[1]);
margarethaf18298b2017-09-14 22:14:32 +020066 ClientResponse response = resource().path("auth")
Michael Hanl2c3b0b12016-07-01 18:30:12 +020067 .path("sessionToken").header(Attributes.AUTHORIZATION, auth)
68 .get(ClientResponse.class);
69 assertEquals(ClientResponse.Status.OK.getStatusCode(),
70 response.getStatus());
71 String en = response.getEntity(String.class);
72 JsonNode node = JsonUtils.readTree(en);
73 assertNotNull(node);
74
75 String token = node.path("token").asText();
76 String token_type = node.path("token_type").asText();
77 String expiration = node.path("expires").asText();
78 DateTime ex = TimeUtils.getTime(expiration);
79 assertNotEquals("", token);
80 assertNotEquals("", token_type);
81 assertFalse(TimeUtils.isExpired(ex.getMillis()));
82
margarethaf18298b2017-09-14 22:14:32 +020083 response = resource().path("user")
Michael Hanl2c3b0b12016-07-01 18:30:12 +020084 .path("info").header(Attributes.AUTHORIZATION, token_type + " "+ token)
85 .get(ClientResponse.class);
86 en = response.getEntity(String.class);
87
88 assertEquals(ClientResponse.Status.OK.getStatusCode(),
89 response.getStatus());
margarethaf18298b2017-09-14 22:14:32 +020090
91 response = resource().path("auth")
92 .path("logout").header(Attributes.AUTHORIZATION, token_type + " "+ token)
93 .get(ClientResponse.class);
94
95 assertEquals(ClientResponse.Status.OK.getStatusCode(),
96 response.getStatus());
Michael Hanl2c3b0b12016-07-01 18:30:12 +020097 }
98
99 @Test
margaretha894a7d72017-11-08 19:24:20 +0100100 public void testSessionTokenExpire() throws KustvaktException {
margaretha4b5c1412017-11-15 20:55:04 +0100101 String auth = handler.createAuthorizationHeader(AuthenticationType.SESSION,
102 credentials[0], credentials[1]);
margarethaf18298b2017-09-14 22:14:32 +0200103 ClientResponse response = resource().path("auth")
Michael Hanl2c3b0b12016-07-01 18:30:12 +0200104 .path("sessionToken").header(Attributes.AUTHORIZATION, auth)
105 .get(ClientResponse.class);
106 assertEquals(ClientResponse.Status.OK.getStatusCode(),
107 response.getStatus());
108 String en = response.getEntity(String.class);
109 JsonNode node = JsonUtils.readTree(en);
110 assertNotNull(node);
111
112 String token = node.path("token").asText();
113 String token_type = node.path("token_type").asText();
114 String expiration = node.path("expires").asText();
115 DateTime ex = TimeUtils.getTime(expiration);
116 assertNotEquals("", token);
117 assertNotEquals("", token_type);
118
119 while (true) {
120 if (TimeUtils.isExpired(ex.getMillis()))
121 break;
122 }
margarethaf18298b2017-09-14 22:14:32 +0200123 response = resource().path("user")
Michael Hanl2c3b0b12016-07-01 18:30:12 +0200124 .path("info").header(Attributes.AUTHORIZATION, token_type + " "+ token)
125 .get(ClientResponse.class);
126 en = response.getEntity(String.class);
127 node = JsonUtils.readTree(en);
128 assertNotNull(node);
129
130 assertEquals(StatusCodes.BAD_CREDENTIALS, node.at("/errors/0/0").asInt());
131 assertEquals(ClientResponse.Status.UNAUTHORIZED.getStatusCode(),
132 response.getStatus());
133 }
134
135
margarethaf18298b2017-09-14 22:14:32 +0200136// @Test
137// public void testBlockingFilterFail() {
138//
139// }
140//
141//
142// @Test
143// public void testBasicLogout () {
144//
145// }
146//
147//
148// @Test
149// public void testSessionTokenLogin () {
150//
151// }
152//
153//
154// @Test
155// public void testSessionTokenLogout () {
156//
157// }
158//
159//
160// @Test
161// public void testOpenIDLogin () {
162//
163// }
164//
165//
166// @Test
167// public void testOpenIDLogout () {
168//
169// }
170//
171//
172// // -- are these even right? auth - authorization
173// @Test
174// public void testOAuth2Login () {
175//
176// }
177//
178//
179// @Test
180// public void testOAuth2Logout () {
181//
182// }
Michael Hanlcb2d3f92016-06-02 17:34:06 +0200183
Michael Hanlcf9c6ea2016-02-06 15:04:13 +0100184 //todo: test basicauth via secure connection
185
Michael Hanlcf9c6ea2016-02-06 15:04:13 +0100186}