Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] CODENVY-627 Check for reserved usernames on user creation #1511

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*******************************************************************************/
package org.eclipse.che.api.user.server;

import com.google.common.collect.Sets;

import org.eclipse.che.api.core.ConflictException;
import org.eclipse.che.api.core.NotFoundException;
import org.eclipse.che.api.core.ServerException;
Expand All @@ -22,9 +24,11 @@
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Named;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static com.google.common.base.MoreObjects.firstNonNull;
import static java.lang.String.format;
Expand All @@ -45,15 +49,17 @@ public class UserManager {
private final UserDao userDao;
private final UserProfileDao profileDao;
private final PreferenceDao preferenceDao;

private final Set<String> reservedNames;

@Inject
public UserManager(UserDao userDao,
UserProfileDao profileDao,
PreferenceDao preferenceDao) {
PreferenceDao preferenceDao,
@Named("user.reserved_names") String[] reservedNames) {
this.userDao = userDao;
this.profileDao = profileDao;
this.preferenceDao = preferenceDao;
this.reservedNames = Sets.newHashSet(reservedNames);
}


Expand All @@ -68,6 +74,9 @@ public UserManager(UserDao userDao,
* when any other error occurs
*/
public void create(User user, boolean isTemporary) throws ConflictException, ServerException {
if (reservedNames.contains(user.getName())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to do this case-insensitive

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

throw new ConflictException(String.format("Username \"%s\" is reserved", user.getName()));
}
user.withId(generate("user", ID_LENGTH))
.withPassword(firstNonNull(user.getPassword(), generate("", PASSWORD_LENGTH)));
userDao.create(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
*******************************************************************************/
package org.eclipse.che.api.user.server;

import org.eclipse.che.api.core.BadRequestException;
import org.eclipse.che.api.core.ConflictException;
import org.eclipse.che.api.user.server.dao.PreferenceDao;
import org.eclipse.che.api.user.server.dao.Profile;
import org.eclipse.che.api.user.server.dao.User;
import org.eclipse.che.api.user.server.dao.UserDao;
import org.eclipse.che.api.user.server.dao.UserProfileDao;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.annotations.Listeners;
Expand All @@ -26,7 +25,6 @@
import static org.mockito.Matchers.anyMapOf;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

/**
Expand All @@ -45,13 +43,11 @@ public class UserManagerTest {
@Mock
PreferenceDao preferenceDao;

@InjectMocks
UserManager manager;

@Test
public void shouldCreateProfileAndPreferencesOnUserCreation() throws Exception {
final User user = new User().withEmail("test@email.com").withName("testName");
manager.create(user, false);

new UserManager(userDao, profileDao, preferenceDao, new String[0]).create(user, false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mb it would be better to create instance of UserManager in method annotated with @BeforeMethod

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it's ok as is it. If there will be more tests, maybe then add BeforeMethod.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mb it would be better to create instance of UserManager in method annotated with @BeforeMethod
+1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


verify(profileDao).create(any(Profile.class));
verify(preferenceDao).setPreferences(anyString(), anyMapOf(String.class, String.class));
Expand All @@ -60,8 +56,16 @@ public void shouldCreateProfileAndPreferencesOnUserCreation() throws Exception {
@Test
public void shouldGeneratedPasswordWhenCreatingUserAndItIsMissing() throws Exception {
final User user = new User().withEmail("test@email.com").withName("testName");
manager.create(user, false);

new UserManager(userDao, profileDao, preferenceDao, new String[0]).create(user, false);

verify(userDao).create(eq(user.withPassword("<none>")));
}

@Test(expectedExceptions = ConflictException.class)
public void shouldThrowConflictExceptionOnCreationIfUserNameIsReserved() throws Exception {
final User user = new User().withEmail("test@email.com").withName("reserved");

new UserManager(userDao, profileDao, preferenceDao, new String[] {"reserved"}).create(user, false);
}
}