| Michael Hanl | cf9c6ea | 2016-02-06 15:04:13 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.service.full; |
| 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; |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame^] | 12 | import org.springframework.beans.factory.annotation.Autowired; |
| margaretha | 843c463 | 2017-10-25 19:16:12 +0200 | [diff] [blame] | 13 | |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 14 | import com.fasterxml.jackson.databind.JsonNode; |
| 15 | import com.sun.jersey.api.client.ClientResponse; |
| margaretha | 843c463 | 2017-10-25 19:16:12 +0200 | [diff] [blame] | 16 | |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame^] | 17 | import de.ids_mannheim.korap.authentication.framework.HttpAuthorizationHandler; |
| 18 | import de.ids_mannheim.korap.authentication.framework.TransferEncoding; |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 19 | import de.ids_mannheim.korap.config.Attributes; |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame^] | 20 | import de.ids_mannheim.korap.config.AuthenticationType; |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 21 | import de.ids_mannheim.korap.config.TestHelper; |
| 22 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 23 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 24 | import de.ids_mannheim.korap.user.User; |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 25 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 26 | import de.ids_mannheim.korap.utils.TimeUtils; |
| Michael Hanl | cf9c6ea | 2016-02-06 15:04:13 +0100 | [diff] [blame] | 27 | import de.ids_mannheim.korap.web.service.FastJerseyTest; |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 28 | |
| Michael Hanl | cf9c6ea | 2016-02-06 15:04:13 +0100 | [diff] [blame] | 29 | /** |
| margaretha | 843c463 | 2017-10-25 19:16:12 +0200 | [diff] [blame] | 30 | * EM: fix tests. New DB does not save users. |
| Michael Hanl | cf9c6ea | 2016-02-06 15:04:13 +0100 | [diff] [blame] | 31 | * @author hanl |
| 32 | * @date 24/09/2015 |
| 33 | */ |
| margaretha | 843c463 | 2017-10-25 19:16:12 +0200 | [diff] [blame] | 34 | @Ignore |
| Michael Hanl | cf9c6ea | 2016-02-06 15:04:13 +0100 | [diff] [blame] | 35 | public class AuthServiceTest extends FastJerseyTest { |
| 36 | |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame^] | 37 | @Autowired |
| 38 | HttpAuthorizationHandler handler; |
| 39 | |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 40 | private static String[] credentials; |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame^] | 41 | |
| 42 | |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 43 | @BeforeClass |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 44 | public static void configure () throws Exception { |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 45 | credentials = new String[2]; |
| 46 | credentials[0] = (String) TestHelper.getUserCredentials().get(Attributes.USERNAME); |
| 47 | credentials[1] = (String) TestHelper.getUserCredentials().get(Attributes.PASSWORD); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | |
| 51 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 52 | public void initMethod () throws KustvaktException { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 53 | helper().setupAccount(); |
| 54 | } |
| 55 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 56 | |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 57 | @Test |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 58 | public void testBasicHttp () { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 59 | User user = helper().getUser(); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Test |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 63 | public void testSessionToken() throws KustvaktException { |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame^] | 64 | String auth = handler.createAuthorizationHeader(AuthenticationType.SESSION, |
| 65 | credentials[0], credentials[1]); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 66 | ClientResponse response = resource().path("auth") |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 67 | .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 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 83 | response = resource().path("user") |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 84 | .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()); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 90 | |
| 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 Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Test |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 100 | public void testSessionTokenExpire() throws KustvaktException { |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame^] | 101 | String auth = handler.createAuthorizationHeader(AuthenticationType.SESSION, |
| 102 | credentials[0], credentials[1]); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 103 | ClientResponse response = resource().path("auth") |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 104 | .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 | } |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 123 | response = resource().path("user") |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 124 | .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 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 136 | // @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 Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 183 | |
| Michael Hanl | cf9c6ea | 2016-02-06 15:04:13 +0100 | [diff] [blame] | 184 | //todo: test basicauth via secure connection |
| 185 | |
| Michael Hanl | cf9c6ea | 2016-02-06 15:04:13 +0100 | [diff] [blame] | 186 | } |