Skip to content

Commit

Permalink
#18101 adding fixes for the codacy feedback and some unit test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jdotcms committed Mar 17, 2020
1 parent b9de60c commit 00288f3
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 154 deletions.
Expand Up @@ -2,6 +2,7 @@

import com.dotcms.auth.providers.jwt.beans.ApiToken;
import com.dotcms.auth.providers.jwt.beans.JWToken;
import com.dotcms.datagen.CompanyDataGen;
import com.dotcms.datagen.UserDataGen;
import com.dotcms.enterprise.cluster.ClusterFactory;
import com.dotcms.repackage.org.apache.commons.net.util.SubnetUtils;
Expand All @@ -10,6 +11,9 @@
import com.dotmarketing.business.CacheLocator;
import com.dotmarketing.business.DotStateException;
import com.dotmarketing.util.DateUtil;
import com.dotmarketing.util.UUIDGenerator;
import com.liferay.portal.ejb.CompanyUtil;
import com.liferay.portal.model.Company;
import com.liferay.portal.model.User;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -357,7 +361,26 @@ public void test_expired_ApiToken() throws Exception {
@Test
public void test_user_must_be_active_to_validate_ApiToken() throws Exception {

User user = new UserDataGen().nextPersisted();
final Company company = new CompanyDataGen()
.name("TestCompany")
.shortName("TC")
.authType("email")
.autoLogin(true)
.emailAddress("lol2@dotCMS.com")
.homeURL("localhost")
.city("NYC")
.mx("MX")
.type("test")
.phone("5552368")
.portalURL("/portalURL")
.nextPersisted();
assertNotNull(company.getCompanyId());
final Company retrievedCompany = CompanyUtil.findByPrimaryKey(company.getCompanyId());
assertEquals(company.getCompanyId(), retrievedCompany.getCompanyId());
User user = new UserDataGen().active(true)
.skinId(UUIDGenerator.generateUuid())
.companyId(retrievedCompany.getCompanyId())
.nextPersisted();
assertTrue(user.isActive());

ApiToken skinnyToken = ApiToken.from(getSkinnyToken()).withUserId(user.getUserId()).build();
Expand Down
Expand Up @@ -23,7 +23,7 @@ public class UserDataGen extends AbstractDataGen<User> {
private String lastName = "testLastName" + currentTime;
private String emailAddress = "testEmailAddress@" + currentTime + ".com";
private String password = String.valueOf(currentTime);
private String skinId = UUIDGenerator.generateUuid();
private String skinIdentifier = UUIDGenerator.generateUuid();
private String companyId = UUIDGenerator.generateUuid();
private List<Role> roles = new ArrayList<>();

Expand All @@ -39,8 +39,8 @@ public UserDataGen companyId(final String companyId) {
}

@SuppressWarnings("unused")
public UserDataGen skinId(final String skinId) {
this.skinId = skinId;
public UserDataGen skinId(final String skinIdentifier) {
this.skinIdentifier = skinIdentifier;
return this;
}

Expand Down Expand Up @@ -89,7 +89,7 @@ public User next() {
user.setLastName(lastName);
user.setEmailAddress(emailAddress);
user.setPassword(password);
user.setSkinId(this.skinId);
user.setSkinId(this.skinIdentifier);
user.setCompanyId(companyId);

return user;
Expand Down
Expand Up @@ -29,6 +29,7 @@

public class ResetPasswordResourceIntegrationTest{

private static final String DOTCMS_ORG_1 = "dotcms.org.1";
HttpServletRequest request;
ResponseUtil responseUtil;
ResetPasswordForm resetPasswordForm;
Expand Down Expand Up @@ -59,10 +60,9 @@ public void initTest(){
@Test
public void testNoSuchUserException() throws DotSecurityException, NoSuchUserException, DotInvalidTokenException, DotDataException {
UserManager userManager = getUserManagerThrowingException( new NoSuchUserException("") );
final User user = APILocator.getUserAPI().loadUserById("dotcms.org.1");
final JsonWebTokenService jsonWebTokenService = mock(JsonWebTokenService.class);
final UserToken jwtBean = new UserToken.Builder().id(UUIDGenerator.generateUuid())
.subject("dotcms.org.1").modificationDate(new Date()).expiresDate(100000).build();
.subject(DOTCMS_ORG_1).modificationDate(new Date()).expiresDate(100000).build();

when(jsonWebTokenService.parseToken(eq("token1"))).thenReturn(jwtBean);
ResetPasswordResource resetPasswordResource = new ResetPasswordResource(userManager, responseUtil, jsonWebTokenService);
Expand All @@ -74,10 +74,10 @@ public void testNoSuchUserException() throws DotSecurityException, NoSuchUserExc
@Test
public void testTokenInvalidException() throws DotSecurityException, NoSuchUserException, DotInvalidTokenException, DotDataException {
UserManager userManager = getUserManagerThrowingException( new DotInvalidTokenException("") );
final User user = APILocator.getUserAPI().loadUserById("dotcms.org.1");
final User user = APILocator.getUserAPI().loadUserById(DOTCMS_ORG_1);
final JsonWebTokenService jsonWebTokenService = mock(JsonWebTokenService.class);
final UserToken jwtBean = new UserToken.Builder().id(user.getRememberMeToken())
.subject("dotcms.org.1")
.subject(DOTCMS_ORG_1)
.modificationDate(new Date())
.expiresDate(100000).build();
when(jsonWebTokenService.parseToken(eq("token1"))).thenReturn(jwtBean);
Expand All @@ -90,10 +90,10 @@ public void testTokenInvalidException() throws DotSecurityException, NoSuchUserE
@Test
public void testTokenExpiredException() throws DotSecurityException, NoSuchUserException, DotInvalidTokenException, DotDataException {
UserManager userManager = getUserManagerThrowingException( new DotInvalidTokenException("", true) );
final User user = APILocator.getUserAPI().loadUserById("dotcms.org.1");
final User user = APILocator.getUserAPI().loadUserById(DOTCMS_ORG_1);
final JsonWebTokenService jsonWebTokenService = mock(JsonWebTokenService.class);
final UserToken jwtBean = new UserToken.Builder().id(user.getRememberMeToken()).
subject("dotcms.org.1").modificationDate(new Date()).expiresDate(100000).build();
subject(DOTCMS_ORG_1).modificationDate(new Date()).expiresDate(100000).build();

when(jsonWebTokenService.parseToken(eq("token1"))).thenReturn(jwtBean);
ResetPasswordResource resetPasswordResource = new ResetPasswordResource(userManager, responseUtil, jsonWebTokenService);
Expand All @@ -105,7 +105,7 @@ public void testTokenExpiredException() throws DotSecurityException, NoSuchUserE
private UserManager getUserManagerThrowingException(Exception e)
throws NoSuchUserException, DotSecurityException, DotInvalidTokenException {
UserManager userManager = mock( UserManager.class );
doThrow( e ).when( userManager ).resetPassword("dotcms.org.1",
doThrow( e ).when( userManager ).resetPassword(DOTCMS_ORG_1,
"token2", resetPasswordForm.getPassword());
return userManager;
}
Expand Down
Expand Up @@ -11,6 +11,12 @@
*/
public class UserPersistenceTest {

private static final String TOKEN_XXX = "xxx";
private static final String TOKEN_YYY = "yyy";
private static final String TOKEN_EEE = "eee";
private static final String TOKEN_III = "iii";
private static final String TOKEN_USER_ID = "111";

@BeforeClass
public static void prepare() throws Exception{
//Setting web app environment
Expand All @@ -24,20 +30,20 @@ public void test_diff_same_user_nodiff() {
final UserHBM userHBM = new UserHBM();
final User user = new User();

userHBM.setPassword("xxx");
user.setPassword("xxx");
userHBM.setPassword(TOKEN_XXX);
user.setPassword(TOKEN_XXX);

userHBM.setCompanyId("yyy");
user.setCompanyId("yyy");
userHBM.setCompanyId(TOKEN_YYY);
user.setCompanyId(TOKEN_YYY);

userHBM.setEmailAddress("eee");
user.setEmailAddress("eee");
userHBM.setEmailAddress(TOKEN_EEE);
user.setEmailAddress(TOKEN_EEE);

userHBM.setLayoutIds("iii");
user.setLayoutIds("iii");
userHBM.setLayoutIds(TOKEN_III);
user.setLayoutIds(TOKEN_III);

userHBM.setUserId("111");
user.setUserId("111");
userHBM.setUserId(TOKEN_USER_ID);
user.setUserId(TOKEN_USER_ID);

Assert.assertFalse(userPersistence.diff(userHBM, user));
}
Expand All @@ -49,20 +55,20 @@ public void test_diff_diff_pass_diff() {
final UserHBM userHBM = new UserHBM();
final User user = new User();

userHBM.setPassword("xxx");
userHBM.setPassword(TOKEN_XXX);
user.setPassword("ppp");

userHBM.setCompanyId("yyy");
user.setCompanyId("yyy");
userHBM.setCompanyId(TOKEN_YYY);
user.setCompanyId(TOKEN_YYY);

userHBM.setEmailAddress("eee");
user.setEmailAddress("eee");
userHBM.setEmailAddress(TOKEN_EEE);
user.setEmailAddress(TOKEN_EEE);

userHBM.setLayoutIds("iii");
user.setLayoutIds("iii");
userHBM.setLayoutIds(TOKEN_III);
user.setLayoutIds(TOKEN_III);

userHBM.setUserId("111");
user.setUserId("111");
userHBM.setUserId(TOKEN_USER_ID);
user.setUserId(TOKEN_USER_ID);

Assert.assertTrue(userPersistence.diff(userHBM, user));
}
Expand All @@ -74,20 +80,20 @@ public void test_diff_diff_company_diff() {
final UserHBM userHBM = new UserHBM();
final User user = new User();

userHBM.setPassword("xxx");
user.setPassword("xxx");
userHBM.setPassword(TOKEN_XXX);
user.setPassword(TOKEN_XXX);

userHBM.setCompanyId("yyy");
userHBM.setCompanyId(TOKEN_YYY);
user.setCompanyId("mmm");

userHBM.setEmailAddress("eee");
user.setEmailAddress("eee");
userHBM.setEmailAddress(TOKEN_EEE);
user.setEmailAddress(TOKEN_EEE);

userHBM.setLayoutIds("iii");
user.setLayoutIds("iii");
userHBM.setLayoutIds(TOKEN_III);
user.setLayoutIds(TOKEN_III);

userHBM.setUserId("111");
user.setUserId("111");
userHBM.setUserId(TOKEN_USER_ID);
user.setUserId(TOKEN_USER_ID);

Assert.assertTrue(userPersistence.diff(userHBM, user));
}
Expand All @@ -99,20 +105,20 @@ public void test_diff_diff_email_diff() {
final UserHBM userHBM = new UserHBM();
final User user = new User();

userHBM.setPassword("xxx");
user.setPassword("xxx");
userHBM.setPassword(TOKEN_XXX);
user.setPassword(TOKEN_XXX);

userHBM.setCompanyId("yyy");
user.setCompanyId("yyy");
userHBM.setCompanyId(TOKEN_YYY);
user.setCompanyId(TOKEN_YYY);

userHBM.setEmailAddress("eee");
userHBM.setEmailAddress(TOKEN_EEE);
user.setEmailAddress("eeeeeee");

userHBM.setLayoutIds("iii");
user.setLayoutIds("iii");
userHBM.setLayoutIds(TOKEN_III);
user.setLayoutIds(TOKEN_III);

userHBM.setUserId("111");
user.setUserId("111");
userHBM.setUserId(TOKEN_USER_ID);
user.setUserId(TOKEN_USER_ID);

Assert.assertTrue(userPersistence.diff(userHBM, user));
}
Expand All @@ -124,20 +130,20 @@ public void test_diff_diff_layout_diff() {
final UserHBM userHBM = new UserHBM();
final User user = new User();

userHBM.setPassword("xxx");
user.setPassword("xxx");
userHBM.setPassword(TOKEN_XXX);
user.setPassword(TOKEN_XXX);

userHBM.setCompanyId("yyy");
user.setCompanyId("yyy");
userHBM.setCompanyId(TOKEN_YYY);
user.setCompanyId(TOKEN_YYY);

userHBM.setEmailAddress("eee");
user.setEmailAddress("eee");
userHBM.setEmailAddress(TOKEN_EEE);
user.setEmailAddress(TOKEN_EEE);

userHBM.setLayoutIds("iii");
userHBM.setLayoutIds(TOKEN_III);
user.setLayoutIds("iiixyz");

userHBM.setUserId("111");
user.setUserId("111");
userHBM.setUserId(TOKEN_USER_ID);
user.setUserId(TOKEN_USER_ID);

Assert.assertTrue(userPersistence.diff(userHBM, user));
}
Expand All @@ -149,20 +155,20 @@ public void test_diff_diff_id_diff() {
final UserHBM userHBM = new UserHBM();
final User user = new User();

userHBM.setPassword("xxx");
user.setPassword("xxx");
userHBM.setPassword(TOKEN_XXX);
user.setPassword(TOKEN_XXX);

userHBM.setCompanyId("yyy");
user.setCompanyId("yyy");
userHBM.setCompanyId(TOKEN_YYY);
user.setCompanyId(TOKEN_YYY);

userHBM.setEmailAddress("eee");
user.setEmailAddress("eee");
userHBM.setEmailAddress(TOKEN_EEE);
user.setEmailAddress(TOKEN_EEE);

userHBM.setLayoutIds("iii");
user.setLayoutIds("iii");
userHBM.setLayoutIds(TOKEN_III);
user.setLayoutIds(TOKEN_III);

userHBM.setUserId("111222");
user.setUserId("111");
user.setUserId(TOKEN_USER_ID);

Assert.assertTrue(userPersistence.diff(userHBM, user));
}
Expand All @@ -174,20 +180,20 @@ public void test_diff_diff_id_null_diff() {
final UserHBM userHBM = new UserHBM();
final User user = new User();

userHBM.setPassword("xxx");
user.setPassword("xxx");
userHBM.setPassword(TOKEN_XXX);
user.setPassword(TOKEN_XXX);

userHBM.setCompanyId("yyy");
user.setCompanyId("yyy");
userHBM.setCompanyId(TOKEN_YYY);
user.setCompanyId(TOKEN_YYY);

userHBM.setEmailAddress("eee");
user.setEmailAddress("eee");
userHBM.setEmailAddress(TOKEN_EEE);
user.setEmailAddress(TOKEN_EEE);

userHBM.setLayoutIds("iii");
user.setLayoutIds("iii");
userHBM.setLayoutIds(TOKEN_III);
user.setLayoutIds(TOKEN_III);

userHBM.setUserId(null);
user.setUserId("111");
user.setUserId(TOKEN_USER_ID);

Assert.assertTrue(userPersistence.diff(userHBM, user));
}
Expand All @@ -199,19 +205,19 @@ public void test_diff_diff_id2_null_diff() {
final UserHBM userHBM = new UserHBM();
final User user = new User();

userHBM.setPassword("xxx");
user.setPassword("xxx");
userHBM.setPassword(TOKEN_XXX);
user.setPassword(TOKEN_XXX);

userHBM.setCompanyId("yyy");
user.setCompanyId("yyy");
userHBM.setCompanyId(TOKEN_YYY);
user.setCompanyId(TOKEN_YYY);

userHBM.setEmailAddress("eee");
user.setEmailAddress("eee");
userHBM.setEmailAddress(TOKEN_EEE);
user.setEmailAddress(TOKEN_EEE);

userHBM.setLayoutIds("iii");
user.setLayoutIds("iii");
userHBM.setLayoutIds(TOKEN_III);
user.setLayoutIds(TOKEN_III);

userHBM.setUserId("111");
userHBM.setUserId(TOKEN_USER_ID);
user.setUserId(null);

Assert.assertTrue(userPersistence.diff(userHBM, user));
Expand Down

0 comments on commit 00288f3

Please sign in to comment.