feat: store company codes in capitals
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
ff4b12c2e9
commit
41c616b3f6
6 files changed
+27
-27
No files matched your search
@@ -185,8 +185,8 @@ ALTER TABLE `company`
|
||||
ADD COLUMN `code` varchar(20) NULL AFTER `name`,
|
||||
ADD COLUMN `status` char(1) NOT NULL DEFAULT 'A' COMMENT 'A active, I inactive' AFTER `code`;
|
||||
|
||||
UPDATE `company` SET `name` = 'INS', `code` = 'ins' WHERE `id` = 1;
|
||||
INSERT INTO `company` (`name`, `code`, `status`) VALUES ('CV', 'cv', 'A');
|
||||
UPDATE `company` SET `name` = 'INS', `code` = 'INS' WHERE `id` = 1;
|
||||
INSERT INTO `company` (`name`, `code`, `status`) VALUES ('CV', 'CV', 'A');
|
||||
|
||||
ALTER TABLE `company`
|
||||
MODIFY COLUMN `code` varchar(20) NOT NULL,
|
||||
@@ -226,7 +226,7 @@ DROP PROCEDURE `colite_check_admin_usernames`;
|
||||
UPDATE `user` u SET u.`company_id` = NULL
|
||||
WHERE u.`type` = 'A' AND EXISTS (SELECT 1 FROM `users_roles` r WHERE r.`user_id` = u.`id` AND r.`role_id` = 1);
|
||||
|
||||
UPDATE `user` u SET u.`company_id` = 1, u.`username` = CONCAT(u.`username`, '@ins')
|
||||
UPDATE `user` u SET u.`company_id` = 1, u.`username` = CONCAT(u.`username`, '@INS')
|
||||
WHERE u.`type` = 'A' AND NOT EXISTS (SELECT 1 FROM `users_roles` r WHERE r.`user_id` = u.`id` AND r.`role_id` = 1);
|
||||
|
||||
-- add-lead-dashboard/sql/01-status-change.sql (02 backfill has nothing to do on an empty volume)
|
||||
|
||||
@@ -4,14 +4,14 @@ Serial. One implementer, in this session.
|
||||
|
||||
## 1. Data
|
||||
|
||||
- [ ] 1.1 `sql/01-uppercase-codes.sql` and `sql/99-rollback.sql`; change the codes in `local/seed/03-colite.sql` to `INS` and `CV` (the `@ins` login names it builds follow); apply 01 to the local database.
|
||||
- [x] 1.1 `sql/01-uppercase-codes.sql` and `sql/99-rollback.sql`; change the codes in `local/seed/03-colite.sql` to `INS` and `CV` (the `@ins` login names it builds follow); apply 01 to the local database.
|
||||
- Depends on: none
|
||||
- Scope: `local/seed/**`, `openspec/changes/uppercase-company-codes/sql/**`
|
||||
- Verify: `SELECT code FROM company` gives `INS`, `CV`; staff login names end in `@INS` or `@CV`.
|
||||
|
||||
## 2. Backend
|
||||
|
||||
- [ ] 2.1 services-system `CompanyService` stores codes in capitals (`^[A-Z0-9-]{2,20}$`, entered value trimmed and turned to capitals); mobile-api admin login and forgot-password build the login name with the code in capitals.
|
||||
- [x] 2.1 services-system `CompanyService` stores codes in capitals (`^[A-Z0-9-]{2,20}$`, entered value trimmed and turned to capitals); mobile-api admin login and forgot-password build the login name with the code in capitals.
|
||||
- Depends on: 1.1
|
||||
- Tests first: create with `cv` stores `CV`; rename `cv` to `CV` on a company whose code is `cv` succeeds; `ins` is taken when `INS` exists; an invalid code is refused; login with company `cv` looks up `user@CV`.
|
||||
- Scope: `solvway-services-system/src/**`, `solvway-mobile-api/src/**`
|
||||
|
||||
+1
-1
@@ -207,7 +207,7 @@ public class AdminAuthController {
|
||||
if (!StringUtils.hasText(username) || username.contains("@")) {
|
||||
return null;
|
||||
}
|
||||
String code = company == null ? "" : company.trim().toLowerCase();
|
||||
String code = company == null ? "" : company.trim().toUpperCase();
|
||||
return code.isEmpty() ? username.trim() : username.trim() + "@" + code;
|
||||
}
|
||||
}
|
||||
+7
-7
@@ -84,9 +84,9 @@ class AdminAuthControllerCompanyTest {
|
||||
|
||||
ArgumentCaptor<AuthRequest> sent = ArgumentCaptor.forClass(AuthRequest.class);
|
||||
verify(auth).getToken(sent.capture());
|
||||
assertThat(sent.getValue().getUsername()).isEqualTo("john@ins");
|
||||
verify(auth).isLocked("john@ins");
|
||||
verify(auth).record("john@ins", "LOGIN_SUCCESS", null);
|
||||
assertThat(sent.getValue().getUsername()).isEqualTo("john@INS");
|
||||
verify(auth).isLocked("john@INS");
|
||||
verify(auth).record("john@INS", "LOGIN_SUCCESS", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -114,7 +114,7 @@ class AdminAuthControllerCompanyTest {
|
||||
void login_wrongCompanyOrUnknownUser_isBadLogin() throws Exception {
|
||||
when(auth.getToken(any())).thenReturn(null);
|
||||
login("{\"company\":\"nope\",\"username\":\"john\",\"password\":\"pw\"}").andExpect(status().isUnauthorized());
|
||||
verify(auth).record("john@nope", "LOGIN_FAIL", null);
|
||||
verify(auth).record("john@NOPE", "LOGIN_FAIL", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -124,7 +124,7 @@ class AdminAuthControllerCompanyTest {
|
||||
|
||||
login("{\"company\":\"ins\",\"username\":\"john\",\"password\":\"pw\"}")
|
||||
.andExpect(status().isUnauthorized()).andExpect(jsonPath("$.message").value("Invalid username or password"));
|
||||
verify(auth).record("john@ins", "LOGIN_FAIL", null);
|
||||
verify(auth).record("john@INS", "LOGIN_FAIL", null);
|
||||
verify(authenticationService).logoutToken("tok");
|
||||
}
|
||||
|
||||
@@ -154,10 +154,10 @@ class AdminAuthControllerCompanyTest {
|
||||
mvc.perform(post("/api/admin/auth/forgot-password").contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"company\":\"INS\",\"username\":\"john\",\"email\":\"j@x.com\"}"))
|
||||
.andExpect(status().isOk()).andExpect(jsonPath("$.result").value("true"));
|
||||
verify(auth).resetAdminPassword("john@ins", "j@x.com");
|
||||
verify(auth).resetAdminPassword("john@INS", "j@x.com");
|
||||
|
||||
mvc.perform(post("/api/admin/auth/forgot-password").contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"company\":\"ins\",\"username\":\"a@b\",\"email\":\"j@x.com\"}")).andExpect(status().isOk());
|
||||
verify(auth, never()).resetAdminPassword(org.mockito.ArgumentMatchers.eq("a@b@ins"), anyString());
|
||||
verify(auth, never()).resetAdminPassword(org.mockito.ArgumentMatchers.eq("a@b@INS"), anyString());
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -18,7 +18,7 @@ import java.util.regex.Pattern;
|
||||
public class CompanyService {
|
||||
|
||||
private static final int USERNAME_MAX = 255; // user.username column length
|
||||
private static final Pattern CODE = Pattern.compile("^[a-z0-9-]{2,20}$");
|
||||
private static final Pattern CODE = Pattern.compile("^[A-Z0-9-]{2,20}$");
|
||||
|
||||
private final CompanyRepository companies;
|
||||
private final UserRepository users;
|
||||
@@ -97,8 +97,8 @@ public class CompanyService {
|
||||
}
|
||||
|
||||
private static String validCode(String code) {
|
||||
String v = code == null ? "" : code.trim().toLowerCase();
|
||||
if (!CODE.matcher(v).matches()) throw new IllegalArgumentException("Code must be 2 to 20 characters: a-z, 0-9 or -");
|
||||
String v = code == null ? "" : code.trim().toUpperCase();
|
||||
if (!CODE.matcher(v).matches()) throw new IllegalArgumentException("Code must be 2 to 20 characters: A-Z, 0-9 or -");
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
+11
-11
@@ -69,17 +69,17 @@ class CompanyServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void createLowercasesCodeAndDefaultsToActive() {
|
||||
Company saved = service.save(company(null, " CV ", " CV-2 ", null));
|
||||
void createStoresCodeInCapitalsAndDefaultsToActive() {
|
||||
Company saved = service.save(company(null, " CV ", " cv-2 ", null));
|
||||
|
||||
assertThat(saved.getName()).isEqualTo("CV");
|
||||
assertThat(saved.getCode()).isEqualTo("cv-2");
|
||||
assertThat(saved.getCode()).isEqualTo("CV-2");
|
||||
assertThat(saved.getStatus()).isEqualTo("A");
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateCodeIgnoringCaseIsRefused() {
|
||||
when(companies.findByCodeIgnoreCase("ins")).thenReturn(Optional.of(company(1L, "INS", "ins", "A")));
|
||||
when(companies.findByCodeIgnoreCase("INS")).thenReturn(Optional.of(company(1L, "INS", "ins", "A")));
|
||||
|
||||
assertThatThrownBy(() -> service.save(company(null, "Other", "INS", "A")))
|
||||
.isInstanceOfSatisfying(ResponseStatusException.class, e -> assertThat(e.getStatusCode().value()).isEqualTo(409));
|
||||
@@ -87,7 +87,7 @@ class CompanyServiceTest {
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"", "a", "UPPER_case", "has space", "under_score", "abcdefghijklmnopqrstu"})
|
||||
@ValueSource(strings = {"", "a", "has space", "under_score", "abcdefghijklmnopqrstu"})
|
||||
void badCodeFormatIsRefused(String code) {
|
||||
assertThatThrownBy(() -> service.save(company(null, "X", code, "A"))).isInstanceOf(IllegalArgumentException.class);
|
||||
verify(companies, never()).save(any());
|
||||
@@ -129,9 +129,9 @@ class CompanyServiceTest {
|
||||
|
||||
Company renamed = service.renameCode(2L, "CVX");
|
||||
|
||||
assertThat(renamed.getCode()).isEqualTo("cvx");
|
||||
assertThat(a.getUsername()).isEqualTo("sales@cvx");
|
||||
assertThat(b.getUsername()).isEqualTo("odd@name@cvx");
|
||||
assertThat(renamed.getCode()).isEqualTo("CVX");
|
||||
assertThat(a.getUsername()).isEqualTo("sales@CVX");
|
||||
assertThat(b.getUsername()).isEqualTo("odd@name@CVX");
|
||||
verify(users).findByCompanyIdAndType(2L, UserType.A); // only that company, only admins
|
||||
verify(users).saveAll(List.of(a, b));
|
||||
// one transaction: company update and username rewrite share one @Transactional method
|
||||
@@ -142,7 +142,7 @@ class CompanyServiceTest {
|
||||
@Test
|
||||
void renameToTakenCodeIsRefusedAndRewritesNothing() {
|
||||
when(companies.findById(2L)).thenReturn(Optional.of(company(2L, "CV", "cv", "A")));
|
||||
when(companies.findByCodeIgnoreCase("ins")).thenReturn(Optional.of(company(1L, "INS", "ins", "A")));
|
||||
when(companies.findByCodeIgnoreCase("INS")).thenReturn(Optional.of(company(1L, "INS", "ins", "A")));
|
||||
|
||||
assertThatThrownBy(() -> service.renameCode(2L, "INS")).isInstanceOf(ResponseStatusException.class);
|
||||
verify(users, never()).saveAll(any());
|
||||
@@ -191,10 +191,10 @@ class CompanyServiceTest {
|
||||
void renameToOwnCodeIgnoringCaseIsAllowed() {
|
||||
Company cv = company(2L, "CV", "cv", "A");
|
||||
when(companies.findById(2L)).thenReturn(Optional.of(cv));
|
||||
when(companies.findByCodeIgnoreCase("cv")).thenReturn(Optional.of(cv));
|
||||
when(companies.findByCodeIgnoreCase("CV")).thenReturn(Optional.of(cv));
|
||||
when(users.findByCompanyIdAndType(2L, UserType.A)).thenReturn(List.of());
|
||||
|
||||
assertThat(service.renameCode(2L, "CV").getCode()).isEqualTo("cv");
|
||||
assertThat(service.renameCode(2L, "CV").getCode()).isEqualTo("CV");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user