blob: 4cf5ecba74dc637c15f1996c4e02c8be860ae901 [file] [log] [blame]
Michael Hanle25dea22015-09-24 19:37:56 +02001package de.ids_mannheim.korap.web.service;
2
3import de.ids_mannheim.korap.config.AuthCodeInfo;
4import de.ids_mannheim.korap.config.BeanConfiguration;
5import de.ids_mannheim.korap.config.ClientInfo;
Michael Hanl25aac542016-02-01 18:16:44 +01006import de.ids_mannheim.korap.config.TestHelper;
Michael Hanle25dea22015-09-24 19:37:56 +02007import de.ids_mannheim.korap.exceptions.KustvaktException;
Michael Hanl482f30d2015-09-25 12:39:46 +02008import de.ids_mannheim.korap.handlers.OAuth2Handler;
Michael Hanle25dea22015-09-24 19:37:56 +02009import de.ids_mannheim.korap.interfaces.EncryptionIface;
Michael Hanl482f30d2015-09-25 12:39:46 +020010import de.ids_mannheim.korap.user.TokenContext;
11import de.ids_mannheim.korap.user.User;
Michael Hanle25dea22015-09-24 19:37:56 +020012import org.junit.AfterClass;
13import org.junit.Assert;
14import org.junit.BeforeClass;
15import org.junit.Test;
16
17/**
18 * @author hanl
19 * @date 13/05/2015
20 */
21
Michael Hanle25dea22015-09-24 19:37:56 +020022public class OAuth2HandlerTest {
23
24 private static ClientInfo info;
25 private static OAuth2Handler handler;
26 private static EncryptionIface crypto;
Michael Hanl482f30d2015-09-25 12:39:46 +020027 private static final String SCOPES = "search preferences queries account";
Michael Hanl25aac542016-02-01 18:16:44 +010028 private static User user;
Michael Hanle25dea22015-09-24 19:37:56 +020029
30 @BeforeClass
31 public static void setup() throws KustvaktException {
Michael Hanl6fdba052016-01-16 19:06:40 +010032 BeanConfiguration.loadClasspathContext("default-config.xml");
Michael Hanle25dea22015-09-24 19:37:56 +020033 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 Hanl7368aa42016-02-05 18:15:47 +010042 TestHelper.setupAccount();
Michael Hanl25aac542016-02-01 18:16:44 +010043 user = TestHelper.getUser();
Michael Hanle25dea22015-09-24 19:37:56 +020044 handler.registerClient(info, user);
45 }
46
47 @AfterClass
48 public static void drop() throws KustvaktException {
Michael Hanlf21773f2015-10-16 23:02:31 +020049 assert handler != null;
Michael Hanle25dea22015-09-24 19:37:56 +020050 handler.removeClient(info, user);
Michael Hanl25aac542016-02-01 18:16:44 +010051 TestHelper.dropUser();
Michael Hanlf21773f2015-10-16 23:02:31 +020052 BeanConfiguration.closeApplication();
Michael Hanle25dea22015-09-24 19:37:56 +020053 }
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 Hanl482f30d2015-09-25 12:39:46 +020061 codeInfo.setScopes(SCOPES);
Michael Hanle25dea22015-09-24 19:37:56 +020062
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 Hanl482f30d2015-09-25 12:39:46 +020074 codeInfo.setScopes(SCOPES);
Michael Hanle25dea22015-09-24 19:37:56 +020075
76 handler.authorize(codeInfo, user);
77 String t = crypto.createToken();
Michael Hanl482f30d2015-09-25 12:39:46 +020078 String refresh = crypto.createToken();
79 handler.addToken(codeInfo.getCode(), t, refresh, 7200);
Michael Hanle25dea22015-09-24 19:37:56 +020080
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 Hanl482f30d2015-09-25 12:39:46 +020089 public void testTokenEndpointRedirect() {
90
91 }
92
93 @Test
Michael Hanle25dea22015-09-24 19:37:56 +020094 public void testStoreAccessCodeViaAuthCodeThrowsNoException() {
Michael Hanl482f30d2015-09-25 12:39:46 +020095 String auth_code = crypto.createToken();
96 AuthCodeInfo codeInfo = new AuthCodeInfo(info.getClient_id(),
97 auth_code);
98 codeInfo.setScopes(SCOPES);
Michael Hanle25dea22015-09-24 19:37:56 +020099
100 }
101
102 @Test
103 public void testDeleteAccessCodesByUserDeleteCascade() {
104
105 }
106
107 @Test
108 public void testAccessTokenbyUserDeleteCascade() {
109
110 }
Michael Hanl482f30d2015-09-25 12:39:46 +0200111
112 @Test
113 public void testRefreshToken() {
114
115 }
116
117 // fixme: exception thrown?!
118 @Test
119 public void testAccessTokenExpired() {
120
121 }
Michael Hanle25dea22015-09-24 19:37:56 +0200122}