Skip to content

Commit

Permalink
fix: misunderstanding node URL api
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Kolstad committed May 11, 2021
1 parent 69ded61 commit 171b518
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/lib/services/reset-token-service.ts
Expand Up @@ -28,15 +28,15 @@ export default class ResetTokenService {

private logger: Logger;

private readonly unleashBase: URL;
private readonly unleashBase: string;

constructor(
stores: IStores,
{ getLogger, server }: Pick<IUnleashConfig, 'getLogger' | 'server'>,
) {
this.store = stores.resetTokenStore;
this.logger = getLogger('/services/reset-token-service.ts');
this.unleashBase = new URL(server.unleashUrl);
this.unleashBase = server.unleashUrl;
}

async useAccessToken(token: IResetQuery): Promise<boolean> {
Expand Down Expand Up @@ -82,7 +82,7 @@ export default class ResetTokenService {
}

private getExistingInvitationUrl(token: IResetToken) {
return new URL(`/#/new-user?token=${token.token}`, this.unleashBase);
return new URL(`${this.unleashBase}/new-user?token=${token.token}`);
}

private async createResetUrl(
Expand All @@ -92,7 +92,7 @@ export default class ResetTokenService {
): Promise<URL> {
const token = await this.createToken(forUser, creator);
return Promise.resolve(
new URL(`${path}?token=${token.token}`, this.unleashBase),
new URL(`${this.unleashBase}${path}?token=${token.token}`),
);
}

Expand Down
29 changes: 27 additions & 2 deletions src/test/e2e/services/reset-token-service.e2e.test.ts
Expand Up @@ -65,6 +65,28 @@ test.serial('Should create a reset link', async t => {
);
});

test.serial(
'Should create a reset link with unleashUrl with context path',
async t => {
const localConfig = createTestConfig({
server: { unleashUrl: 'http://localhost:4242/my/sub/path' },
});
const resetToken: ResetTokenService = new ResetTokenService(
stores,
localConfig,
);

const url = await resetToken.createResetPasswordUrl(
userIdToCreateResetFor,
adminUser,
);
t.is(
url.toString().substring(0, url.toString().indexOf('=')),
`${localConfig.server.unleashUrl}/reset-password?token`,
);
},
);

test.serial('Should create a welcome link', async t => {
const url = await resetTokenService.createNewUserUrl(
userIdToCreateResetFor,
Expand Down Expand Up @@ -108,8 +130,11 @@ test.serial('Creating a new token should expire older tokens', async t => {
test.serial(
'Retrieving valid invitation links should retrieve an object with userid key and token value',
async t => {
await resetTokenService.createToken(userIdToCreateResetFor, adminUser);

const token = await resetTokenService.createToken(
userIdToCreateResetFor,
adminUser,
);
t.truthy(token);
const activeInvitations = await resetTokenService.getActiveInvitations();
t.true(Object.keys(activeInvitations).length === 1);
t.true(+Object.keys(activeInvitations)[0] === userIdToCreateResetFor);
Expand Down

0 comments on commit 171b518

Please sign in to comment.