Skip to content

Commit

Permalink
test: fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed May 22, 2023
1 parent e4cb2c2 commit 5c5318e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
9 changes: 8 additions & 1 deletion src/cmap/auth/mongodb_oidc/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export abstract class ExpiringCacheEntry {
* Create a new expiring token entry.
*/
constructor(expiration: number) {
this.expiration = expiration;
this.expiration = this.expirationTime(expiration);
}
/**
* The entry is still valid if the expiration is more than
Expand All @@ -20,6 +20,13 @@ export abstract class ExpiringCacheEntry {
isValid() {
return this.expiration - Date.now() > EXPIRATION_BUFFER_MS;
}

/**
* Get an expiration time in milliseconds past epoch. Defaults to immediate.
*/
private expirationTime(expiresInSeconds: number): number {
return Date.now() + expiresInSeconds * 1000;
}
}

/**
Expand Down
9 changes: 1 addition & 8 deletions src/cmap/auth/mongodb_oidc/token_entry_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class TokenEntryCache extends AbstractCache<TokenEntry> {
const entry = new TokenEntry(
tokenResult,
serverInfo,
expirationTime(tokenResult.expiresInSeconds)
tokenResult.expiresInSeconds ?? DEFAULT_EXPIRATION_SECS
);
this.entries.set(this.cacheKey(address, username, callbackHash), entry);
return entry;
Expand Down Expand Up @@ -75,10 +75,3 @@ export class TokenEntryCache extends AbstractCache<TokenEntry> {
return this.hashedCacheKey(address, username, callbackHash);
}
}

/**
* Get an expiration time in milliseconds past epoch. Defaults to immediate.
*/
function expirationTime(expiresInSeconds?: number): number {
return Date.now() + (expiresInSeconds ?? DEFAULT_EXPIRATION_SECS) * 1000;
}
4 changes: 2 additions & 2 deletions test/unit/cmap/auth/mongodb_oidc/azure_token_cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('AzureTokenCache', function () {
});

it('adds the token result', function () {
expect(entry.tokenResult).to.deep.equal(tokenResultWithExpiration);
expect(entry.token).to.equal('test');
});

it('creates an expiration', function () {
Expand Down Expand Up @@ -64,7 +64,7 @@ describe('AzureTokenCache', function () {

context('when there is a matching entry', function () {
it('returns the entry', function () {
expect(cache.getEntry('audience1')).to.equal(tokenResultWithExpiration);
expect(cache.getEntry('audience1')?.token).to.equal('test');
});
});

Expand Down
4 changes: 2 additions & 2 deletions test/unit/cmap/auth/mongodb_oidc/token_entry_cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('TokenEntryCache', function () {
});

it('sets an immediate expiration', function () {
expect(entry.expiration).to.be.at.most(Date.now());
expect(entry?.expiration).to.be.at.most(Date.now());
});
});

Expand All @@ -67,7 +67,7 @@ describe('TokenEntryCache', function () {
});

it('sets an immediate expiration', function () {
expect(entry.expiration).to.be.at.most(Date.now());
expect(entry?.expiration).to.be.at.most(Date.now());
});
});
});
Expand Down

0 comments on commit 5c5318e

Please sign in to comment.