Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = 'com.flexcodelabs'
version = '0.0.71'
version = '0.0.72'
description = 'Flextuma App'

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

/** Suggestions is empty when {@code available} is true -- there's nothing to suggest an
* alternative to. */
public record UsernameAvailabilityDto(String username, boolean available, List<String> suggestions) {
* alternative to. {@code emailAvailable} is null when no email was passed to the check. */
public record UsernameAvailabilityDto(String username, boolean available, List<String> suggestions,
Boolean emailAvailable) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.flexcodelabs.flextuma.core.dtos;

/** Request body for the username-availability check. {@code email} is optional. */
public record UsernameAvailabilityRequestDto(String username, String email) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public interface UserRepository extends JpaRepository<User, UUID>, JpaSpecificat
Optional<User> findByPhoneNumber(String phoneNumber);

boolean existsByUsername(String username);

boolean existsByEmail(String email);
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) {
.requestMatchers(org.springframework.http.HttpMethod.POST, "/api/webhooks/whatsapp/**").permitAll()
.requestMatchers(org.springframework.http.HttpMethod.POST, "/api/webhooks/*").permitAll()
.requestMatchers(org.springframework.http.HttpMethod.GET, "/api/public/**").permitAll()
.requestMatchers(org.springframework.http.HttpMethod.POST, "/api/public/**").permitAll()
.requestMatchers("/").permitAll()
.requestMatchers("/assets/**").permitAll()
.requestMatchers(new RegexRequestMatcher("^/(?!api(?:/|$)).*", null)).permitAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.flexcodelabs.flextuma.core.dtos.UsernameAvailabilityDto;
import com.flexcodelabs.flextuma.core.dtos.UsernameAvailabilityRequestDto;
import com.flexcodelabs.flextuma.core.services.PublicEndpointRateLimitService;
import com.flexcodelabs.flextuma.modules.auth.services.UserService;

Expand All @@ -33,10 +34,10 @@ public class PublicUserController {
@Value("${flextuma.rate-limit.username-availability.window-seconds:60}")
private int windowSeconds;

@GetMapping("/username-availability")
@PostMapping("/username-availability")
public ResponseEntity<UsernameAvailabilityDto> checkUsernameAvailability(
@RequestParam("username") String username, HttpServletRequest httpRequest) {
@RequestBody UsernameAvailabilityRequestDto request, HttpServletRequest httpRequest) {
rateLimitService.checkAndRecord(USERNAME_AVAILABILITY_BUCKET, httpRequest, maxRequestsPerWindow, windowSeconds);
return ResponseEntity.ok(userService.checkUsernameAvailability(username));
return ResponseEntity.ok(userService.checkUsernameAvailability(request.username(), request.email()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ public User findByUsername(String username) {
}

/** Public, unauthenticated username-availability check backing the signup form's live
* validation. When taken, suggests alternatives so the caller isn't left to guess one. */
public UsernameAvailabilityDto checkUsernameAvailability(String rawUsername) {
* validation. When taken, suggests alternatives so the caller isn't left to guess one --
* built from the email's local part (before '@') when an email was passed and it's still
* free, since that's more likely to read as "theirs" than a random suffix. */
public UsernameAvailabilityDto checkUsernameAvailability(String rawUsername, String rawEmail) {
String username = rawUsername == null ? "" : rawUsername.trim();
if (username.isBlank()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Username is required");
Expand All @@ -158,9 +160,28 @@ public UsernameAvailabilityDto checkUsernameAvailability(String rawUsername) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Username is too long");
}

String email = rawEmail == null ? "" : rawEmail.trim().toLowerCase();
Boolean emailAvailable = null;
String emailLocalPart = null;
if (!email.isBlank()) {
emailAvailable = !repository.existsByEmail(email);
int at = email.indexOf('@');
if (at > 0) {
emailLocalPart = email.substring(0, at);
}
}

boolean available = !repository.existsByUsername(username);
List<String> suggestions = available ? List.of() : generateAvailableUsernames(username);
return new UsernameAvailabilityDto(username, available, suggestions);
List<String> suggestions;
if (available) {
suggestions = List.of();
} else {
String suggestionBase = Boolean.TRUE.equals(emailAvailable) && emailLocalPart != null
? emailLocalPart
: username;
suggestions = generateAvailableUsernames(suggestionBase);
}
return new UsernameAvailabilityDto(username, available, suggestions, emailAvailable);
}

private List<String> generateAvailableUsernames(String requested) {
Expand All @@ -187,6 +208,12 @@ public User register(RegisterDto request) {
throw new ResponseStatusException(HttpStatus.CONFLICT,
"User with username " + request.getUsername() + " already exists");
});
if (request.getEmail() != null && !request.getEmail().isBlank()) {
repository.findByEmail(request.getEmail()).ifPresent(u -> {
throw new ResponseStatusException(HttpStatus.CONFLICT,
"User with email " + request.getEmail() + " already exists");
});
}

User user = new User();
user.setName(request.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.List;

import org.springframework.http.MediaType;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -47,10 +49,12 @@ void setUp() {

@Test
void checkUsernameAvailability_shouldRecordAgainstRateLimiter_beforeReturningResult() throws Exception {
when(userService.checkUsernameAvailability("jane"))
.thenReturn(new UsernameAvailabilityDto("jane", true, List.of()));
when(userService.checkUsernameAvailability("jane", null))
.thenReturn(new UsernameAvailabilityDto("jane", true, List.of(), null));

mockMvc.perform(get("/api/public/users/username-availability").param("username", "jane"))
mockMvc.perform(post("/api/public/users/username-availability")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"username\":\"jane\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.available").value(true));

Expand All @@ -62,9 +66,11 @@ void checkUsernameAvailability_shouldRejectWithTooManyRequests_whenRateLimited()
doThrow(new RateLimitExceededException("Too many requests.", 30))
.when(rateLimitService).checkAndRecord(eq("username-availability"), any(), eq(20), eq(60));

mockMvc.perform(get("/api/public/users/username-availability").param("username", "jane"))
mockMvc.perform(post("/api/public/users/username-availability")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"username\":\"jane\"}"))
.andExpect(status().isTooManyRequests());

verify(userService, never()).checkUsernameAvailability(any());
verify(userService, never()).checkUsernameAvailability(any(), any());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,11 @@ void login_shouldNotLookUpByUsername_whenIdentifierLooksLikeEmailButUnknown() {
void checkUsernameAvailability_shouldReportAvailable_withNoSuggestions_whenUsernameIsFree() {
when(repository.existsByUsername("newname")).thenReturn(false);

var result = service.checkUsernameAvailability("newname");
var result = service.checkUsernameAvailability("newname", null);

assertTrue(result.available());
assertEquals(List.of(), result.suggestions());
assertNull(result.emailAvailable());
}

@Test
Expand All @@ -149,17 +150,46 @@ void checkUsernameAvailability_shouldSuggestAlternatives_whenUsernameIsTaken() {
when(repository.existsByUsername(argThat(candidate -> candidate.startsWith("jane") && !candidate.equals("jane"))))
.thenReturn(false);

var result = service.checkUsernameAvailability("jane");
var result = service.checkUsernameAvailability("jane", null);

assertFalse(result.available());
assertEquals(5, result.suggestions().size());
assertTrue(result.suggestions().stream().allMatch(s -> s.startsWith("jane") && !s.equals("jane")));
assertEquals(result.suggestions().size(), Set.copyOf(result.suggestions()).size());
}

@Test
void checkUsernameAvailability_shouldSuggestFromEmailLocalPart_whenUsernameTakenAndEmailFree() {
when(repository.existsByUsername("jane")).thenReturn(true);
when(repository.existsByEmail("jane.doe@example.com")).thenReturn(false);
when(repository.existsByUsername(argThat(candidate -> candidate.startsWith("janedoe") && !candidate.equals("janedoe"))))
.thenReturn(false);

var result = service.checkUsernameAvailability("jane", "jane.doe@example.com");

assertFalse(result.available());
assertTrue(result.emailAvailable());
assertEquals(5, result.suggestions().size());
assertTrue(result.suggestions().stream().allMatch(s -> s.startsWith("janedoe")));
}

@Test
void checkUsernameAvailability_shouldSuggestFromUsername_whenUsernameAndEmailBothTaken() {
when(repository.existsByUsername("jane")).thenReturn(true);
when(repository.existsByEmail("jane@example.com")).thenReturn(true);
when(repository.existsByUsername(argThat(candidate -> candidate.startsWith("jane") && !candidate.equals("jane"))))
.thenReturn(false);

var result = service.checkUsernameAvailability("jane", "jane@example.com");

assertFalse(result.available());
assertFalse(result.emailAvailable());
assertTrue(result.suggestions().stream().allMatch(s -> s.startsWith("jane")));
}

@Test
void checkUsernameAvailability_shouldThrow_whenUsernameBlank() {
assertThrows(ResponseStatusException.class, () -> service.checkUsernameAvailability(" "));
assertThrows(ResponseStatusException.class, () -> service.checkUsernameAvailability(" ", null));
}

@Test
Expand Down