1
2
3
4
5
6
7
8
9
10
11
12
13
14 package sparklr.common;
15
16 import javax.sql.DataSource;
17
18 import org.junit.Before;
19 import org.junit.Rule;
20 import org.junit.runner.RunWith;
21 import org.springframework.aop.framework.Advised;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.beans.factory.annotation.Value;
24 import org.springframework.boot.autoconfigure.security.SecurityProperties;
25 import org.springframework.boot.autoconfigure.web.ServerProperties;
26 import org.springframework.boot.test.IntegrationTest;
27 import org.springframework.jdbc.core.JdbcTemplate;
28 import org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails;
29 import org.springframework.security.oauth2.client.test.BeforeOAuth2Context;
30 import org.springframework.security.oauth2.client.test.OAuth2ContextSetup;
31 import org.springframework.security.oauth2.client.token.grant.implicit.ImplicitResourceDetails;
32 import org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails;
33 import org.springframework.security.oauth2.client.token.grant.redirect.AbstractRedirectResourceDetails;
34 import org.springframework.security.oauth2.provider.approval.ApprovalStore;
35 import org.springframework.security.oauth2.provider.approval.InMemoryApprovalStore;
36 import org.springframework.security.oauth2.provider.approval.JdbcApprovalStore;
37 import org.springframework.security.oauth2.provider.token.TokenStore;
38 import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
39 import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
40 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
41 import org.springframework.test.context.web.WebAppConfiguration;
42
43 @RunWith(SpringJUnit4ClassRunner.class)
44 @WebAppConfiguration
45 @IntegrationTest("server.port=0")
46 public abstract class AbstractIntegrationTests {
47
48 @Value("${local.server.port}")
49 private int port;
50
51 private static String globalTokenPath;
52
53 private static String globalAuthorizePath;
54
55 @Rule
56 public HttpTestUtils http = HttpTestUtils.standard();
57
58 @Rule
59 public OAuth2ContextSetup context = OAuth2ContextSetup.standard(http);
60
61 @Autowired
62 private ServerProperties server;
63
64 @Autowired
65 protected SecurityProperties security;
66
67 @Autowired(required=false)
68 private TokenStore tokenStore;
69
70 @Autowired(required=false)
71 private ApprovalStore approvalStore;
72
73 @Autowired(required=false)
74 private DataSource dataSource;
75
76 @Before
77 public void init() throws Exception {
78 http.setPort(port);
79 clear(tokenStore);
80 clear(approvalStore);
81 }
82
83 @BeforeOAuth2Context
84 public void fixPaths() {
85 String prefix = server.getServletPrefix();
86 http.setPort(port);
87 http.setPrefix(prefix);
88 BaseOAuth2ProtectedResourceDetails resource = (BaseOAuth2ProtectedResourceDetails) context.getResource();
89 resource.setAccessTokenUri(http.getUrl(tokenPath()));
90 if (resource instanceof AbstractRedirectResourceDetails) {
91 ((AbstractRedirectResourceDetails) resource).setUserAuthorizationUri(http.getUrl(authorizePath()));
92 }
93 if (resource instanceof ImplicitResourceDetails) {
94 resource.setAccessTokenUri(http.getUrl(authorizePath()));
95 }
96 if (resource instanceof ResourceOwnerPasswordResourceDetails) {
97 ((ResourceOwnerPasswordResourceDetails) resource).setUsername(security.getUser().getName());
98 ((ResourceOwnerPasswordResourceDetails) resource).setPassword(security.getUser().getPassword());
99 }
100 }
101
102 private void clear(ApprovalStore approvalStore) throws Exception {
103 if (approvalStore instanceof Advised) {
104 Advised advised = (Advised) tokenStore;
105 ApprovalStore target = (ApprovalStore) advised.getTargetSource().getTarget();
106 clear(target);
107 return;
108 }
109 if (approvalStore instanceof InMemoryApprovalStore) {
110 ((InMemoryApprovalStore) approvalStore).clear();
111 }
112 if (approvalStore instanceof JdbcApprovalStore) {
113 JdbcTemplate template = new JdbcTemplate(dataSource);
114 template.execute("delete from oauth_approvals");
115 }
116 }
117
118 private void clear(TokenStore tokenStore) throws Exception {
119 if (tokenStore instanceof Advised) {
120 Advised advised = (Advised) tokenStore;
121 TokenStore target = (TokenStore) advised.getTargetSource().getTarget();
122 clear(target);
123 return;
124 }
125 if (tokenStore instanceof InMemoryTokenStore) {
126 ((InMemoryTokenStore) tokenStore).clear();
127 }
128 if (tokenStore instanceof JdbcTokenStore) {
129 JdbcTemplate template = new JdbcTemplate(dataSource);
130 template.execute("delete from oauth_access_token");
131 template.execute("delete from oauth_refresh_token");
132 template.execute("delete from oauth_client_token");
133 template.execute("delete from oauth_code");
134 }
135 }
136
137 @Value("${oauth.paths.token:/oauth/token}")
138 public void setTokenPath(String tokenPath) {
139 globalTokenPath = tokenPath;
140 }
141
142 @Value("${oauth.paths.authorize:/oauth/authorize}")
143 public void setAuthorizePath(String authorizePath) {
144 globalAuthorizePath = authorizePath;
145 }
146
147 public static String tokenPath() {
148 return globalTokenPath;
149 }
150
151 public static String authorizePath() {
152 return globalAuthorizePath;
153 }
154
155 }