-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from mdrxtech/refrest-token
implement grant_type client_credentials and refresh_token to tokens e…
- Loading branch information
Showing
8 changed files
with
151 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,22 @@ | ||
import { IsNotEmpty, IsString } from 'class-validator'; | ||
import { IsIn, IsNotEmpty, IsString, ValidateIf } from 'class-validator'; | ||
|
||
export default class ApiTokenRequestDto { | ||
@IsNotEmpty() | ||
@IsIn(['client_credentials', 'refresh_token']) | ||
grant_type: 'client_credentials' | 'refresh_token'; | ||
|
||
@ValidateIf((o) => o.grant_type === 'refresh_token') | ||
@IsString() | ||
@IsNotEmpty() | ||
refresh_token?: string; | ||
|
||
@ValidateIf((o) => o.grant_type === 'client_credentials') | ||
@IsString() | ||
@IsNotEmpty() | ||
client_secret: string; | ||
client_secret?: string; | ||
|
||
@ValidateIf((o) => o.grant_type === 'client_credentials') | ||
@IsString() | ||
@IsNotEmpty() | ||
client_id: string; | ||
client_id?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { | ||
CORRECT_API_CLIENT, | ||
CORRECT_API_SECRET, | ||
E2eFixture, | ||
} from '../e2e.fixture'; | ||
import { mockTokens, mockTokensResponse } from '../mocks/tokens.mock'; | ||
|
||
describe('api/tokens (GET)', () => { | ||
const fixture = new E2eFixture(); | ||
|
||
beforeAll(async () => { | ||
await fixture.init(); | ||
}); | ||
|
||
describe('token', () => { | ||
it('returns bad request if client_credentials not supplied', async () => { | ||
return fixture | ||
.request() | ||
.post('/api/tokens') | ||
.send({ client_id: 'a', client_secret: 'b' }) | ||
.expect(400); | ||
}); | ||
|
||
it('returns bad request if client_credentials no secret', async () => { | ||
return fixture | ||
.request() | ||
.post('/api/tokens') | ||
.send({ | ||
grant_type: 'client_credentials', | ||
client_id: 'a', | ||
client_secret: '', | ||
}) | ||
.expect(400); | ||
}); | ||
|
||
it('returns bad request if client_credentials no id', async () => { | ||
return fixture | ||
.request() | ||
.post('/api/tokens') | ||
.send({ | ||
grant_type: 'client_credentials', | ||
client_id: '', | ||
client_secret: 'b', | ||
}) | ||
.expect(400); | ||
}); | ||
|
||
it('logs in with client credentials if grant type selected', async () => { | ||
return fixture | ||
.request() | ||
.post('/api/tokens') | ||
.send({ | ||
grant_type: 'client_credentials', | ||
client_id: CORRECT_API_CLIENT, | ||
client_secret: CORRECT_API_SECRET, | ||
}) | ||
.expect(201) | ||
.expect(mockTokensResponse); | ||
}); | ||
|
||
it('logs in with refresh token if grant type selected', async () => { | ||
return fixture | ||
.request() | ||
.post('/api/tokens') | ||
.send({ | ||
grant_type: 'refresh_token', | ||
refresh_token: mockTokens.RefreshToken, | ||
}) | ||
.expect(201) | ||
.expect(mockTokensResponse); | ||
}); | ||
|
||
it('returns bad request if refresh_token no token', async () => { | ||
return fixture | ||
.request() | ||
.post('/api/tokens') | ||
.send({ | ||
grant_type: 'refresh_token', | ||
refresh_token: '', | ||
}) | ||
.expect(400); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters