Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement uniform return interface for js functions #26

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8b44d71
Just trying to show how a uniformed API can help improve the use of t…
Oct 30, 2023
1021305
Merge branch 'main' into implement-uniform-return-interface-for-js-fu…
Oct 30, 2023
7af66cc
modify tests for signup
Oct 30, 2023
43ff120
fix with linter
Oct 30, 2023
407e979
modify all functions to use the unified response API
Nov 1, 2023
ee5bdc9
Merge branch 'main' into implement-uniform-return-interface-for-js-fu…
Nov 1, 2023
042a42b
bump version
Nov 3, 2023
3d56ab5
update clashing response name
Nov 3, 2023
8882921
update clashing response name
Nov 3, 2023
5340f1c
updated tests
Nov 3, 2023
9c47fc4
updated tests
Nov 3, 2023
e849bc6
update Response to genericResponse, to differentiate from the Http Re…
Nov 3, 2023
85d7d42
update Response to genericResponse, to differentiate from the Http Re…
Nov 3, 2023
e33b684
update test
Nov 3, 2023
27c5f83
update test
Nov 3, 2023
efcd2e9
bump
Nov 3, 2023
610c26e
change Response to GenericResponse in Api methods
Nov 3, 2023
5d423d4
change Response to GenericResponse in Api methods
Nov 3, 2023
251b6c4
include lib in github branch use
Nov 3, 2023
3dd6bc2
add method for resending verification email
Nov 10, 2023
7504c57
trying package version bump
Nov 10, 2023
489812f
lint fix copied over
Nov 24, 2023
1a8b492
Merge branch 'bypass' into implement-uniform-return-interface-for-js-…
Nov 24, 2023
ac79e5b
include lib in gitignore
Nov 26, 2023
ae3d10f
As per recommendations, make errors of type Error[], change response …
Nov 27, 2023
8d05e35
fix with linter
Nov 27, 2023
53dc974
remove ok property
Nov 27, 2023
c65fca4
remove lib folders from PR
Nov 27, 2023
c45a548
remove lib folder
Nov 27, 2023
0dadca0
Merge branch 'main' into implement-uniform-return-interface-for-js-fu…
lakhansamani Dec 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
node_modules
dist
lib
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add lib back to git ignore as that should not be part of the repo.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my bad , i removed it while testing the lib via github

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated now

package-lock.json
lib/*
package-lock.json
.idea
.history
75 changes: 38 additions & 37 deletions __test__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
const { Authorizer } = require('../lib')

const authRef = new Authorizer({
Expand All @@ -17,7 +18,8 @@ describe('signup success', () => {
password,
confirm_password: password,
})
expect(signupRes.message.length).not.toEqual(0)
expect(signupRes?.ok).toEqual(true)
expect(signupRes?.response?.message?.length).not.toEqual(0)
})

it('should verify email', async () => {
Expand Down Expand Up @@ -47,41 +49,40 @@ describe('signup success', () => {

const verifyEmailRes = await authRef.verifyEmail({ token: item.token })

expect(verifyEmailRes.access_token.length).not.toEqual(0)
expect(verifyEmailRes?.response?.access_token?.length).toBeGreaterThan(0)
})
})

describe('login failures', () => {
it('should throw password invalid error', async () => {
try {
await authRef.login({

const resp= await authRef.login({
email,
password: `${password}test`,
})
} catch (e) {
expect(e.message).toContain('bad user credentials')
}

expect(resp?.error?.message).toContain('bad user credentials')
})

it('should throw password invalid role', async () => {
try {
await authRef.login({
email,
password,
roles: ['admin'],
})
} catch (e) {
expect(e.message).toMatch('invalid role')
}

const resp = await authRef.login({
email,
password,
roles: ['admin'],
})
expect(resp.error?.message).toMatch('invalid role')
expect(resp.ok).toBeFalsy()
})

})

describe('forgot password success', () => {
it('should create forgot password request', async () => {
const forgotPasswordRes = await authRef.forgotPassword({
email,
})
expect(forgotPasswordRes.message.length).not.toEqual(0)
expect(forgotPasswordRes?.error?.message?.length).not.toEqual(0)
})

it('should reset password', async () => {
Expand Down Expand Up @@ -116,7 +117,7 @@ describe('forgot password success', () => {
password,
confirm_password: password,
})
expect(resetPasswordRes.message.length).not.toEqual(0)
expect(resetPasswordRes?.error?.message?.length).not.toEqual(0)
}
})
})
Expand All @@ -129,18 +130,18 @@ describe('login success', () => {
password,
scope: ['openid', 'profile', 'email', 'offline_access'],
})
expect(loginRes.access_token.length).not.toEqual(0)
expect(loginRes.refresh_token.length).not.toEqual(0)
expect(loginRes.expires_in).not.toEqual(0)
expect(loginRes.id_token.length).not.toEqual(0)
expect(loginRes?.response?.access_token.length).not.toEqual(0)
expect(loginRes?.response?.refresh_token.length).not.toEqual(0)
expect(loginRes?.response?.expires_in).not.toEqual(0)
expect(loginRes?.response?.id_token.length).not.toEqual(0)
})

it('should validate jwt token', async () => {
const validateRes = await authRef.validateJWTToken({
token_type: 'access_token',
token: loginRes.access_token,
token: loginRes?.response?.access_token,
})
expect(validateRes.is_valid).toEqual(true)
expect(validateRes?.response?.is_valid).toEqual(true)
})

it('should update profile successfully', async () => {
Expand All @@ -149,41 +150,41 @@ describe('login success', () => {
given_name: 'bob',
},
{
Authorization: `Bearer ${loginRes.access_token}`,
Authorization: `Bearer ${loginRes?.response?.access_token}`,
}
)
expect(updateProfileRes.message.length).not.toEqual(0)
expect(updateProfileRes?.error?.message?.length).not.toEqual(0)
})

it('should fetch profile successfully', async () => {
const profileRes = await authRef.getProfile({
Authorization: `Bearer ${loginRes.access_token}`,
Authorization: `Bearer ${loginRes?.response?.access_token}`,
})
expect(profileRes.given_name).toMatch('bob')
expect(profileRes?.response?.given_name).toMatch('bob')
})

it('should validate get token', async () => {
const tokenRes = await authRef.getToken({
grant_type: 'refresh_token',
refresh_token: loginRes.refresh_token,
refresh_token: loginRes?.response?.refresh_token,
})
expect(tokenRes.access_token.length).not.toEqual(0)
expect(tokenRes?.response?.access_token.length).not.toEqual(0)
})

it('should deactivate account', async () => {
console.log(`loginRes.access_token`, loginRes.access_token)
console.log(`loginRes?.response?.access_token`, loginRes?.response?.access_token)
const deactivateRes = await authRef.deactivateAccount({
Authorization: `Bearer ${loginRes.access_token}`,
Authorization: `Bearer ${loginRes?.response?.access_token}`,
})
expect(deactivateRes.message.length).not.toEqual(0)
expect(deactivateRes?.error?.message?.length).not.toEqual(0)
})

it('should throw error while accessing profile after deactivation', async () => {
await expect(
const resp=await
authRef.getProfile({
Authorization: `Bearer ${loginRes.access_token}`,
Authorization: `Bearer ${loginRes?.response?.access_token}`,
})
).rejects.toThrow('Error: unauthorized')
expect(resp?.error?.message).toEqual('Error: unauthorized')
})

it('should clear data', async () => {
Expand All @@ -210,7 +211,7 @@ describe('magic login success', () => {
email,
})

expect(magicLinkLoginRes.message.length).not.toEqual(0)
expect(magicLinkLoginRes?.error?.message?.length).not.toEqual(0)
})

it('should verify email', async () => {
Expand Down
Loading