Skip to content

Commit 1944bed

Browse files
committed
tests: Add test cases for createToken method
1 parent 7f634c5 commit 1944bed

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/Authenticator.test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ jest.mock('./network/HTTP');
55

66
const email = 'knot@knot.com';
77
const password = '123qwe123qwe';
8+
const userToken = 'user-access-token';
89

910
describe('SDK Authenticator', () => {
1011
beforeEach(() => {
@@ -30,4 +31,61 @@ describe('SDK Authenticator', () => {
3031
}
3132
expect(error).toBe(postErr);
3233
});
34+
35+
test('should create a user token when connection is ok', async () => {
36+
const token = 'authorization-token';
37+
const http = new HTTP({ postRet: { token } });
38+
const auth = new Authenticator(http);
39+
const response = await auth.createToken(email, password, 'user');
40+
expect(response).toMatchObject({ token });
41+
expect(httpMocks.mockPost).toHaveBeenCalled();
42+
});
43+
44+
test('should fail to create a user token when something goes wrong', async () => {
45+
const postErr = 'error in post request';
46+
const http = new HTTP({ postErr });
47+
const auth = new Authenticator(http);
48+
let error;
49+
try {
50+
await auth.createToken(email, password, 'user');
51+
} catch (err) {
52+
error = err.message;
53+
}
54+
expect(error).toBe(postErr);
55+
});
56+
57+
test('should create a app token when connection is ok', async () => {
58+
const token = 'authorization-token';
59+
const http = new HTTP({ postRet: { token } });
60+
const auth = new Authenticator(http);
61+
const response = await auth.createToken(email, userToken, 'app', 0);
62+
expect(response).toMatchObject({ token });
63+
expect(httpMocks.mockPost).toHaveBeenCalled();
64+
});
65+
66+
test('should fail to create a app token when something goes wrong', async () => {
67+
const postErr = 'error in post request';
68+
const http = new HTTP({ postErr });
69+
const auth = new Authenticator(http);
70+
let error;
71+
try {
72+
await auth.createToken(email, userToken, 'app', 0);
73+
} catch (err) {
74+
error = err.message;
75+
}
76+
expect(error).toBe(postErr);
77+
});
78+
79+
test('should fail to create a token when type parameter is invalid', async () => {
80+
const postErr = 'error in post request';
81+
const http = new HTTP({ postErr });
82+
const auth = new Authenticator(http);
83+
let error;
84+
try {
85+
await auth.createToken(email, password, 'invalid-type');
86+
} catch (err) {
87+
error = err.message;
88+
}
89+
expect(error).toBe(postErr);
90+
});
3391
});

0 commit comments

Comments
 (0)