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

feat: DAH-2987 Reset password api #2461

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
99 changes: 99 additions & 0 deletions app/javascript/__tests__/__util__/accountUtils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import React from "react"
import UserContext, { ContextProps } from "../../authentication/context/UserContext"
import { User } from "../../authentication/user"

export const mockProfileStub: User = {
uid: "abc123",
email: "email@email.com",
created_at: new Date(),
updated_at: new Date(),
DOB: "1999-01-01",
firstName: "FirstName",
lastName: "LastName",
middleName: "MiddleName",
}

export const setupUserContext = ({
loggedIn,
setUpMockLocation = true,
saveProfileMock = jest.fn(),
mockProfile = mockProfileStub,
}: {
loggedIn: boolean
setUpMockLocation?: boolean
saveProfileMock?: jest.Mock
mockProfile?: ContextProps["profile"]
}) => {
const originalUseContext = React.useContext
const originalLocation = window.location
const mockContextValue: ContextProps = {
profile: loggedIn ? mockProfile : undefined,
signIn: jest.fn(),
signOut: jest.fn(),
timeOut: jest.fn(),
saveProfile: saveProfileMock || jest.fn(),
loading: false,
initialStateLoaded: true,
}

jest.spyOn(React, "useContext").mockImplementation((context) => {
if (context === UserContext) {
return mockContextValue
}
return originalUseContext(context)
})

if (!loggedIn && setUpMockLocation) {
// Allows for a redirect to the Sign In page
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (window as any)?.location
;(window as Window).location = {
...originalLocation,
href: "http://dahlia.com",
assign: jest.fn(),
replace: jest.fn(),
reload: jest.fn(),
toString: jest.fn(),
}
}

return originalUseContext
}

export const setupLocationAndRouteMock = () => {
const originalLocation = window.location

const customLocation = {
...originalLocation,
href: "http://dahlia.com",
assign: jest.fn(),
replace: jest.fn(),
reload: jest.fn(),
toString: jest.fn(),
}

Object.defineProperty(window, "location", {
configurable: true,
enumerable: true,
writable: true,
value: customLocation,
})

// Redefine the href setter to resolve relative URLs
Object.defineProperty(window.location, "href", {
configurable: true,
enumerable: true,
set: function (href: string) {
const base = "http://dahlia.com"
try {
const newUrl = new URL(href, base)
this._href = newUrl.href
} catch {
this._href = href
}
},
get: function () {
return this._href || "http://dahlia.com"
},
})
}
16 changes: 16 additions & 0 deletions app/javascript/__tests__/api/authApiService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
updatePassword,
getApplications,
deleteApplication,
resetPassword,
} from "../../api/authApiService"

jest.mock("axios")
Expand Down Expand Up @@ -100,6 +101,21 @@ describe("authApiService", () => {
})
})

describe("resetPassword", () => {
it("calls apiService put", async () => {
const url = "/api/v1/auth/password"
const newPassword = "abc123"
await resetPassword(newPassword)
expect(authenticatedPut).toHaveBeenCalledWith(
url,
expect.objectContaining({
password: newPassword,
password_confirmation: newPassword,
})
)
})
})

describe("updatePassword", () => {
it("calls apiService put", async () => {
const url = "/api/v1/auth/password"
Expand Down
Loading
Loading