Skip to content

Commit

Permalink
KAA-1256, added user email validation to GWT module,user management t…
Browse files Browse the repository at this point in the history
…ests were fixed
  • Loading branch information
Pyshankov committed Jul 19, 2016
1 parent 4e13548 commit 375e225
Show file tree
Hide file tree
Showing 10 changed files with 326 additions and 137 deletions.
Expand Up @@ -16,13 +16,17 @@

package org.kaaproject.kaa.common.dto;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;

public class TenantDto implements HasId, Serializable {

private static final long serialVersionUID = 330532156388681820L;

private String id;
@Size(min = 2,max = 255)
@NotNull(message="tenant name can't be null")
private String name;

public String getId() {
Expand Down
@@ -0,0 +1,24 @@
# Mongodb configurations
# list of mongodb nodes, possible to use multiply servers
servers=localhost:27017

# mongodb database name
db_name=kaa

# write concern for mongodb write operations
write_concern=acknowledged

#The number of connections allowed per host (the pool size, per host)
connections_per_host=100

# The max wait time for a blocking thread for a connection from the pool in ms_
max_wait_time=120000

# The connection timeout in milliseconds
connection_timeout=5000

# The socket timeout_ A timeout of zero is interpreted as an infinite timeout.
socket_timeout=0

# This controls whether or not to have socket keep alive turned on
socket_keepalive=false
Expand Up @@ -20,6 +20,7 @@
import java.util.ArrayList;
import java.util.List;

import com.google.gwt.regexp.shared.RegExp;
import org.kaaproject.avro.ui.gwt.client.widget.SizedTextBox;
import org.kaaproject.kaa.common.dto.KaaAuthorityDto;
import org.kaaproject.kaa.server.admin.client.mvp.view.UserView;
Expand Down Expand Up @@ -133,8 +134,15 @@ protected void resetImpl() {

@Override
protected boolean validate() {
String pattern = "^(?:[a-zA-Z0-9_'^&/+-])+(?:\\.(?:[a-zA-Z0-9_'^&/+-])+)\" +\n" +
" \"*@(?:(?:\\\\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\\\\.)\" +\n" +
" \"{3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\\\]?)|(?:[a-zA-Z0-9-]+\\\\.)\" +\n" +
" \"+(?:[a-zA-Z]){2,}\\.?)$";

RegExp regExp=RegExp.compile(pattern);

boolean result = userName.getValue().length()>0;
result &= email.getValue().length()>0;
result &= regExp.test(email.getValue());
result &= authority.getValue() != null;
return result;
}
Expand Down
Expand Up @@ -633,7 +633,6 @@ public org.kaaproject.kaa.common.dto.admin.UserDto editUser(org.kaaproject.kaa.c
checkTenantId(storedUser.getTenantId());
}
Long userId = saveUser(user);
user.setId(userId.toString());
UserDto userDto = new UserDto();
userDto.setId(user.getId());
userDto.setUsername(user.getUsername());
Expand Down Expand Up @@ -2854,22 +2853,6 @@ public String getRecordDataByApplicationIdAndSchemaVersion(String applicationId,
}
}

// private TenantUserDto toTenantUser(TenantAdminDto tenantAdmin) {
// User user = userFacade.findById(Long.valueOf(tenantAdmin.getExternalUid()));
// LOG.debug("Convert tenant admin to tenant user {}.", user);
// TenantUserDto tenantUser = null;
// if (user != null) {
// tenantUser = new TenantUserDto(user.getId().toString(), user.getUsername(), user.getFirstName(), user.getLastName(),
// user.getMail(), KaaAuthorityDto.valueOf(user.getAuthorities().iterator().next().getAuthority()));
// tenantUser.setId(tenantAdmin.getUserId());
// tenantUser.setTenantId(tenantAdmin.getTenant().getId());
// tenantUser.setTenantName(tenantAdmin.getTenant().getName());
// } else {
// LOG.debug("Can't find tenant user by external id {}.", tenantAdmin.getExternalUid());
// }
// return tenantUser;
// }

private org.kaaproject.kaa.common.dto.admin.UserDto toUser(UserDto tenantUser) {
User user = userFacade.findById(Long.valueOf(tenantUser.getExternalUid()));
org.kaaproject.kaa.common.dto.admin.UserDto result = new org.kaaproject.kaa.common.dto.admin.UserDto(user.getId().toString(),
Expand Down
Expand Up @@ -53,6 +53,7 @@
import org.kaaproject.kaa.server.common.dao.AbstractTest;
import org.kaaproject.kaa.server.common.dao.impl.sql.H2DBTestRunner;
import org.kaaproject.kaa.server.common.dao.impl.sql.PostgreDBTestRunner;
import org.kaaproject.kaa.server.common.dao.model.sql.Tenant;
import org.kaaproject.kaa.server.common.nosql.mongo.dao.MongoDBTestRunner;
import org.kaaproject.kaa.server.node.service.initialization.KaaNodeInitializationService;
import org.slf4j.Logger;
Expand Down Expand Up @@ -358,10 +359,8 @@ protected boolean createTenantDeveloperNeeded() {
private void createUsers() throws Exception {
LOG.info("Creating users...");
client.createKaaAdmin(kaaAdminUser, kaaAdminPassword);
loginTenantAdmin(tenantAdminUser);
if (createTenantAdminNeeded()) {
tenantAdminDto = createTenant(tenantAdminUser);

tenantAdminDto = createTenantAdmin(tenantAdminUser);
loginTenantAdmin(tenantAdminUser);
if (createTenantDeveloperNeeded()) {
tenantDeveloperDto = createTenantDeveloper(tenantDeveloperUser);
Expand Down Expand Up @@ -487,14 +486,24 @@ public int compare(HasId o1, HasId o2) {
}
}

protected TenantDto createTenant() throws Exception{
loginKaaAdmin();
TenantDto tenantDto = new TenantDto();
tenantDto.setName(generateString(TENANT_NAME));
tenantDto= client.editTenant(tenantDto);
return tenantDto;
}



/**
* Creates the tenant.
*
* @return the tenant user dto
* @throws Exception the exception
*/
protected TenantAdminDto createTenant() throws Exception {
return createTenant(null);
protected TenantAdminDto createTenantAdmin() throws Exception {
return createTenantAdmin(null);
}

/**
Expand All @@ -504,17 +513,21 @@ protected TenantAdminDto createTenant() throws Exception {
* @return the tenant user dto
* @throws Exception the exception
*/
protected TenantAdminDto createTenant(String username) throws Exception {
protected TenantAdminDto createTenantAdmin(String username) throws Exception {
loginKaaAdmin();
if (username == null) {
username = generateString(username);
}
TenantDto tenantDto=null;
TenantDto tenantDto = new TenantDto();
if(client.getTenants().isEmpty()) {
tenantDto = new TenantDto();
tenantDto.setName(TENANT_NAME);
tenantDto = client.editTenant(tenantDto);
}else tenantDto=client.getTenant("1");
}else {
for(TenantDto t :client.getTenants()){
if(t.getName().equals(TENANT_NAME))
tenantDto=t;
}
}

org.kaaproject.kaa.common.dto.admin.UserDto tenAdmin = new org.kaaproject.kaa.common.dto.admin.UserDto();
tenAdmin.setAuthority(KaaAuthorityDto.TENANT_ADMIN);
Expand Down Expand Up @@ -554,6 +567,7 @@ private org.kaaproject.kaa.common.dto.admin.UserDto createTenantDeveloper(String
tenantDeveloper.setMail(username + "@demoproject.org");
tenantDeveloper.setFirstName("Tenant");
tenantDeveloper.setLastName("Developer");
tenantDeveloper.setTenantId(tenantAdminDto.getTenant().getId());
tenantDeveloper = client.editUser(tenantDeveloper);

if (StringUtils.isNotBlank(tenantDeveloper.getTempPassword())) {
Expand Down Expand Up @@ -591,8 +605,9 @@ protected UserDto createUser(TenantAdminDto tenant, KaaAuthorityDto authority) t
user.setLastName(generateString("User"));
user.setAuthority(authority);
if (tenant == null) {
tenant = createTenant();
tenant = createTenantAdmin();
}
user.setTenantId(tenantAdminDto.getTenant().getId());
loginTenantAdmin(tenant.getUsername());
UserDto savedUser = client.editUser(user);
return savedUser;
Expand All @@ -619,7 +634,7 @@ protected ApplicationDto createApplication(TenantAdminDto tenant) throws Excepti
ApplicationDto application = new ApplicationDto();
application.setName(generateString(APPLICATION));
if (tenant == null) {
tenant = createTenant();
tenant = createTenantAdmin();
}
loginTenantAdmin(tenant.getUsername());
ApplicationDto savedApplication = client
Expand Down Expand Up @@ -1159,7 +1174,7 @@ protected EventClassFamilyDto createEventClassFamily(String tenantId, String cla
}
eventClassFamily.setClassName(className);
if (strIsEmpty(tenantId)) {
TenantAdminDto tenant = createTenant(tenantAdminUser);
TenantAdminDto tenant = createTenantAdmin(tenantAdminUser);
eventClassFamily.setTenantId(tenant.getId());
}
else {
Expand Down
@@ -0,0 +1,157 @@
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.kaaproject.kaa.server.control;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.kaaproject.kaa.common.dto.ApplicationDto;
import org.kaaproject.kaa.common.dto.event.AefMapInfoDto;
import org.kaaproject.kaa.common.dto.event.ApplicationEventFamilyMapDto;
import org.kaaproject.kaa.common.dto.event.EcfInfoDto;
import org.kaaproject.kaa.common.dto.event.EventClassFamilyDto;
import org.springframework.web.client.ResourceAccessException;

/**
* The Class ControlServerApplicationEventMapIT.
*/
public class ControlServerApplicationEventMapIT extends AbstractTestControlServer {

/**
* Test create application event family map.
*
* @throws Exception the exception
*/
@Test
public void testCreateApplicationEventFamilyMap() throws Exception {
ApplicationEventFamilyMapDto applicationEventFamilyMap = createApplicationEventFamilyMap();
Assert.assertFalse(strIsEmpty(applicationEventFamilyMap.getId()));
}

/**
* Test get application event family map.
*
* @throws Exception the exception
*/
@Test
public void testGetApplicationEventFamilyMap() throws Exception {
ApplicationEventFamilyMapDto applicationEventFamilyMap = createApplicationEventFamilyMap();

ApplicationEventFamilyMapDto storedApplicationEventFamilyMap = client.getApplicationEventFamilyMap(applicationEventFamilyMap.getId());

Assert.assertNotNull(storedApplicationEventFamilyMap);
Assert.assertEquals(applicationEventFamilyMap, storedApplicationEventFamilyMap);
}

/**
* Test get application event family maps by application token.
*
* @throws Exception the exception
*/
@Test
public void testGetApplicationEventFamilyMapsByApplicationToken() throws Exception {
List<ApplicationEventFamilyMapDto> applicationEventFamilyMaps = new ArrayList<>(10);
ApplicationDto application = createApplication(tenantAdminDto);
EventClassFamilyDto eventClassFamily = createEventClassFamily(application.getTenantId());
for (int i=0;i<10;i++) {
ApplicationEventFamilyMapDto applicationEventFamilyMap = createApplicationEventFamilyMap(
application.getApplicationToken(), eventClassFamily.getId(), (i+1));
applicationEventFamilyMaps.add(applicationEventFamilyMap);
}

Collections.sort(applicationEventFamilyMaps, new IdComparator());

loginTenantDeveloper(tenantDeveloperUser);

List<ApplicationEventFamilyMapDto> storedApplicationEventFamilyMaps = client.getApplicationEventFamilyMapsByApplicationToken(
application.getApplicationToken());

Collections.sort(storedApplicationEventFamilyMaps, new IdComparator());

Assert.assertEquals(applicationEventFamilyMaps, storedApplicationEventFamilyMaps);
}

/**
* Test update application event family map.
*
* @throws Exception the exception
*/
@Test
public void testUpdateApplicationEventFamilyMap() throws Exception {
final ApplicationEventFamilyMapDto applicationEventFamilyMap = createApplicationEventFamilyMap();
checkBadRequest(new TestRestCall() {
@Override
public void executeRestCall() throws Exception {
client.editApplicationEventFamilyMap(applicationEventFamilyMap);
}
});
}

/**
* Test get vacant event class families by application token.
*
* @throws Exception the exception
*/
@Test
public void testGetVacantEventClassFamiliesByApplicationToken() throws Exception {
ApplicationDto application = createApplication(tenantAdminDto);
EventClassFamilyDto eventClassFamily = createEventClassFamily(application.getTenantId());
createApplicationEventFamilyMap(application.getApplicationToken(), eventClassFamily.getId(), 1);

loginTenantDeveloper(tenantDeveloperUser);
List<EcfInfoDto> vacantEcfs = client.getVacantEventClassFamiliesByApplicationToken(application.getApplicationToken());
Assert.assertNotNull(vacantEcfs);
Assert.assertEquals(0, vacantEcfs.size());

loginTenantAdmin(tenantAdminUser);
client.addEventClassFamilySchema(eventClassFamily.getId(), TEST_EVENT_CLASS_FAMILY_SCHEMA);

loginTenantDeveloper(tenantDeveloperUser);
vacantEcfs = client.getVacantEventClassFamiliesByApplicationToken(application.getApplicationToken());
Assert.assertNotNull(vacantEcfs);
Assert.assertEquals(1, vacantEcfs.size());
Assert.assertNotNull(vacantEcfs.get(0));
Assert.assertEquals(eventClassFamily.getId(), vacantEcfs.get(0).getEcfId());
Assert.assertEquals(eventClassFamily.getName(), vacantEcfs.get(0).getEcfName());
Assert.assertEquals(2, vacantEcfs.get(0).getVersion());
}

/**
* Test get event class families by application token.
*
* @throws Exception the exception
*/
@Test
public void testGetEventClassFamiliesByApplicationToken() throws Exception {
ApplicationDto application = createApplication(tenantAdminDto);
EventClassFamilyDto eventClassFamily = createEventClassFamily(application.getTenantId());
createApplicationEventFamilyMap(application.getApplicationToken(), eventClassFamily.getId(), 1);

loginTenantDeveloper(tenantDeveloperUser);

List<AefMapInfoDto> applicationEcfs = client.getEventClassFamiliesByApplicationToken(application.getApplicationToken());
Assert.assertNotNull(applicationEcfs);
Assert.assertEquals(1, applicationEcfs.size());
Assert.assertNotNull(applicationEcfs.get(0));
Assert.assertEquals(eventClassFamily.getId(), applicationEcfs.get(0).getEcfId());
Assert.assertEquals(eventClassFamily.getName(), applicationEcfs.get(0).getEcfName());
Assert.assertEquals(1, applicationEcfs.get(0).getVersion());
}
}
Expand Up @@ -84,7 +84,7 @@ public void testGetEventClassFamily() throws Exception {
@Test
public void testGetEventClassFamiliesByTenantId() throws Exception {
List<EventClassFamilyDto> eventClassFamilies = new ArrayList<>(10);
TenantAdminDto tenant = createTenant(tenantAdminUser);
TenantAdminDto tenant = createTenantAdmin(tenantAdminUser);
loginTenantAdmin(tenantAdminUser);
for (int i=0;i<10;i++) {
EventClassFamilyDto eventClassFamily = createEventClassFamily(tenant.getId(), ""+i);
Expand Down Expand Up @@ -147,7 +147,7 @@ public void testGetEventClassesByFamilyIdVersionAndType() throws Exception {
*/
@Test
public void testDuplicateEventClassFamilyName() throws Exception {
TenantAdminDto tenant = createTenant(tenantAdminUser);
TenantAdminDto tenant = createTenantAdmin(tenantAdminUser);
loginTenantAdmin(tenantAdminUser);
EventClassFamilyDto eventClassFamily = createEventClassFamily(tenant.getId());
final EventClassFamilyDto secondEventClassFamily = createEventClassFamily(tenant.getId(), "test");
Expand Down Expand Up @@ -200,7 +200,7 @@ public void testAddEventClassFamilySchema() throws Exception {
*/
@Test
public void testDuplicateEventClassFamilyFqns() throws Exception {
TenantAdminDto tenant = createTenant(tenantAdminUser);
TenantAdminDto tenant = createTenantAdmin(tenantAdminUser);
loginTenantAdmin(tenantAdminUser);
EventClassFamilyDto eventClassFamily = createEventClassFamily(tenant.getId());
client.addEventClassFamilySchema(eventClassFamily.getId(), TEST_EVENT_CLASS_FAMILY_SCHEMA);
Expand Down

0 comments on commit 375e225

Please sign in to comment.