From ffc270b567d14a2ef24ed6d9111af0fd0fbac00d Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 15 Mar 2021 18:04:41 -0700 Subject: [PATCH] fix: Incorrect calculation of subdomain when previously used more than once --- server/models/Team.js | 8 ++++++-- server/models/Team.test.js | 12 +++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/server/models/Team.js b/server/models/Team.js index 8d65d2002..a0002ff7c 100644 --- a/server/models/Team.js +++ b/server/models/Team.js @@ -133,9 +133,13 @@ const uploadAvatar = async (model) => { } }; -Team.prototype.provisionSubdomain = async function (subdomain, options = {}) { +Team.prototype.provisionSubdomain = async function ( + requestedSubdomain: string, + options = {} +) { if (this.subdomain) return this.subdomain; + let subdomain = requestedSubdomain; let append = 0; while (true) { try { @@ -143,7 +147,7 @@ Team.prototype.provisionSubdomain = async function (subdomain, options = {}) { break; } catch (err) { // subdomain was invalid or already used, try again - subdomain = `${subdomain}${++append}`; + subdomain = `${requestedSubdomain}${++append}`; } } diff --git a/server/models/Team.test.js b/server/models/Team.test.js index 2d33841d2..a25bc8b07 100644 --- a/server/models/Team.test.js +++ b/server/models/Team.test.js @@ -11,7 +11,7 @@ it("should set subdomain if available", async () => { expect(team.subdomain).toEqual("testy"); }); -it("should set subdomain with append if unavailable", async () => { +it("should set subdomain append if unavailable", async () => { await buildTeam({ subdomain: "myteam" }); const team = await buildTeam(); @@ -20,6 +20,16 @@ it("should set subdomain with append if unavailable", async () => { expect(team.subdomain).toEqual("myteam1"); }); +it("should increment subdomain append if unavailable", async () => { + await buildTeam({ subdomain: "myteam" }); + await buildTeam({ subdomain: "myteam1" }); + + const team = await buildTeam(); + const subdomain = await team.provisionSubdomain("myteam"); + expect(subdomain).toEqual("myteam2"); + expect(team.subdomain).toEqual("myteam2"); +}); + it("should do nothing if subdomain already set", async () => { const team = await buildTeam({ subdomain: "example" }); const subdomain = await team.provisionSubdomain("myteam");