| margaretha | 541b8cc | 2018-01-10 13:02:46 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.controller; |
| Michael Hanl | cf9c6ea | 2016-02-06 15:04:13 +0100 | [diff] [blame] | 2 | |
| margaretha | 843c463 | 2017-10-25 19:16:12 +0200 | [diff] [blame] | 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertFalse; |
| 5 | import static org.junit.Assert.assertNotEquals; |
| 6 | import static org.junit.Assert.assertNotNull; |
| 7 | |
| 8 | import org.joda.time.DateTime; |
| 9 | import org.junit.BeforeClass; |
| 10 | import org.junit.Ignore; |
| 11 | import org.junit.Test; |
| 12 | |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 13 | import com.fasterxml.jackson.databind.JsonNode; |
| 14 | import com.sun.jersey.api.client.ClientResponse; |
| margaretha | 843c463 | 2017-10-25 19:16:12 +0200 | [diff] [blame] | 15 | |
| margaretha | 56e8e55 | 2017-12-05 16:31:21 +0100 | [diff] [blame] | 16 | import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler; |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 17 | import de.ids_mannheim.korap.config.Attributes; |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 18 | import de.ids_mannheim.korap.config.TestHelper; |
| 19 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 20 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 21 | import de.ids_mannheim.korap.user.User; |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 22 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 23 | import de.ids_mannheim.korap.utils.TimeUtils; |
| margaretha | 541b8cc | 2018-01-10 13:02:46 +0100 | [diff] [blame] | 24 | import de.ids_mannheim.korap.web.FastJerseyTest; |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 25 | |
| Michael Hanl | cf9c6ea | 2016-02-06 15:04:13 +0100 | [diff] [blame] | 26 | /** |
| margaretha | 843c463 | 2017-10-25 19:16:12 +0200 | [diff] [blame] | 27 | * EM: fix tests. New DB does not save users. |
| Michael Hanl | cf9c6ea | 2016-02-06 15:04:13 +0100 | [diff] [blame] | 28 | * @author hanl |
| 29 | * @date 24/09/2015 |
| 30 | */ |
| margaretha | 843c463 | 2017-10-25 19:16:12 +0200 | [diff] [blame] | 31 | @Ignore |
| margaretha | 541b8cc | 2018-01-10 13:02:46 +0100 | [diff] [blame] | 32 | public class AuthenticationControllerTest extends FastJerseyTest { |
| Michael Hanl | cf9c6ea | 2016-02-06 15:04:13 +0100 | [diff] [blame] | 33 | |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 34 | private static String[] credentials; |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 35 | |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 36 | @BeforeClass |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 37 | public static void configure () throws Exception { |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 38 | credentials = new String[2]; |
| 39 | credentials[0] = (String) TestHelper.getUserCredentials().get(Attributes.USERNAME); |
| 40 | credentials[1] = (String) TestHelper.getUserCredentials().get(Attributes.PASSWORD); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | |
| 44 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 45 | public void initMethod () throws KustvaktException { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 46 | helper().setupAccount(); |
| 47 | } |
| 48 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 49 | |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 50 | @Test |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 51 | public void testBasicHttp () { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 52 | User user = helper().getUser(); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Test |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 56 | public void testSessionToken() throws KustvaktException { |
| margaretha | 064eb6f | 2018-07-10 18:33:01 +0200 | [diff] [blame] | 57 | String auth = HttpAuthorizationHandler.createBasicAuthorizationHeaderValue( |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 58 | credentials[0], credentials[1]); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 59 | ClientResponse response = resource().path("auth") |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 60 | .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 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 76 | response = resource().path("user") |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 77 | .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()); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 83 | |
| 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 Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Test |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 93 | public void testSessionTokenExpire() throws KustvaktException { |
| margaretha | 064eb6f | 2018-07-10 18:33:01 +0200 | [diff] [blame] | 94 | String auth = HttpAuthorizationHandler.createBasicAuthorizationHeaderValue( |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 95 | credentials[0], credentials[1]); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 96 | ClientResponse response = resource().path("auth") |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 97 | .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 | } |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 116 | response = resource().path("user") |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 117 | .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 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 129 | // @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 Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 176 | |
| Michael Hanl | cf9c6ea | 2016-02-06 15:04:13 +0100 | [diff] [blame] | 177 | //todo: test basicauth via secure connection |
| 178 | |
| Michael Hanl | cf9c6ea | 2016-02-06 15:04:13 +0100 | [diff] [blame] | 179 | } |