...
|
...
|
@@ -15,27 +15,21 @@ |
15
|
15
|
*/
|
16
|
16
|
package org.thingsboard.server.dao.service;
|
17
|
17
|
|
18
|
|
-import org.apache.commons.lang3.tuple.Pair;
|
19
|
18
|
import org.junit.After;
|
20
|
19
|
import org.junit.Assert;
|
21
|
20
|
import org.junit.Before;
|
22
|
21
|
import org.junit.Test;
|
23
|
22
|
import org.springframework.beans.factory.annotation.Autowired;
|
24
|
|
-import org.thingsboard.server.common.data.DataConstants;
|
25
|
23
|
import org.thingsboard.server.common.data.Tenant;
|
26
|
|
-import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationId;
|
27
|
24
|
import org.thingsboard.server.common.data.id.TenantId;
|
28
|
25
|
import org.thingsboard.server.common.data.oauth2.*;
|
29
|
26
|
import org.thingsboard.server.dao.attributes.AttributesService;
|
30
|
|
-import org.thingsboard.server.dao.exception.DataValidationException;
|
31
|
27
|
import org.thingsboard.server.dao.oauth2.OAuth2Service;
|
32
|
28
|
import org.thingsboard.server.dao.oauth2.OAuth2Utils;
|
33
|
29
|
|
34
|
|
-import javax.transaction.Transactional;
|
35
|
30
|
import java.io.IOException;
|
36
|
31
|
import java.util.*;
|
37
|
32
|
import java.util.stream.Collectors;
|
38
|
|
-import java.util.stream.Stream;
|
39
|
33
|
|
40
|
34
|
import static org.thingsboard.server.dao.oauth2.OAuth2Utils.ALLOW_OAUTH2_CONFIGURATION;
|
41
|
35
|
import static org.thingsboard.server.dao.oauth2.OAuth2Utils.toClientRegistrations;
|
...
|
...
|
@@ -91,10 +85,10 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
91
|
85
|
@Test
|
92
|
86
|
public void testCreateNewSystemParams() {
|
93
|
87
|
OAuth2ClientRegistration clientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID);
|
94
|
|
- OAuth2ClientsParams savedClientsParams = oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(clientRegistration)));
|
95
|
|
- Assert.assertNotNull(savedClientsParams);
|
|
88
|
+ List<OAuth2ClientsDomainParams> savedDomainsParams = oAuth2Service.saveDomainsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toDomainsParams(Collections.singletonList(clientRegistration)));
|
|
89
|
+ Assert.assertNotNull(savedDomainsParams);
|
96
|
90
|
|
97
|
|
- List<OAuth2ClientRegistration> savedClientRegistrations = OAuth2Utils.toClientRegistrations(TenantId.SYS_TENANT_ID, savedClientsParams);
|
|
91
|
+ List<OAuth2ClientRegistration> savedClientRegistrations = OAuth2Utils.toClientRegistrations(TenantId.SYS_TENANT_ID, savedDomainsParams);
|
98
|
92
|
Assert.assertEquals(1, savedClientRegistrations.size());
|
99
|
93
|
|
100
|
94
|
OAuth2ClientRegistration savedClientRegistration = savedClientRegistrations.get(0);
|
...
|
...
|
@@ -107,13 +101,13 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
107
|
101
|
@Test
|
108
|
102
|
public void testFindSystemParamsByTenant() {
|
109
|
103
|
OAuth2ClientRegistration clientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID);
|
110
|
|
- oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(clientRegistration)));
|
|
104
|
+ oAuth2Service.saveDomainsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toDomainsParams(Collections.singletonList(clientRegistration)));
|
111
|
105
|
|
112
|
|
- OAuth2ClientsParams foundClientsParams = oAuth2Service.findClientsParamsByTenantId(TenantId.SYS_TENANT_ID);
|
113
|
|
- Assert.assertEquals(1, foundClientsParams.getOAuth2DomainDtos().size());
|
|
106
|
+ List<OAuth2ClientsDomainParams> foundDomainsParams = oAuth2Service.findDomainsParamsByTenantId(TenantId.SYS_TENANT_ID);
|
|
107
|
+ Assert.assertEquals(1, foundDomainsParams.size());
|
114
|
108
|
Assert.assertEquals(1, oAuth2Service.findAllClientRegistrations().size());
|
115
|
109
|
|
116
|
|
- List<OAuth2ClientRegistration> foundClientRegistrations = OAuth2Utils.toClientRegistrations(TenantId.SYS_TENANT_ID, foundClientsParams);
|
|
110
|
+ List<OAuth2ClientRegistration> foundClientRegistrations = OAuth2Utils.toClientRegistrations(TenantId.SYS_TENANT_ID, foundDomainsParams);
|
117
|
111
|
OAuth2ClientRegistration foundClientRegistration = foundClientRegistrations.get(0);
|
118
|
112
|
Assert.assertNotNull(foundClientRegistration);
|
119
|
113
|
clientRegistration.setId(foundClientRegistration.getId());
|
...
|
...
|
@@ -124,10 +118,10 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
124
|
118
|
@Test
|
125
|
119
|
public void testCreateNewTenantParams() {
|
126
|
120
|
OAuth2ClientRegistration clientRegistration = validClientRegistration(tenantId);
|
127
|
|
- OAuth2ClientsParams savedClientsParams = oAuth2Service.saveClientsParams(tenantId, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(clientRegistration)));
|
128
|
|
- Assert.assertNotNull(savedClientsParams);
|
|
121
|
+ List<OAuth2ClientsDomainParams> savedDomainsParams = oAuth2Service.saveDomainsParams(tenantId, OAuth2Utils.toDomainsParams(Collections.singletonList(clientRegistration)));
|
|
122
|
+ Assert.assertNotNull(savedDomainsParams);
|
129
|
123
|
|
130
|
|
- List<OAuth2ClientRegistration> savedClientRegistrations = OAuth2Utils.toClientRegistrations(tenantId, savedClientsParams);
|
|
124
|
+ List<OAuth2ClientRegistration> savedClientRegistrations = OAuth2Utils.toClientRegistrations(tenantId, savedDomainsParams);
|
131
|
125
|
Assert.assertEquals(1, savedClientRegistrations.size());
|
132
|
126
|
|
133
|
127
|
OAuth2ClientRegistration savedClientRegistration = savedClientRegistrations.get(0);
|
...
|
...
|
@@ -142,13 +136,13 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
142
|
136
|
@Test
|
143
|
137
|
public void testFindTenantParams() {
|
144
|
138
|
OAuth2ClientRegistration clientRegistration = validClientRegistration(tenantId);
|
145
|
|
- oAuth2Service.saveClientsParams(tenantId, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(clientRegistration)));
|
|
139
|
+ oAuth2Service.saveDomainsParams(tenantId, OAuth2Utils.toDomainsParams(Collections.singletonList(clientRegistration)));
|
146
|
140
|
|
147
|
|
- OAuth2ClientsParams foundClientsParams = oAuth2Service.findClientsParamsByTenantId(tenantId);
|
148
|
|
- Assert.assertEquals(1, foundClientsParams.getOAuth2DomainDtos().size());
|
|
141
|
+ List<OAuth2ClientsDomainParams> foundDomainsParams = oAuth2Service.findDomainsParamsByTenantId(tenantId);
|
|
142
|
+ Assert.assertEquals(1, foundDomainsParams.size());
|
149
|
143
|
Assert.assertEquals(1, oAuth2Service.findAllClientRegistrations().size());
|
150
|
144
|
|
151
|
|
- List<OAuth2ClientRegistration> foundClientRegistrations = OAuth2Utils.toClientRegistrations(tenantId, foundClientsParams);
|
|
145
|
+ List<OAuth2ClientRegistration> foundClientRegistrations = OAuth2Utils.toClientRegistrations(tenantId, foundDomainsParams);
|
152
|
146
|
OAuth2ClientRegistration foundClientRegistration = foundClientRegistrations.get(0);
|
153
|
147
|
|
154
|
148
|
Assert.assertNotNull(foundClientRegistration);
|
...
|
...
|
@@ -162,19 +156,19 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
162
|
156
|
OAuth2ClientRegistration tenantClientRegistration = validClientRegistration(tenantId);
|
163
|
157
|
OAuth2ClientRegistration sysAdminClientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID);
|
164
|
158
|
|
165
|
|
- OAuth2ClientsParams savedTenantClientsParams = oAuth2Service.saveClientsParams(tenantId,
|
166
|
|
- OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(tenantClientRegistration)));
|
167
|
|
- OAuth2ClientsParams savedSysAdminClientsParams = oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID,
|
168
|
|
- OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(sysAdminClientRegistration)));
|
|
159
|
+ List<OAuth2ClientsDomainParams> savedTenantDomainsParams = oAuth2Service.saveDomainsParams(tenantId,
|
|
160
|
+ OAuth2Utils.toDomainsParams(Collections.singletonList(tenantClientRegistration)));
|
|
161
|
+ List<OAuth2ClientsDomainParams> savedSysAdminDomainsParams = oAuth2Service.saveDomainsParams(TenantId.SYS_TENANT_ID,
|
|
162
|
+ OAuth2Utils.toDomainsParams(Collections.singletonList(sysAdminClientRegistration)));
|
169
|
163
|
|
170
|
164
|
Assert.assertEquals(2, oAuth2Service.findAllClientRegistrations().size());
|
171
|
165
|
|
172
|
|
- Assert.assertEquals(savedTenantClientsParams, oAuth2Service.findClientsParamsByTenantId(tenantId));
|
173
|
|
- Assert.assertEquals(savedSysAdminClientsParams, oAuth2Service.findClientsParamsByTenantId(TenantId.SYS_TENANT_ID));
|
|
166
|
+ Assert.assertEquals(savedTenantDomainsParams, oAuth2Service.findDomainsParamsByTenantId(tenantId));
|
|
167
|
+ Assert.assertEquals(savedSysAdminDomainsParams, oAuth2Service.findDomainsParamsByTenantId(TenantId.SYS_TENANT_ID));
|
174
|
168
|
|
175
|
|
- OAuth2ClientRegistration savedTenantClientRegistration = toClientRegistrations(tenantId, savedTenantClientsParams).get(0);
|
|
169
|
+ OAuth2ClientRegistration savedTenantClientRegistration = toClientRegistrations(tenantId, savedTenantDomainsParams).get(0);
|
176
|
170
|
Assert.assertEquals(savedTenantClientRegistration, oAuth2Service.findClientRegistration(savedTenantClientRegistration.getUuidId()));
|
177
|
|
- OAuth2ClientRegistration savedSysAdminClientRegistration = toClientRegistrations(TenantId.SYS_TENANT_ID, savedSysAdminClientsParams).get(0);
|
|
171
|
+ OAuth2ClientRegistration savedSysAdminClientRegistration = toClientRegistrations(TenantId.SYS_TENANT_ID, savedSysAdminDomainsParams).get(0);
|
178
|
172
|
Assert.assertEquals(savedSysAdminClientRegistration, oAuth2Service.findClientRegistration(savedSysAdminClientRegistration.getUuidId()));
|
179
|
173
|
}
|
180
|
174
|
|
...
|
...
|
@@ -184,8 +178,8 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
184
|
178
|
OAuth2ClientRegistration tenantClientRegistration = validClientRegistration(tenantId, testDomainName);
|
185
|
179
|
OAuth2ClientRegistration sysAdminClientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID, testDomainName);
|
186
|
180
|
|
187
|
|
- oAuth2Service.saveClientsParams(tenantId, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(tenantClientRegistration)));
|
188
|
|
- oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(sysAdminClientRegistration)));
|
|
181
|
+ oAuth2Service.saveDomainsParams(tenantId, OAuth2Utils.toDomainsParams(Collections.singletonList(tenantClientRegistration)));
|
|
182
|
+ oAuth2Service.saveDomainsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toDomainsParams(Collections.singletonList(sysAdminClientRegistration)));
|
189
|
183
|
|
190
|
184
|
List<OAuth2ClientInfo> oAuth2Clients = oAuth2Service.getOAuth2Clients(testDomainName);
|
191
|
185
|
|
...
|
...
|
@@ -201,8 +195,8 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
201
|
195
|
String testDomainName = "test_domain";
|
202
|
196
|
OAuth2ClientRegistration tenantClientRegistration = validClientRegistration(tenantId, testDomainName);
|
203
|
197
|
OAuth2ClientRegistration sysAdminClientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID, testDomainName);
|
204
|
|
- oAuth2Service.saveClientsParams(tenantId, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(tenantClientRegistration)));
|
205
|
|
- oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(sysAdminClientRegistration)));
|
|
198
|
+ oAuth2Service.saveDomainsParams(tenantId, OAuth2Utils.toDomainsParams(Collections.singletonList(tenantClientRegistration)));
|
|
199
|
+ oAuth2Service.saveDomainsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toDomainsParams(Collections.singletonList(sysAdminClientRegistration)));
|
206
|
200
|
List<OAuth2ClientInfo> oAuth2Clients = oAuth2Service.getOAuth2Clients("random-domain");
|
207
|
201
|
Assert.assertTrue(oAuth2Clients.isEmpty());
|
208
|
202
|
}
|
...
|
...
|
@@ -212,13 +206,13 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
212
|
206
|
OAuth2ClientRegistration tenantClientRegistration = validClientRegistration(tenantId);
|
213
|
207
|
OAuth2ClientRegistration sysAdminClientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID);
|
214
|
208
|
|
215
|
|
- OAuth2ClientsParams savedTenantClientsParams = oAuth2Service.saveClientsParams(tenantId,
|
216
|
|
- OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(tenantClientRegistration)));
|
217
|
|
- OAuth2ClientsParams savedSysAdminClientsParams = oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID,
|
218
|
|
- OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(sysAdminClientRegistration)));
|
|
209
|
+ List<OAuth2ClientsDomainParams> savedTenantDomainsParams = oAuth2Service.saveDomainsParams(tenantId,
|
|
210
|
+ OAuth2Utils.toDomainsParams(Collections.singletonList(tenantClientRegistration)));
|
|
211
|
+ List<OAuth2ClientsDomainParams> savedSysAdminDomainsParams = oAuth2Service.saveDomainsParams(TenantId.SYS_TENANT_ID,
|
|
212
|
+ OAuth2Utils.toDomainsParams(Collections.singletonList(sysAdminClientRegistration)));
|
219
|
213
|
|
220
|
|
- OAuth2ClientRegistration savedTenantRegistration = toClientRegistrations(tenantId, savedTenantClientsParams).get(0);
|
221
|
|
- OAuth2ClientRegistration savedSysAdminRegistration = toClientRegistrations(TenantId.SYS_TENANT_ID, savedSysAdminClientsParams).get(0);
|
|
214
|
+ OAuth2ClientRegistration savedTenantRegistration = toClientRegistrations(tenantId, savedTenantDomainsParams).get(0);
|
|
215
|
+ OAuth2ClientRegistration savedSysAdminRegistration = toClientRegistrations(TenantId.SYS_TENANT_ID, savedSysAdminDomainsParams).get(0);
|
222
|
216
|
|
223
|
217
|
oAuth2Service.deleteClientRegistrationById(tenantId, savedTenantRegistration.getId());
|
224
|
218
|
List<OAuth2ClientRegistration> foundRegistrations = oAuth2Service.findAllClientRegistrations();
|
...
|
...
|
@@ -228,39 +222,39 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
228
|
222
|
|
229
|
223
|
@Test
|
230
|
224
|
public void testDeleteTenantOAuth2ClientRegistrations() {
|
231
|
|
- oAuth2Service.saveClientsParams(tenantId, OAuth2Utils.toOAuth2ClientsParams(Arrays.asList(
|
|
225
|
+ oAuth2Service.saveDomainsParams(tenantId, OAuth2Utils.toDomainsParams(Arrays.asList(
|
232
|
226
|
validClientRegistration(tenantId, "domain"),
|
233
|
227
|
validClientRegistration(tenantId, "domain"),
|
234
|
228
|
validClientRegistration(tenantId, "domain")
|
235
|
229
|
)));
|
236
|
230
|
Assert.assertEquals(3, oAuth2Service.findAllClientRegistrations().size());
|
237
|
|
- Assert.assertEquals(1, oAuth2Service.findClientsParamsByTenantId(tenantId).getOAuth2DomainDtos().size());
|
|
231
|
+ Assert.assertEquals(1, oAuth2Service.findDomainsParamsByTenantId(tenantId).size());
|
238
|
232
|
|
239
|
233
|
oAuth2Service.deleteClientRegistrationsByTenantId(tenantId);
|
240
|
234
|
Assert.assertEquals(0, oAuth2Service.findAllClientRegistrations().size());
|
241
|
|
- Assert.assertEquals(0, oAuth2Service.findClientsParamsByTenantId(tenantId).getOAuth2DomainDtos().size());
|
|
235
|
+ Assert.assertEquals(0, oAuth2Service.findDomainsParamsByTenantId(tenantId).size());
|
242
|
236
|
}
|
243
|
237
|
|
244
|
238
|
@Test
|
245
|
239
|
public void testDeleteTenantDomainOAuth2ClientRegistrations() {
|
246
|
|
- oAuth2Service.saveClientsParams(tenantId, OAuth2Utils.toOAuth2ClientsParams(Arrays.asList(
|
|
240
|
+ oAuth2Service.saveDomainsParams(tenantId, OAuth2Utils.toDomainsParams(Arrays.asList(
|
247
|
241
|
validClientRegistration(tenantId, "domain1"),
|
248
|
242
|
validClientRegistration(tenantId, "domain1"),
|
249
|
243
|
validClientRegistration(tenantId, "domain2")
|
250
|
244
|
)));
|
251
|
|
- oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toOAuth2ClientsParams(Arrays.asList(
|
|
245
|
+ oAuth2Service.saveDomainsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toDomainsParams(Arrays.asList(
|
252
|
246
|
validClientRegistration(TenantId.SYS_TENANT_ID, "domain2")
|
253
|
247
|
)));
|
254
|
248
|
Assert.assertEquals(4, oAuth2Service.findAllClientRegistrations().size());
|
255
|
|
- OAuth2ClientsParams tenantClientsParams = oAuth2Service.findClientsParamsByTenantId(tenantId);
|
256
|
|
- List<OAuth2ClientRegistration> tenantClientRegistrations = toClientRegistrations(tenantId, tenantClientsParams);
|
257
|
|
- Assert.assertEquals(2, tenantClientsParams.getOAuth2DomainDtos().size());
|
|
249
|
+ List<OAuth2ClientsDomainParams> tenantDomainsParams = oAuth2Service.findDomainsParamsByTenantId(tenantId);
|
|
250
|
+ List<OAuth2ClientRegistration> tenantClientRegistrations = toClientRegistrations(tenantId, tenantDomainsParams);
|
|
251
|
+ Assert.assertEquals(2, tenantDomainsParams.size());
|
258
|
252
|
Assert.assertEquals(3, tenantClientRegistrations.size());
|
259
|
253
|
|
260
|
254
|
oAuth2Service.deleteClientRegistrationsByDomain(tenantId, "domain1");
|
261
|
255
|
Assert.assertEquals(2, oAuth2Service.findAllClientRegistrations().size());
|
262
|
|
- Assert.assertEquals(1, oAuth2Service.findClientsParamsByTenantId(tenantId).getOAuth2DomainDtos().size());
|
263
|
|
- Assert.assertEquals(1, toClientRegistrations(tenantId, oAuth2Service.findClientsParamsByTenantId(tenantId)).size());
|
|
256
|
+ Assert.assertEquals(1, oAuth2Service.findDomainsParamsByTenantId(tenantId).size());
|
|
257
|
+ Assert.assertEquals(1, toClientRegistrations(tenantId, oAuth2Service.findDomainsParamsByTenantId(tenantId)).size());
|
264
|
258
|
}
|
265
|
259
|
|
266
|
260
|
private void updateTenantAllowOAuth2Setting(Boolean allowOAuth2) throws IOException {
|
...
|
...
|
|