|
|
|
@ -38,6 +38,7 @@ import java.util.stream.Collectors; |
|
|
|
import java.util.stream.Stream; |
|
|
|
|
|
|
|
import static org.thingsboard.server.dao.oauth2.OAuth2Utils.ALLOW_OAUTH2_CONFIGURATION; |
|
|
|
import static org.thingsboard.server.dao.oauth2.OAuth2Utils.toClientRegistrations; |
|
|
|
|
|
|
|
public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
|
|
|
|
|
|
|
@ -90,9 +91,13 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
|
|
|
@Test |
|
|
|
public void testCreateNewSystemParams() { |
|
|
|
OAuth2ClientRegistration clientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID); |
|
|
|
OAuth2ClientRegistration savedClientRegistration = oAuth2Service.saveClientRegistration(clientRegistration); |
|
|
|
OAuth2ClientsParams savedClientsParams = oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(clientRegistration))); |
|
|
|
Assert.assertNotNull(savedClientsParams); |
|
|
|
|
|
|
|
Assert.assertNotNull(savedClientRegistration); |
|
|
|
List<OAuth2ClientRegistration> savedClientRegistrations = OAuth2Utils.toClientRegistrations(TenantId.SYS_TENANT_ID, savedClientsParams); |
|
|
|
Assert.assertEquals(1, savedClientRegistrations.size()); |
|
|
|
|
|
|
|
OAuth2ClientRegistration savedClientRegistration = savedClientRegistrations.get(0); |
|
|
|
Assert.assertNotNull(savedClientRegistration.getId()); |
|
|
|
clientRegistration.setId(savedClientRegistration.getId()); |
|
|
|
clientRegistration.setCreatedTime(savedClientRegistration.getCreatedTime()); |
|
|
|
@ -102,12 +107,14 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
|
|
|
@Test |
|
|
|
public void testFindSystemParamsByTenant() { |
|
|
|
OAuth2ClientRegistration clientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID); |
|
|
|
oAuth2Service.saveClientRegistration(clientRegistration); |
|
|
|
oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(clientRegistration))); |
|
|
|
|
|
|
|
List<OAuth2ClientRegistration> clientRegistrationsByTenantId = oAuth2Service.findClientRegistrationsByTenantId(TenantId.SYS_TENANT_ID); |
|
|
|
Assert.assertEquals(1, clientRegistrationsByTenantId.size()); |
|
|
|
OAuth2ClientsParams foundClientsParams = oAuth2Service.findClientsParamsByTenantId(TenantId.SYS_TENANT_ID); |
|
|
|
Assert.assertEquals(1, foundClientsParams.getOAuth2DomainDtos().size()); |
|
|
|
Assert.assertEquals(1, oAuth2Service.findAllClientRegistrations().size()); |
|
|
|
OAuth2ClientRegistration foundClientRegistration = clientRegistrationsByTenantId.get(0); |
|
|
|
|
|
|
|
List<OAuth2ClientRegistration> foundClientRegistrations = OAuth2Utils.toClientRegistrations(TenantId.SYS_TENANT_ID, foundClientsParams); |
|
|
|
OAuth2ClientRegistration foundClientRegistration = foundClientRegistrations.get(0); |
|
|
|
Assert.assertNotNull(foundClientRegistration); |
|
|
|
clientRegistration.setId(foundClientRegistration.getId()); |
|
|
|
clientRegistration.setCreatedTime(foundClientRegistration.getCreatedTime()); |
|
|
|
@ -117,7 +124,13 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
|
|
|
@Test |
|
|
|
public void testCreateNewTenantParams() { |
|
|
|
OAuth2ClientRegistration clientRegistration = validClientRegistration(tenantId); |
|
|
|
OAuth2ClientRegistration savedClientRegistration = oAuth2Service.saveClientRegistration(clientRegistration); |
|
|
|
OAuth2ClientsParams savedClientsParams = oAuth2Service.saveClientsParams(tenantId, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(clientRegistration))); |
|
|
|
Assert.assertNotNull(savedClientsParams); |
|
|
|
|
|
|
|
List<OAuth2ClientRegistration> savedClientRegistrations = OAuth2Utils.toClientRegistrations(tenantId, savedClientsParams); |
|
|
|
Assert.assertEquals(1, savedClientRegistrations.size()); |
|
|
|
|
|
|
|
OAuth2ClientRegistration savedClientRegistration = savedClientRegistrations.get(0); |
|
|
|
|
|
|
|
Assert.assertNotNull(savedClientRegistration); |
|
|
|
Assert.assertNotNull(savedClientRegistration.getId()); |
|
|
|
@ -129,12 +142,15 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
|
|
|
@Test |
|
|
|
public void testFindTenantParams() { |
|
|
|
OAuth2ClientRegistration clientRegistration = validClientRegistration(tenantId); |
|
|
|
oAuth2Service.saveClientRegistration(clientRegistration); |
|
|
|
oAuth2Service.saveClientsParams(tenantId, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(clientRegistration))); |
|
|
|
|
|
|
|
List<OAuth2ClientRegistration> clientRegistrationsByTenantId = oAuth2Service.findClientRegistrationsByTenantId(tenantId); |
|
|
|
Assert.assertEquals(1, clientRegistrationsByTenantId.size()); |
|
|
|
OAuth2ClientsParams foundClientsParams = oAuth2Service.findClientsParamsByTenantId(tenantId); |
|
|
|
Assert.assertEquals(1, foundClientsParams.getOAuth2DomainDtos().size()); |
|
|
|
Assert.assertEquals(1, oAuth2Service.findAllClientRegistrations().size()); |
|
|
|
OAuth2ClientRegistration foundClientRegistration = clientRegistrationsByTenantId.get(0); |
|
|
|
|
|
|
|
List<OAuth2ClientRegistration> foundClientRegistrations = OAuth2Utils.toClientRegistrations(tenantId, foundClientsParams); |
|
|
|
OAuth2ClientRegistration foundClientRegistration = foundClientRegistrations.get(0); |
|
|
|
|
|
|
|
Assert.assertNotNull(foundClientRegistration); |
|
|
|
clientRegistration.setId(foundClientRegistration.getId()); |
|
|
|
clientRegistration.setCreatedTime(foundClientRegistration.getCreatedTime()); |
|
|
|
@ -146,18 +162,20 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
|
|
|
OAuth2ClientRegistration tenantClientRegistration = validClientRegistration(tenantId); |
|
|
|
OAuth2ClientRegistration sysAdminClientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID); |
|
|
|
|
|
|
|
OAuth2ClientRegistration savedTenantClientRegistration = oAuth2Service.saveClientRegistration(tenantClientRegistration); |
|
|
|
OAuth2ClientRegistration savedSysAdminClientRegistration = oAuth2Service.saveClientRegistration(sysAdminClientRegistration); |
|
|
|
OAuth2ClientsParams savedTenantClientsParams = oAuth2Service.saveClientsParams(tenantId, |
|
|
|
OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(tenantClientRegistration))); |
|
|
|
OAuth2ClientsParams savedSysAdminClientsParams = oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID, |
|
|
|
OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(sysAdminClientRegistration))); |
|
|
|
|
|
|
|
Assert.assertEquals(2, oAuth2Service.findAllClientRegistrations().size()); |
|
|
|
|
|
|
|
Assert.assertEquals(savedTenantClientRegistration, oAuth2Service.findClientRegistrationsByTenantId(tenantId).get(0)); |
|
|
|
Assert.assertEquals(savedSysAdminClientRegistration, oAuth2Service.findClientRegistrationsByTenantId(TenantId.SYS_TENANT_ID).get(0)); |
|
|
|
Assert.assertEquals(savedTenantClientsParams, oAuth2Service.findClientsParamsByTenantId(tenantId)); |
|
|
|
Assert.assertEquals(savedSysAdminClientsParams, oAuth2Service.findClientsParamsByTenantId(TenantId.SYS_TENANT_ID)); |
|
|
|
|
|
|
|
Assert.assertEquals(savedTenantClientRegistration, |
|
|
|
oAuth2Service.findClientRegistration(savedTenantClientRegistration.getUuidId())); |
|
|
|
Assert.assertEquals(savedSysAdminClientRegistration, |
|
|
|
oAuth2Service.findClientRegistration(savedSysAdminClientRegistration.getUuidId())); |
|
|
|
OAuth2ClientRegistration savedTenantClientRegistration = toClientRegistrations(tenantId, savedTenantClientsParams).get(0); |
|
|
|
Assert.assertEquals(savedTenantClientRegistration, oAuth2Service.findClientRegistration(savedTenantClientRegistration.getUuidId())); |
|
|
|
OAuth2ClientRegistration savedSysAdminClientRegistration = toClientRegistrations(TenantId.SYS_TENANT_ID, savedSysAdminClientsParams).get(0); |
|
|
|
Assert.assertEquals(savedSysAdminClientRegistration, oAuth2Service.findClientRegistration(savedSysAdminClientRegistration.getUuidId())); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
@ -166,8 +184,8 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
|
|
|
OAuth2ClientRegistration tenantClientRegistration = validClientRegistration(tenantId, testDomainName); |
|
|
|
OAuth2ClientRegistration sysAdminClientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID, testDomainName); |
|
|
|
|
|
|
|
oAuth2Service.saveClientRegistration(tenantClientRegistration); |
|
|
|
oAuth2Service.saveClientRegistration(sysAdminClientRegistration); |
|
|
|
oAuth2Service.saveClientsParams(tenantId, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(tenantClientRegistration))); |
|
|
|
oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(sysAdminClientRegistration))); |
|
|
|
|
|
|
|
List<OAuth2ClientInfo> oAuth2Clients = oAuth2Service.getOAuth2Clients(testDomainName); |
|
|
|
|
|
|
|
@ -183,8 +201,8 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
|
|
|
String testDomainName = "test_domain"; |
|
|
|
OAuth2ClientRegistration tenantClientRegistration = validClientRegistration(tenantId, testDomainName); |
|
|
|
OAuth2ClientRegistration sysAdminClientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID, testDomainName); |
|
|
|
oAuth2Service.saveClientRegistration(tenantClientRegistration); |
|
|
|
oAuth2Service.saveClientRegistration(sysAdminClientRegistration); |
|
|
|
oAuth2Service.saveClientsParams(tenantId, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(tenantClientRegistration))); |
|
|
|
oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(sysAdminClientRegistration))); |
|
|
|
List<OAuth2ClientInfo> oAuth2Clients = oAuth2Service.getOAuth2Clients("random-domain"); |
|
|
|
Assert.assertTrue(oAuth2Clients.isEmpty()); |
|
|
|
} |
|
|
|
@ -193,8 +211,14 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
|
|
|
public void testDeleteOAuth2ClientRegistration() { |
|
|
|
OAuth2ClientRegistration tenantClientRegistration = validClientRegistration(tenantId); |
|
|
|
OAuth2ClientRegistration sysAdminClientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID); |
|
|
|
OAuth2ClientRegistration savedTenantRegistration = oAuth2Service.saveClientRegistration(tenantClientRegistration); |
|
|
|
OAuth2ClientRegistration savedSysAdminRegistration = oAuth2Service.saveClientRegistration(sysAdminClientRegistration); |
|
|
|
|
|
|
|
OAuth2ClientsParams savedTenantClientsParams = oAuth2Service.saveClientsParams(tenantId, |
|
|
|
OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(tenantClientRegistration))); |
|
|
|
OAuth2ClientsParams savedSysAdminClientsParams = oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID, |
|
|
|
OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(sysAdminClientRegistration))); |
|
|
|
|
|
|
|
OAuth2ClientRegistration savedTenantRegistration = toClientRegistrations(tenantId, savedTenantClientsParams).get(0); |
|
|
|
OAuth2ClientRegistration savedSysAdminRegistration = toClientRegistrations(TenantId.SYS_TENANT_ID, savedSysAdminClientsParams).get(0); |
|
|
|
|
|
|
|
oAuth2Service.deleteClientRegistrationById(tenantId, savedTenantRegistration.getId()); |
|
|
|
List<OAuth2ClientRegistration> foundRegistrations = oAuth2Service.findAllClientRegistrations(); |
|
|
|
@ -204,29 +228,39 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testDeleteTenantOAuth2ClientRegistrations() { |
|
|
|
oAuth2Service.saveClientRegistration(validClientRegistration(tenantId)); |
|
|
|
oAuth2Service.saveClientRegistration(validClientRegistration(tenantId)); |
|
|
|
oAuth2Service.saveClientRegistration(validClientRegistration(tenantId)); |
|
|
|
oAuth2Service.saveClientsParams(tenantId, OAuth2Utils.toOAuth2ClientsParams(Arrays.asList( |
|
|
|
validClientRegistration(tenantId, "domain"), |
|
|
|
validClientRegistration(tenantId, "domain"), |
|
|
|
validClientRegistration(tenantId, "domain") |
|
|
|
))); |
|
|
|
Assert.assertEquals(3, oAuth2Service.findAllClientRegistrations().size()); |
|
|
|
Assert.assertEquals(3, oAuth2Service.findClientRegistrationsByTenantId(tenantId).size()); |
|
|
|
Assert.assertEquals(1, oAuth2Service.findClientsParamsByTenantId(tenantId).getOAuth2DomainDtos().size()); |
|
|
|
|
|
|
|
oAuth2Service.deleteClientRegistrationsByTenantId(tenantId); |
|
|
|
Assert.assertEquals(0, oAuth2Service.findAllClientRegistrations().size()); |
|
|
|
Assert.assertEquals(0, oAuth2Service.findClientRegistrationsByTenantId(tenantId).size()); |
|
|
|
Assert.assertEquals(0, oAuth2Service.findClientsParamsByTenantId(tenantId).getOAuth2DomainDtos().size()); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testDeleteTenantDomainOAuth2ClientRegistrations() { |
|
|
|
oAuth2Service.saveClientRegistration(validClientRegistration(tenantId, "domain1")); |
|
|
|
oAuth2Service.saveClientRegistration(validClientRegistration(tenantId, "domain1")); |
|
|
|
oAuth2Service.saveClientRegistration(validClientRegistration(tenantId, "domain2")); |
|
|
|
oAuth2Service.saveClientRegistration(validClientRegistration(TenantId.SYS_TENANT_ID, "domain2")); |
|
|
|
oAuth2Service.saveClientsParams(tenantId, OAuth2Utils.toOAuth2ClientsParams(Arrays.asList( |
|
|
|
validClientRegistration(tenantId, "domain1"), |
|
|
|
validClientRegistration(tenantId, "domain1"), |
|
|
|
validClientRegistration(tenantId, "domain2") |
|
|
|
))); |
|
|
|
oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toOAuth2ClientsParams(Arrays.asList( |
|
|
|
validClientRegistration(TenantId.SYS_TENANT_ID, "domain2") |
|
|
|
))); |
|
|
|
Assert.assertEquals(4, oAuth2Service.findAllClientRegistrations().size()); |
|
|
|
Assert.assertEquals(3, oAuth2Service.findClientRegistrationsByTenantId(tenantId).size()); |
|
|
|
OAuth2ClientsParams tenantClientsParams = oAuth2Service.findClientsParamsByTenantId(tenantId); |
|
|
|
List<OAuth2ClientRegistration> tenantClientRegistrations = toClientRegistrations(tenantId, tenantClientsParams); |
|
|
|
Assert.assertEquals(2, tenantClientsParams.getOAuth2DomainDtos().size()); |
|
|
|
Assert.assertEquals(3, tenantClientRegistrations.size()); |
|
|
|
|
|
|
|
oAuth2Service.deleteClientRegistrationsByDomain(tenantId, "domain1"); |
|
|
|
Assert.assertEquals(2, oAuth2Service.findAllClientRegistrations().size()); |
|
|
|
Assert.assertEquals(1, oAuth2Service.findClientRegistrationsByTenantId(tenantId).size()); |
|
|
|
Assert.assertEquals(1, oAuth2Service.findClientsParamsByTenantId(tenantId).getOAuth2DomainDtos().size()); |
|
|
|
Assert.assertEquals(1, toClientRegistrations(tenantId, oAuth2Service.findClientsParamsByTenantId(tenantId)).size()); |
|
|
|
} |
|
|
|
|
|
|
|
private void updateTenantAllowOAuth2Setting(Boolean allowOAuth2) throws IOException { |
|
|
|
|