379 changed files with 8059 additions and 93042 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,57 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
package org.thingsboard.server.config; |
|||
|
|||
import org.junit.jupiter.api.Test; |
|||
import org.junit.jupiter.api.extension.ExtendWith; |
|||
import org.mockito.InjectMocks; |
|||
import org.mockito.Mock; |
|||
import org.mockito.junit.jupiter.MockitoExtension; |
|||
import org.springframework.mock.web.MockHttpServletRequest; |
|||
import org.springframework.security.oauth2.client.registration.ClientRegistration; |
|||
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; |
|||
import org.springframework.security.oauth2.core.AuthorizationGrantType; |
|||
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; |
|||
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
|||
import org.thingsboard.server.dao.oauth2.OAuth2ClientService; |
|||
import org.thingsboard.server.service.security.model.token.OAuth2AppTokenFactory; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
import static org.mockito.BDDMockito.given; |
|||
import static org.mockito.Mockito.verifyNoInteractions; |
|||
|
|||
@ExtendWith(MockitoExtension.class) |
|||
class CustomOAuth2AuthorizationRequestResolverTest { |
|||
|
|||
@Mock |
|||
private ClientRegistrationRepository clientRegistrationRepository; |
|||
@Mock |
|||
private OAuth2ClientService oAuth2ClientService; |
|||
@Mock |
|||
private OAuth2AppTokenFactory oAuth2AppTokenFactory; |
|||
@InjectMocks |
|||
private CustomOAuth2AuthorizationRequestResolver resolver; |
|||
|
|||
@Test |
|||
void resolvesTheRegistrationIdFromTheAuthorizationPath() { |
|||
given(clientRegistrationRepository.findByRegistrationId("abc-123")).willReturn(ClientRegistration.withRegistrationId("abc-123") |
|||
.clientId("client") |
|||
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) |
|||
.redirectUri("{baseUrl}/login/oauth2/code/") |
|||
.authorizationUri("https://idp.example.com/authorize") |
|||
.tokenUri("https://idp.example.com/token") |
|||
.build()); |
|||
|
|||
OAuth2AuthorizationRequest request = resolver.resolve(new MockHttpServletRequest("GET", "/oauth2/authorization/abc-123")); |
|||
|
|||
assertThat(request.getAttributes()).containsEntry(OAuth2ParameterNames.REGISTRATION_ID, "abc-123"); |
|||
assertThat(request.getAuthorizationUri()).isEqualTo("https://idp.example.com/authorize"); |
|||
} |
|||
|
|||
@Test |
|||
void ignoresDeeperPaths() { |
|||
assertThat(resolver.resolve(new MockHttpServletRequest("GET", "/oauth2/authorization/abc/extra"))).isNull(); |
|||
verifyNoInteractions(clientRegistrationRepository); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,40 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
package org.thingsboard.server.config; |
|||
|
|||
import com.fasterxml.jackson.databind.DeserializationFeature; |
|||
import com.fasterxml.jackson.databind.ObjectMapper; |
|||
import org.junit.Test; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.http.converter.HttpMessageConverter; |
|||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; |
|||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; |
|||
import org.thingsboard.server.controller.AbstractControllerTest; |
|||
import org.thingsboard.server.dao.service.DaoSqlTest; |
|||
|
|||
import java.util.List; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
|
|||
@DaoSqlTest |
|||
public class JsonMapperWiringTest extends AbstractControllerTest { |
|||
|
|||
@Autowired |
|||
private RequestMappingHandlerAdapter handlerAdapter; |
|||
|
|||
@Autowired |
|||
private ObjectMapper objectMapper; |
|||
|
|||
@Test |
|||
public void mvcUsesJackson2() { |
|||
List<HttpMessageConverter<?>> converters = handlerAdapter.getMessageConverters(); |
|||
assertThat(converters).noneMatch(c -> c.getClass().getName().startsWith("org.springframework.http.converter.json.JacksonJson")); |
|||
MappingJackson2HttpMessageConverter json = converters.stream() |
|||
.filter(MappingJackson2HttpMessageConverter.class::isInstance) |
|||
.map(MappingJackson2HttpMessageConverter.class::cast) |
|||
.findFirst().orElseThrow(); |
|||
assertThat(json.getObjectMapper()).isSameAs(objectMapper); |
|||
assertThat(objectMapper.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)).isFalse(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
package org.thingsboard.server.config; |
|||
|
|||
import org.junit.jupiter.api.Test; |
|||
import org.springframework.boot.env.YamlPropertySourceLoader; |
|||
import org.springframework.core.env.PropertySource; |
|||
import org.springframework.core.env.StandardEnvironment; |
|||
import org.springframework.core.io.ClassPathResource; |
|||
|
|||
import java.io.IOException; |
|||
import java.util.List; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
|
|||
class ThingsboardYmlDefaultsTest { |
|||
|
|||
@Test |
|||
void tomcatMaxParameterCountDefaultsTo10000() throws IOException { |
|||
List<PropertySource<?>> sources = new YamlPropertySourceLoader().load("thingsboard", new ClassPathResource("thingsboard.yml")); |
|||
StandardEnvironment environment = new StandardEnvironment(); |
|||
sources.forEach(environment.getPropertySources()::addLast); |
|||
assertThat(environment.getProperty("server.tomcat.max-parameter-count", Integer.class)).isEqualTo(10000); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,195 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
package org.thingsboard.server.controller; |
|||
|
|||
import org.junit.Before; |
|||
import org.junit.Test; |
|||
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; |
|||
import org.springframework.test.web.servlet.ResultActions; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.rule.engine.api.DashboardReportService; |
|||
import org.thingsboard.server.common.data.Dashboard; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.User; |
|||
import org.thingsboard.server.common.data.dashboardreport.DashboardReportConfig; |
|||
import org.thingsboard.server.common.data.dashboardreport.DashboardReportData; |
|||
import org.thingsboard.server.common.data.group.EntityGroup; |
|||
import org.thingsboard.server.common.data.id.UserId; |
|||
import org.thingsboard.server.common.data.permission.GroupPermission; |
|||
import org.thingsboard.server.common.data.role.Role; |
|||
import org.thingsboard.server.common.data.role.RoleType; |
|||
import org.thingsboard.server.common.data.security.Authority; |
|||
import org.thingsboard.server.dao.service.DaoSqlTest; |
|||
|
|||
import java.util.concurrent.TimeUnit; |
|||
import java.util.function.Consumer; |
|||
|
|||
import static org.mockito.ArgumentMatchers.any; |
|||
import static org.mockito.ArgumentMatchers.argThat; |
|||
import static org.mockito.ArgumentMatchers.eq; |
|||
import static org.mockito.ArgumentMatchers.isNull; |
|||
import static org.mockito.Mockito.doAnswer; |
|||
import static org.mockito.Mockito.never; |
|||
import static org.mockito.Mockito.verify; |
|||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
|||
|
|||
@DaoSqlTest |
|||
public class DashboardReportControllerTest extends AbstractControllerTest { |
|||
|
|||
private static final String DASHBOARD_VIEWER_EMAIL = "dashboard.viewer@thingsboard.org"; |
|||
private static final String DASHBOARD_VIEWER_PASSWORD = "viewer"; |
|||
private static final String REPORTS_SERVER_URL = "http://localhost:8383"; |
|||
private static final String REPORT_BASE_URL = "http://localhost:8081"; |
|||
|
|||
@MockitoSpyBean |
|||
private DashboardReportService dashboardReportService; |
|||
|
|||
private Dashboard dashboard; |
|||
|
|||
@Before |
|||
public void beforeTest() throws Exception { |
|||
loginSubCustomerAdminUser(); |
|||
Dashboard dashboard = new Dashboard(); |
|||
dashboard.setTitle("Sub-customer dashboard"); |
|||
this.dashboard = doPost("/api/dashboard", dashboard, Dashboard.class); |
|||
|
|||
doAnswer(invocation -> { |
|||
Consumer<DashboardReportData> onSuccess = invocation.getArgument(3); |
|||
DashboardReportData reportData = new DashboardReportData(); |
|||
reportData.setData(new byte[]{1}); |
|||
reportData.setName("report.pdf"); |
|||
reportData.setContentType("application/pdf"); |
|||
onSuccess.accept(reportData); |
|||
return null; |
|||
}).when(dashboardReportService).generateReport(any(), any(), any(), any(), any()); |
|||
} |
|||
|
|||
@Test |
|||
public void testDownloadTestReportRequiresPermissionOverReportUser() throws Exception { |
|||
loginDifferentTenant(); |
|||
UserId otherTenantUserId = savedDifferentTenantUser.getId(); |
|||
loginDifferentCustomer(); |
|||
|
|||
loginCustomerAdminUser(); |
|||
doPostTestReport(tenantAdminUserId.toString()).andExpect(status().isForbidden()); |
|||
doPostTestReport(differentCustomerUserId.toString()).andExpect(status().isForbidden()); |
|||
doPostTestReport(otherTenantUserId.toString()).andExpect(status().isForbidden()); |
|||
|
|||
loginSubCustomerAdminUser(); |
|||
doPostTestReport(customerAdminUserId.toString()).andExpect(status().isForbidden()); |
|||
|
|||
loginTenantAdmin(); |
|||
doPostTestReport(otherTenantUserId.toString()).andExpect(status().isForbidden()); |
|||
|
|||
createDashboardViewer(); |
|||
login(DASHBOARD_VIEWER_EMAIL, DASHBOARD_VIEWER_PASSWORD); |
|||
doPostTestReport(customerAdminUserId.toString()).andExpect(status().isForbidden()); |
|||
doPostTestReport(customerUserId.toString()).andExpect(status().isForbidden()); |
|||
|
|||
verify(dashboardReportService, never()).generateReport(any(), any(), any(), any(), any()); |
|||
} |
|||
|
|||
@Test |
|||
public void testDownloadTestReportAllowedForOwnUser() throws Exception { |
|||
loginCustomerAdminUser(); |
|||
doPostTestReport(customerAdminUserId.toString()).andExpect(status().isOk()); |
|||
verifyReportGenerated(customerAdminUserId); |
|||
|
|||
UserId dashboardViewerId = createDashboardViewer(); |
|||
login(DASHBOARD_VIEWER_EMAIL, DASHBOARD_VIEWER_PASSWORD); |
|||
doPostTestReport(dashboardViewerId.toString()).andExpect(status().isOk()); |
|||
verifyReportGenerated(dashboardViewerId); |
|||
} |
|||
|
|||
@Test |
|||
public void testDownloadTestReportAllowedForOwnedUser() throws Exception { |
|||
loginTenantAdmin(); |
|||
doPostTestReport(customerUserId.toString()).andExpect(status().isOk()); |
|||
verifyReportGenerated(customerUserId); |
|||
|
|||
loginCustomerAdminUser(); |
|||
doPostTestReport(subCustomerAdminUserId.toString()).andExpect(status().isOk()); |
|||
verifyReportGenerated(subCustomerAdminUserId); |
|||
doPostTestReport(customerUserId.toString()).andExpect(status().isOk()); |
|||
} |
|||
|
|||
@Test |
|||
public void testDownloadTestReportWithCurrentUserCredentialsUsesCurrentUserWhenReportUserNotAccessible() throws Exception { |
|||
loginCustomerAdminUser(); |
|||
doPostTestReport(tenantAdminUserId.toString(), null, true).andExpect(status().isOk()); |
|||
verifyReportGenerated(customerAdminUserId); |
|||
} |
|||
|
|||
@Test |
|||
public void testDownloadTestReportRequiresUserId() throws Exception { |
|||
loginCustomerAdminUser(); |
|||
doPostTestReport(null).andExpect(status().isBadRequest()); |
|||
doPostTestReport("not-a-uuid").andExpect(status().isBadRequest()); |
|||
verify(dashboardReportService, never()).generateReport(any(), any(), any(), any(), any()); |
|||
} |
|||
|
|||
@Test |
|||
public void testDownloadTestReportUsesSystemReportServerForCustomerUser() throws Exception { |
|||
loginCustomerAdminUser(); |
|||
doPostTestReport(customerAdminUserId.toString(), REPORTS_SERVER_URL).andExpect(status().isOk()); |
|||
verify(dashboardReportService).generateReport(eq(tenantId), any(), isNull(), any(), any()); |
|||
|
|||
loginTenantAdmin(); |
|||
doPostTestReport(tenantAdminUserId.toString(), REPORTS_SERVER_URL).andExpect(status().isOk()); |
|||
verify(dashboardReportService).generateReport(eq(tenantId), any(), eq(REPORTS_SERVER_URL), any(), any()); |
|||
} |
|||
|
|||
private UserId createDashboardViewer() throws Exception { |
|||
loginTenantAdmin(); |
|||
Role role = new Role(); |
|||
role.setTenantId(tenantId); |
|||
role.setCustomerId(customerId); |
|||
role.setType(RoleType.GENERIC); |
|||
role.setName("Dashboard viewer"); |
|||
role.setPermissions(JacksonUtil.toJsonNode("{\"DASHBOARD\":[\"READ\"]}")); |
|||
role = doPost("/api/role", role, Role.class); |
|||
|
|||
EntityGroup userGroup = new EntityGroup(); |
|||
userGroup.setName("Dashboard viewers"); |
|||
userGroup.setType(EntityType.USER); |
|||
userGroup.setOwnerId(customerId); |
|||
userGroup = doPost("/api/entityGroup", userGroup, EntityGroup.class); |
|||
doPost("/api/groupPermission", new GroupPermission(tenantId, userGroup.getId(), role.getId(), null, null, false), GroupPermission.class); |
|||
|
|||
User user = new User(); |
|||
user.setAuthority(Authority.CUSTOMER_USER); |
|||
user.setTenantId(tenantId); |
|||
user.setCustomerId(customerId); |
|||
user.setEmail(DASHBOARD_VIEWER_EMAIL); |
|||
return createUser(user, DASHBOARD_VIEWER_PASSWORD, userGroup.getId()).getId(); |
|||
} |
|||
|
|||
private ResultActions doPostTestReport(String userId) throws Exception { |
|||
return doPostTestReport(userId, null); |
|||
} |
|||
|
|||
private ResultActions doPostTestReport(String userId, String reportsServerEndpointUrl) throws Exception { |
|||
return doPostTestReport(userId, reportsServerEndpointUrl, false); |
|||
} |
|||
|
|||
private ResultActions doPostTestReport(String userId, String reportsServerEndpointUrl, boolean useCurrentUserCredentials) throws Exception { |
|||
DashboardReportConfig reportConfig = new DashboardReportConfig(); |
|||
reportConfig.setBaseUrl(REPORT_BASE_URL); |
|||
reportConfig.setDashboardId(dashboard.getId().toString()); |
|||
reportConfig.setTimezone("UTC"); |
|||
reportConfig.setNamePattern("report"); |
|||
reportConfig.setType("pdf"); |
|||
reportConfig.setUserId(userId); |
|||
reportConfig.setUseCurrentUserCredentials(useCurrentUserCredentials); |
|||
String url = "/api/report/test"; |
|||
if (reportsServerEndpointUrl != null) { |
|||
url += "?reportsServerEndpointUrl=" + reportsServerEndpointUrl; |
|||
} |
|||
return doPostAsync(url, reportConfig, TimeUnit.SECONDS.toMillis(30)); |
|||
} |
|||
|
|||
private void verifyReportGenerated(UserId userId) throws Exception { |
|||
verify(dashboardReportService).generateReport(eq(tenantId), argThat(config -> userId.toString().equals(config.getUserId())), any(), any(), any()); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,35 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
package org.thingsboard.server.service.install.update; |
|||
|
|||
import org.junit.jupiter.api.Test; |
|||
import org.thingsboard.server.common.data.EntityView; |
|||
import org.thingsboard.server.common.data.group.EntityGroup; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.dao.customer.CustomerService; |
|||
import org.thingsboard.server.dao.entityview.EntityViewService; |
|||
import org.thingsboard.server.dao.group.EntityGroupService; |
|||
|
|||
import java.util.UUID; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
import static org.mockito.Mockito.mock; |
|||
import static org.mockito.Mockito.verify; |
|||
|
|||
class EntityViewGroupAllUpdaterTest { |
|||
|
|||
@Test |
|||
void testUnassignFromCustomerSavesWithoutValidation() { |
|||
EntityViewService entityViewService = mock(EntityViewService.class); |
|||
EntityViewGroupAllUpdater updater = new EntityViewGroupAllUpdater(entityViewService, mock(CustomerService.class), |
|||
mock(EntityGroupService.class), new EntityGroup(), true); |
|||
EntityView entityView = new EntityView(); |
|||
entityView.setCustomerId(new CustomerId(UUID.randomUUID())); |
|||
|
|||
updater.unassignFromCustomer(entityView); |
|||
|
|||
verify(entityViewService).saveEntityView(entityView, false); |
|||
assertThat(entityView.getCustomerId().isNullUid()).isTrue(); |
|||
} |
|||
|
|||
} |
|||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue