-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor amplify methods to single file
- Loading branch information
1 parent
198ebca
commit 4d24713
Showing
16 changed files
with
322 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,10 @@ import { LoginCognito } from "components"; | |
import { RouterWrappedComponent } from "utils/testing/setupJest"; | ||
|
||
const mockSignIn = jest.fn(); | ||
jest.mock("aws-amplify/auth", () => ({ | ||
signIn: (credentials: any) => mockSignIn(credentials), | ||
|
||
jest.mock("utils", () => ({ | ||
loginUser: (username: string, password: string) => | ||
mockSignIn(username, password), | ||
})); | ||
|
||
const mockUseNavigate = jest.fn(); | ||
|
@@ -30,12 +32,9 @@ describe("Test LoginCognito", () => { | |
const passwordInput = screen.getByLabelText("Password"); | ||
const submitButton = screen.getByRole("button"); | ||
await userEvent.type(emailInput, "[email protected]"); | ||
await userEvent.type(passwordInput, "p@$$w0rd"); //pragma: allowlist secret | ||
await userEvent.type(passwordInput, "test"); | ||
await userEvent.click(submitButton); | ||
expect(mockSignIn).toHaveBeenCalledWith({ | ||
username: "[email protected]", | ||
password: "p@$$w0rd", //pragma: allowlist secret | ||
}); | ||
expect(mockSignIn).toHaveBeenCalledWith("[email protected]", "test"); | ||
expect(mockUseNavigate).toHaveBeenCalledWith("/"); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { | ||
deleteApi, | ||
getApi, | ||
getRequestHeaders, | ||
getTokens, | ||
loginUser, | ||
logoutUser, | ||
postApi, | ||
putApi, | ||
refreshSession, | ||
} from "utils"; | ||
|
||
const mockResponse = { response: { body: { json: () => jest.fn() } } }; | ||
const mockDelete = jest.fn().mockImplementation(() => mockResponse); | ||
const mockGet = jest.fn().mockImplementation(() => mockResponse); | ||
const mockPost = jest.fn().mockImplementation(() => mockResponse); | ||
const mockPut = jest.fn().mockImplementation(() => mockResponse); | ||
const mockSession = jest.fn(); | ||
const mockSignIn = jest.fn(); | ||
const mockSignOut = jest.fn(); | ||
|
||
jest.mock("aws-amplify/api", () => ({ | ||
del: () => mockDelete(), | ||
get: () => mockGet(), | ||
post: () => mockPost(), | ||
put: () => mockPut(), | ||
})); | ||
|
||
jest.mock("aws-amplify/auth", () => ({ | ||
fetchAuthSession: () => mockSession(), | ||
signIn: () => mockSignIn(), | ||
signOut: () => mockSignOut(), | ||
})); | ||
|
||
describe("request", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe("getRequestHeaders()", () => { | ||
test("Logs error to console if Auth throws error", async () => { | ||
jest.spyOn(console, "log").mockImplementation(jest.fn()); | ||
const spy = jest.spyOn(console, "log"); | ||
|
||
mockSession.mockImplementation(() => { | ||
throw new Error(); | ||
}); | ||
|
||
await getRequestHeaders(); | ||
|
||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
|
||
test("Returns token if current idToken exists", async () => { | ||
mockSession.mockResolvedValue({ | ||
tokens: { | ||
idToken: { | ||
toString: () => "stringToken", | ||
}, | ||
}, | ||
}); | ||
|
||
const result = await getRequestHeaders(); | ||
|
||
expect(result).toStrictEqual({ "x-api-key": "stringToken" }); | ||
}); | ||
}); | ||
|
||
test("getTokens()", async () => { | ||
await getTokens(); | ||
expect(mockSession).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test("loginUser()", async () => { | ||
await loginUser("[email protected]", "test"); | ||
expect(mockSignIn).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test("logoutUser()", async () => { | ||
await logoutUser(); | ||
expect(mockSignOut).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test("refreshSession()", async () => { | ||
await refreshSession(); | ||
expect(mockSession).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test("deleteApi()", async () => { | ||
await deleteApi("/"); | ||
expect(mockDelete).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test("getApi()", async () => { | ||
await getApi<string>("/"); | ||
expect(mockGet).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test("postApi()", async () => { | ||
await postApi<string>("/", { body: "" }); | ||
expect(mockPost).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test("putApi()", async () => { | ||
await putApi<string>("/"); | ||
expect(mockPut).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { del, get, post, put } from "aws-amplify/api"; | ||
import { | ||
AuthTokens, | ||
fetchAuthSession, | ||
signIn, | ||
signOut, | ||
} from "aws-amplify/auth"; | ||
|
||
const apiName = "mcr"; | ||
|
||
interface RequestHeaders { | ||
"x-api-key": string | undefined; | ||
} | ||
|
||
export const getRequestHeaders = async (): Promise< | ||
RequestHeaders | undefined | ||
> => { | ||
try { | ||
const tokens = await getTokens(); | ||
const headers = { | ||
"x-api-key": tokens?.idToken?.toString(), | ||
}; | ||
|
||
return headers; | ||
} catch (error) { | ||
console.log(error); //eslint-disable-line no-console | ||
return; | ||
} | ||
}; | ||
|
||
export const getTokens = async (): Promise<AuthTokens | undefined> => { | ||
return (await fetchAuthSession()).tokens; | ||
}; | ||
|
||
export const loginUser = async ( | ||
username: string, | ||
password: string | ||
): Promise<void> => { | ||
await signIn({ username, password }); | ||
}; | ||
|
||
export const logoutUser = async (): Promise<void> => { | ||
await signOut(); | ||
}; | ||
|
||
export const refreshSession = async (): Promise<void> => { | ||
await fetchAuthSession({ forceRefresh: true }); | ||
}; | ||
|
||
export const deleteApi = async (path: string, opts?: any): Promise<void> => { | ||
const requestHeaders = await getRequestHeaders(); | ||
const options = { | ||
headers: { ...requestHeaders }, | ||
...opts, | ||
}; | ||
await del({ | ||
apiName, | ||
path, | ||
options, | ||
}).response; | ||
}; | ||
|
||
export const getApi = async <T>(path: string, opts?: any): Promise<T> => { | ||
const requestHeaders = await getRequestHeaders(); | ||
const options = { | ||
headers: { ...requestHeaders }, | ||
...opts, | ||
}; | ||
const { body } = await get({ | ||
apiName, | ||
path, | ||
options, | ||
}).response; | ||
|
||
return (await body.json()) as T; | ||
}; | ||
|
||
export const postApi = async <T>(path: string, opts?: any): Promise<T> => { | ||
const requestHeaders = await getRequestHeaders(); | ||
const options = { | ||
headers: { ...requestHeaders }, | ||
...opts, | ||
}; | ||
const { body } = await post({ | ||
apiName, | ||
path, | ||
options, | ||
}).response; | ||
|
||
return (await body.json()) as T; | ||
}; | ||
|
||
export const putApi = async <T>(path: string, opts?: any): Promise<T> => { | ||
const requestHeaders = await getRequestHeaders(); | ||
const options = { | ||
headers: { ...requestHeaders }, | ||
...opts, | ||
}; | ||
const { body } = await put({ | ||
apiName, | ||
path, | ||
options, | ||
}).response; | ||
|
||
return (await body.json()) as T; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,25 @@ | ||
import { get, post, del } from "aws-amplify/api"; | ||
import { getRequestHeaders } from "./getRequestHeaders"; | ||
import { AdminBannerData } from "types/banners"; | ||
import { updateTimeout } from "utils"; | ||
|
||
const apiName = "mcr"; | ||
import { deleteApi, getApi, postApi, updateTimeout } from "utils"; | ||
|
||
async function getBanner(bannerKey: string) { | ||
const requestHeaders = await getRequestHeaders(); | ||
const options = { | ||
headers: { ...requestHeaders }, | ||
}; | ||
const path = `/banners/${bannerKey}`; | ||
|
||
updateTimeout(); | ||
const { body } = await get({ | ||
apiName, | ||
path, | ||
options, | ||
}).response; | ||
return (await body.json()) as unknown as { Item: AdminBannerData }; | ||
return getApi<AdminBannerData>(path); | ||
} | ||
|
||
async function writeBanner(bannerData: AdminBannerData) { | ||
const requestHeaders = await getRequestHeaders(); | ||
const options = { | ||
headers: { ...requestHeaders }, | ||
body: bannerData, | ||
}; | ||
const path = `/banners/${bannerData.key}`; | ||
|
||
updateTimeout(); | ||
await post({ | ||
apiName, | ||
path, | ||
options, | ||
}).response; | ||
return postApi<AdminBannerData>(path, options); | ||
} | ||
|
||
async function deleteBanner(bannerKey: string) { | ||
const requestHeaders = await getRequestHeaders(); | ||
const options = { | ||
headers: { ...requestHeaders }, | ||
}; | ||
const path = `/banners/${bannerKey}`; | ||
|
||
updateTimeout(); | ||
await del({ | ||
apiName, | ||
path, | ||
options, | ||
}).response; | ||
return deleteApi(path); | ||
} | ||
|
||
export { getBanner, writeBanner, deleteBanner }; |
Oops, something went wrong.