Skip to content

Commit 0949578

Browse files
committed
Added: IT for recreateApiToken repository call
1 parent 7a74a01 commit 0949578

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

src/users/infra/repositories/UsersRepository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class UsersRepository extends ApiRepository implements IUsersRepository {
1616

1717
public async recreateApiToken(): Promise<string> {
1818
return this.doPost(`/${this.usersResourceName}/token/recreate`, {})
19-
.then((response) => response.data.data)
19+
.then((response) => response.data.data.message.split(' ').pop())
2020
.catch((error) => {
2121
throw error
2222
})

test/integration/users/UsersRepository.test.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@ import {
44
DataverseApiAuthMechanism
55
} from '../../../src/core/infra/repositories/ApiConfig'
66
import { TestConstants } from '../../testHelpers/TestConstants'
7-
import { ReadError } from '../../../src'
7+
import { ReadError, WriteError } from '../../../src'
8+
import { createApiTokenViaApi } from '../../testHelpers/users/apiTokenHelper'
89

910
describe('UsersRepository', () => {
1011
const sut: UsersRepository = new UsersRepository()
1112

13+
afterAll(async () => {
14+
ApiConfig.init(
15+
TestConstants.TEST_API_URL,
16+
DataverseApiAuthMechanism.API_KEY,
17+
process.env.TEST_API_KEY
18+
)
19+
})
20+
1221
describe('getCurrentAuthenticatedUser', () => {
1322
test('should return error when authentication is not valid', async () => {
1423
ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.API_KEY, 'invalidApiKey')
@@ -28,5 +37,19 @@ describe('UsersRepository', () => {
2837
})
2938
})
3039

31-
describe('recreateApiToken', () => {})
40+
describe('recreateApiToken', () => {
41+
test('should recreate API token when valid authentication is provided', async () => {
42+
const testApiToken = await createApiTokenViaApi()
43+
ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.API_KEY, testApiToken)
44+
const actualRecreatedApiToken = await sut.recreateApiToken()
45+
expect(actualRecreatedApiToken).not.toBe(testApiToken)
46+
})
47+
48+
test('should return error when authentication is not valid', async () => {
49+
ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.API_KEY, 'invalidApiKey')
50+
51+
const errorExpected: WriteError = new WriteError('[401] Bad API key')
52+
await expect(sut.recreateApiToken()).rejects.toThrow(errorExpected)
53+
})
54+
})
3255
})
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import axios from 'axios'
2+
import { TestConstants } from '../TestConstants'
3+
4+
const CREATE_USER_ENDPOINT = '/builtin-users?key=burrito&password=testuser'
5+
const API_TOKEN_USER_ENDPOINT = '/builtin-users/testuser/api-token'
6+
7+
export const createApiTokenViaApi = async (): Promise<string> => {
8+
try {
9+
await axios.post(
10+
`${TestConstants.TEST_API_URL}${CREATE_USER_ENDPOINT}`,
11+
JSON.stringify({
12+
userName: 'testuser',
13+
firstName: 'John',
14+
lastName: 'Doe',
15+
16+
}),
17+
{
18+
headers: {
19+
'Content-Type': 'application/json'
20+
}
21+
}
22+
)
23+
return axios
24+
.get(`${TestConstants.TEST_API_URL}${API_TOKEN_USER_ENDPOINT}?password=testuser`)
25+
.then((response) => response.data.data.message)
26+
} catch (error) {
27+
throw new Error(`Error while creating API token`)
28+
}
29+
}

0 commit comments

Comments
 (0)