|
|
|
@ -27,6 +27,7 @@ import org.testcontainers.shaded.org.awaitility.Awaitility; |
|
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
|
import org.thingsboard.common.util.ThingsBoardExecutors; |
|
|
|
import org.thingsboard.server.common.data.AttributeScope; |
|
|
|
import org.thingsboard.server.common.data.ClaimRequest; |
|
|
|
import org.thingsboard.server.common.data.Customer; |
|
|
|
import org.thingsboard.server.common.data.Device; |
|
|
|
import org.thingsboard.server.common.data.DeviceProfile; |
|
|
|
@ -60,6 +61,8 @@ import org.thingsboard.server.common.data.sync.ie.importing.csv.BulkImportColumn |
|
|
|
import org.thingsboard.server.common.data.sync.ie.importing.csv.BulkImportRequest; |
|
|
|
import org.thingsboard.server.common.data.sync.ie.importing.csv.BulkImportResult; |
|
|
|
import org.thingsboard.server.dao.device.DeviceDao; |
|
|
|
import org.thingsboard.server.dao.device.claim.ClaimResponse; |
|
|
|
import org.thingsboard.server.dao.device.claim.ClaimResult; |
|
|
|
import org.thingsboard.server.dao.exception.DeviceCredentialsValidationException; |
|
|
|
import org.thingsboard.server.dao.service.DaoSqlTest; |
|
|
|
import org.thingsboard.server.exception.DataValidationException; |
|
|
|
@ -1518,4 +1521,97 @@ public class DeviceControllerTest extends AbstractControllerTest { |
|
|
|
assertThat(resavedSecondDevice.getExternalId()).isEqualTo(secondExternalId); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testReclaimDeviceByCustomerUser() throws Exception { |
|
|
|
Device device = createTenantDevice("Customer device to reclaim", customerId); |
|
|
|
|
|
|
|
loginCustomerAdminUser(); |
|
|
|
doDeleteAsync("/api/customer/device/" + device.getName() + "/claim", String.class); |
|
|
|
|
|
|
|
loginTenantAdmin(); |
|
|
|
Device reclaimedDevice = doGet("/api/device/" + device.getId().getId(), Device.class); |
|
|
|
assertThat(reclaimedDevice.getOwnerId()).isEqualTo(tenantId); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testReclaimDeviceOfDifferentCustomerByCustomerUser() throws Exception { |
|
|
|
createDifferentCustomer(); |
|
|
|
Device device = createTenantDevice("Different customer device", differentCustomerId); |
|
|
|
|
|
|
|
loginCustomerAdminUser(); |
|
|
|
doDelete("/api/customer/device/" + device.getName() + "/claim").andExpect(status().isForbidden()); |
|
|
|
|
|
|
|
loginTenantAdmin(); |
|
|
|
Device foundDevice = doGet("/api/device/" + device.getId().getId(), Device.class); |
|
|
|
assertThat(foundDevice.getOwnerId()).isEqualTo(differentCustomerId); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testReclaimSubCustomerDeviceByParentCustomerUser() throws Exception { |
|
|
|
Device device = createTenantDevice("Sub-customer device to reclaim", subCustomerId); |
|
|
|
|
|
|
|
loginCustomerAdminUser(); |
|
|
|
doDeleteAsync("/api/customer/device/" + device.getName() + "/claim", String.class); |
|
|
|
|
|
|
|
loginTenantAdmin(); |
|
|
|
Device reclaimedDevice = doGet("/api/device/" + device.getId().getId(), Device.class); |
|
|
|
assertThat(reclaimedDevice.getOwnerId()).isEqualTo(tenantId); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testReclaimParentCustomerDeviceBySubCustomerUser() throws Exception { |
|
|
|
Device device = createTenantDevice("Parent customer device", customerId); |
|
|
|
|
|
|
|
loginSubCustomerAdminUser(); |
|
|
|
doDelete("/api/customer/device/" + device.getName() + "/claim").andExpect(status().isForbidden()); |
|
|
|
|
|
|
|
loginTenantAdmin(); |
|
|
|
Device foundDevice = doGet("/api/device/" + device.getId().getId(), Device.class); |
|
|
|
assertThat(foundDevice.getOwnerId()).isEqualTo(customerId); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testReclaimDeviceByTenantAdmin() throws Exception { |
|
|
|
Device device = createTenantDevice("Customer device to reclaim by tenant", customerId); |
|
|
|
|
|
|
|
doDeleteAsync("/api/customer/device/" + device.getName() + "/claim", String.class); |
|
|
|
|
|
|
|
Device reclaimedDevice = doGet("/api/device/" + device.getId().getId(), Device.class); |
|
|
|
assertThat(reclaimedDevice.getOwnerId()).isEqualTo(tenantId); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testClaimUnassignedDeviceByCustomerUser() throws Exception { |
|
|
|
Device device = createTenantDevice("Unassigned device to claim", null); |
|
|
|
claimDevicesService.registerClaimingInfo(tenantId, device.getId(), "secret", TimeUnit.MINUTES.toMillis(1)).get(); |
|
|
|
|
|
|
|
loginCustomerAdminUser(); |
|
|
|
ClaimResult claimResult = doPostClaimAsync("/api/customer/device/" + device.getName() + "/claim", |
|
|
|
new ClaimRequest("secret"), ClaimResult.class, status().isOk()); |
|
|
|
assertThat(claimResult.getResponse()).isEqualTo(ClaimResponse.SUCCESS); |
|
|
|
assertThat(claimResult.getDevice().getOwnerId()).isEqualTo(customerId); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testClaimDeviceOfDifferentCustomerByCustomerUser() throws Exception { |
|
|
|
createDifferentCustomer(); |
|
|
|
Device device = createTenantDevice("Different customer device to claim", differentCustomerId); |
|
|
|
|
|
|
|
loginCustomerAdminUser(); |
|
|
|
doPost("/api/customer/device/" + device.getName() + "/claim", new ClaimRequest(null)).andExpect(status().isForbidden()); |
|
|
|
|
|
|
|
loginTenantAdmin(); |
|
|
|
Device foundDevice = doGet("/api/device/" + device.getId().getId(), Device.class); |
|
|
|
assertThat(foundDevice.getOwnerId()).isEqualTo(differentCustomerId); |
|
|
|
} |
|
|
|
|
|
|
|
private Device createTenantDevice(String name, CustomerId ownerId) throws Exception { |
|
|
|
loginTenantAdmin(); |
|
|
|
Device device = new Device(); |
|
|
|
device.setName(name); |
|
|
|
device.setType("default"); |
|
|
|
device.setCustomerId(ownerId); |
|
|
|
return doPost("/api/device", device, Device.class); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|