Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
56 changes: 52 additions & 4 deletions server/src/__tests__/normal/identity-policy/change-email.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,20 @@ afterEach(async () => {
await mockedKV.empty()
})

const sendCorrectChangeEmailCodeReq = async ({ code }: { code?: string } = {}) => {
const sendCorrectChangeEmailCodeReq = async ({
code, policy = Policy.ChangeEmail,
}: {
code?: string;
policy?: Policy;
} = {}) => {
await insertUsers(
db,
false,
)
const body = await prepareFollowUpBody(db)
const body = await prepareFollowUpBody(
db,
policy,
)
await markAuthCodeAsSecured(body.code)
const correctBody = {
...body,
Expand Down Expand Up @@ -190,7 +198,10 @@ describe(
db,
false,
)
const body = await prepareFollowUpBody(db)
const body = await prepareFollowUpBody(
db,
Policy.ChangeEmail,
)
const res = await app.request(
routeConfig.IdentityRoute.ChangeEmailCode,
{
Expand All @@ -209,6 +220,16 @@ describe(
},
)

test(
'should throw error if auth code has the wrong policy',
async () => {
const { res } = await sendCorrectChangeEmailCodeReq({ policy: Policy.SignInOrSignUp })

expect(res.status).toBe(400)
expect(await res.text()).toBe(messageConfig.RequestError.WrongAuthCode)
},
)

test(
'should throw error if feature not enabled',
async () => {
Expand Down Expand Up @@ -260,6 +281,7 @@ describe(
body: JSON.stringify({
...(await postAuthorizeBody(appRecord)),
credential,
policy: Policy.ChangeEmail,
}),
},
mock(db),
Expand Down Expand Up @@ -322,7 +344,10 @@ describe(
db,
false,
)
const body = await prepareFollowUpBody(db)
const body = await prepareFollowUpBody(
db,
Policy.ChangeEmail,
)
await markAuthCodeAsSecured(body.code)
const correctBody = {
...body,
Expand Down Expand Up @@ -372,6 +397,29 @@ describe(
},
)

test(
'should throw 400 if auth code has the wrong policy',
async () => {
await insertUsers(
db,
false,
)
const body = await prepareFollowUpBody(
db,
Policy.SignInOrSignUp,
)
await markAuthCodeAsSecured(body.code)

const { res } = await sendCorrectChangeEmailReq({
code: body.code,
verificationCode: '123456',
})

expect(res.status).toBe(400)
expect(await res.text()).toBe(messageConfig.RequestError.WrongAuthCode)
},
)

test(
'should throw 400 if use wrong verification code',
async () => {
Expand Down
58 changes: 55 additions & 3 deletions server/src/__tests__/normal/identity-policy/change-org.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,16 @@ const insertUserOrgs = (
`)
}

const sendCorrectGetChangeOrgRequest = async ({ code }: {
const sendCorrectGetChangeOrgRequest = async ({
code, policy = Policy.ChangeOrg,
}: {
code?: string;
policy?: Policy;
} = {}) => {
const body = await prepareFollowUpBody(db)
const body = await prepareFollowUpBody(
db,
policy,
)

// Update user's orgSlug
await db.prepare('UPDATE "user" SET "orgSlug" = ? WHERE id = ?').run(
Expand All @@ -75,11 +81,16 @@ const sendCorrectGetChangeOrgRequest = async ({ code }: {
const sendCorrectPostChangeOrgRequest = async ({
org,
code,
policy = Policy.ChangeOrg,
}: {
org: string;
code?: string;
policy?: Policy;
}) => {
const body = await prepareFollowUpBody(db)
const body = await prepareFollowUpBody(
db,
policy,
)

await markAuthCodeAsSecured(body.code)

Expand Down Expand Up @@ -211,6 +222,25 @@ describe(
},
)

test(
'should throw error if auth code has the wrong policy',
async () => {
process.env.ENABLE_ORG = true as unknown as string
global.process.env.BLOCKED_POLICIES = [] as unknown as string

await insertUsers(
db,
false,
)
const { res } = await sendCorrectGetChangeOrgRequest({ policy: Policy.SignInOrSignUp })
expect(res.status).toBe(400)
expect(await res.text()).toBe(messageConfig.RequestError.WrongAuthCode)

process.env.ENABLE_ORG = false as unknown as string
global.process.env.BLOCKED_POLICIES = [Policy.ChangeOrg] as unknown as string
},
)

test(
'should throw error if org feature is not enabled',
async () => {
Expand Down Expand Up @@ -423,6 +453,28 @@ describe(
},
)

test(
'should throw error if auth code has the wrong policy',
async () => {
process.env.ENABLE_ORG = true as unknown as string
global.process.env.BLOCKED_POLICIES = [] as unknown as string

await insertUsers(
db,
false,
)
const { res } = await sendCorrectPostChangeOrgRequest({
org: 'second-org',
policy: Policy.SignInOrSignUp,
})
expect(res.status).toBe(400)
expect(await res.text()).toBe(messageConfig.RequestError.WrongAuthCode)

process.env.ENABLE_ORG = false as unknown as string
global.process.env.BLOCKED_POLICIES = [Policy.ChangeOrg] as unknown as string
},
)

test(
'should throw error if org feature is not enabled',
async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,21 @@ afterEach(async () => {
await mockedKV.empty()
})

const sendCorrectChangePasswordReq = async ({ code }: {
const sendCorrectChangePasswordReq = async ({
code, policy = Policy.ChangePassword,
}: {
code?: string;
policy?: Policy;
} = {}) => {
await insertUsers(
db,
false,
)

const body = await prepareFollowUpBody(db)
const body = await prepareFollowUpBody(
db,
policy,
)
await markAuthCodeAsSecured(body.code)
const res = await app.request(
routeConfig.IdentityRoute.ChangePassword,
Expand Down Expand Up @@ -86,7 +92,10 @@ describe(
false,
)

const body = await prepareFollowUpBody(db)
const body = await prepareFollowUpBody(
db,
Policy.ChangePassword,
)
await markAuthCodeAsSecured(body.code)
const res = await app.request(
routeConfig.IdentityRoute.ChangePassword,
Expand All @@ -113,7 +122,10 @@ describe(
false,
)

const body = await prepareFollowUpBody(db)
const body = await prepareFollowUpBody(
db,
Policy.ChangePassword,
)

const authStore = await mockedKV.get(`AC-${body.code}`)
await mockedKV.put(
Expand Down Expand Up @@ -154,6 +166,15 @@ describe(
},
)

test(
'should throw 400 if auth code has the wrong policy',
async () => {
const { res } = await sendCorrectChangePasswordReq({ policy: Policy.SignInOrSignUp })
expect(res.status).toBe(400)
expect(await res.text()).toBe(messageConfig.RequestError.WrongAuthCode)
},
)

test(
'should throw error if feature not enabled',
async () => {
Expand Down
Loading
Loading