| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.service; |
| 2 | |
| 3 | import de.ids_mannheim.korap.config.AuthCodeInfo; |
| 4 | import de.ids_mannheim.korap.config.BeanConfiguration; |
| 5 | import de.ids_mannheim.korap.config.ClientInfo; |
| Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 6 | import de.ids_mannheim.korap.config.TestHelper; |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 7 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 8 | import de.ids_mannheim.korap.handlers.OAuth2Handler; |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 9 | import de.ids_mannheim.korap.interfaces.EncryptionIface; |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 10 | import de.ids_mannheim.korap.user.TokenContext; |
| 11 | import de.ids_mannheim.korap.user.User; |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 12 | import org.junit.AfterClass; |
| 13 | import org.junit.Assert; |
| 14 | import org.junit.BeforeClass; |
| 15 | import org.junit.Test; |
| 16 | |
| 17 | /** |
| 18 | * @author hanl |
| 19 | * @date 13/05/2015 |
| 20 | */ |
| 21 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 22 | public class OAuth2HandlerTest { |
| 23 | |
| 24 | private static ClientInfo info; |
| 25 | private static OAuth2Handler handler; |
| 26 | private static EncryptionIface crypto; |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 27 | private static final String SCOPES = "search preferences queries account"; |
| Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 28 | private static User user; |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 29 | |
| 30 | @BeforeClass |
| 31 | public static void setup() throws KustvaktException { |
| Michael Hanl | 6fdba05 | 2016-01-16 19:06:40 +0100 | [diff] [blame] | 32 | BeanConfiguration.loadClasspathContext("default-config.xml"); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 33 | handler = new OAuth2Handler( |
| 34 | BeanConfiguration.getBeans().getPersistenceClient()); |
| 35 | crypto = BeanConfiguration.getBeans().getEncryption(); |
| 36 | info = new ClientInfo(crypto.createID(), crypto.createToken()); |
| 37 | info.setConfidential(true); |
| 38 | //todo: support for subdomains?! |
| 39 | info.setUrl("http://localhost:8080/api/v0.1"); |
| 40 | info.setRedirect_uri("testwebsite/login"); |
| 41 | |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame^] | 42 | TestHelper.setupAccount(); |
| Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 43 | user = TestHelper.getUser(); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 44 | handler.registerClient(info, user); |
| 45 | } |
| 46 | |
| 47 | @AfterClass |
| 48 | public static void drop() throws KustvaktException { |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 49 | assert handler != null; |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 50 | handler.removeClient(info, user); |
| Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 51 | TestHelper.dropUser(); |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 52 | BeanConfiguration.closeApplication(); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void testStoreAuthorizationCodeThrowsNoException() |
| 57 | throws KustvaktException { |
| 58 | String auth_code = crypto.createToken(); |
| 59 | AuthCodeInfo codeInfo = new AuthCodeInfo(info.getClient_id(), |
| 60 | auth_code); |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 61 | codeInfo.setScopes(SCOPES); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 62 | |
| 63 | handler.authorize(codeInfo, user); |
| 64 | codeInfo = handler.getAuthorization(auth_code); |
| 65 | Assert.assertNotNull("client is null!", codeInfo); |
| 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void testAuthorizationCodeRemoveThrowsNoException() |
| 70 | throws KustvaktException { |
| 71 | String auth_code = crypto.createToken(); |
| 72 | AuthCodeInfo codeInfo = new AuthCodeInfo(info.getClient_id(), |
| 73 | auth_code); |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 74 | codeInfo.setScopes(SCOPES); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 75 | |
| 76 | handler.authorize(codeInfo, user); |
| 77 | String t = crypto.createToken(); |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 78 | String refresh = crypto.createToken(); |
| 79 | handler.addToken(codeInfo.getCode(), t, refresh, 7200); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 80 | |
| 81 | TokenContext ctx = handler.getContext(t); |
| 82 | Assert.assertNotNull("context is null", ctx); |
| 83 | |
| 84 | AuthCodeInfo c2 = handler.getAuthorization(codeInfo.getCode()); |
| 85 | Assert.assertNull("clearing authorization failed", c2); |
| 86 | } |
| 87 | |
| 88 | @Test |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 89 | public void testTokenEndpointRedirect() { |
| 90 | |
| 91 | } |
| 92 | |
| 93 | @Test |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 94 | public void testStoreAccessCodeViaAuthCodeThrowsNoException() { |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 95 | String auth_code = crypto.createToken(); |
| 96 | AuthCodeInfo codeInfo = new AuthCodeInfo(info.getClient_id(), |
| 97 | auth_code); |
| 98 | codeInfo.setScopes(SCOPES); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 99 | |
| 100 | } |
| 101 | |
| 102 | @Test |
| 103 | public void testDeleteAccessCodesByUserDeleteCascade() { |
| 104 | |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void testAccessTokenbyUserDeleteCascade() { |
| 109 | |
| 110 | } |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 111 | |
| 112 | @Test |
| 113 | public void testRefreshToken() { |
| 114 | |
| 115 | } |
| 116 | |
| 117 | // fixme: exception thrown?! |
| 118 | @Test |
| 119 | public void testAccessTokenExpired() { |
| 120 | |
| 121 | } |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 122 | } |