Skip to content

Commit 789909d

Browse files
committed
Update tests to ensure access token is correctly passed
1 parent 4a41752 commit 789909d

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

src/App.test.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe("authentication request", () => {
3434

3535
describe("authentication return", () => {
3636
beforeAll(() => {
37-
window.location = { hash: "#access_token=TEST_TOKEN" }
37+
window.location = { hash: "#access_token=TEST_ACCESS_TOKEN" }
3838
})
3939

4040
test("renders playlist component on return from Spotify with auth token", () => {

src/components/PlaylistTable.test.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ afterEach(() => {
2727

2828
// Use a snapshot test to ensure exact component rendering
2929
test("playlist loading", async () => {
30-
const component = renderer.create(<PlaylistTable />)
30+
const component = renderer.create(<PlaylistTable accessToken="TEST_ACCESS_TOKEN" />)
3131
const instance = component.getInstance()
3232

3333
await waitFor(() => {
@@ -42,7 +42,7 @@ describe("single playlist exporting", () => {
4242
const saveAsMock = jest.spyOn(FileSaver, "saveAs")
4343
saveAsMock.mockImplementation(jest.fn())
4444

45-
render(<PlaylistTable />);
45+
render(<PlaylistTable accessToken="TEST_ACCESS_TOKEN" />);
4646

4747
await waitFor(() => {
4848
expect(screen.getByText(/Export All/)).toBeInTheDocument()
@@ -76,7 +76,7 @@ describe("single playlist exporting", () => {
7676
const saveAsMock = jest.spyOn(FileSaver, "saveAs")
7777
saveAsMock.mockImplementation(jest.fn())
7878

79-
render(<PlaylistTable />);
79+
render(<PlaylistTable accessToken="TEST_ACCESS_TOKEN" />);
8080

8181
await waitFor(() => {
8282
expect(screen.getByText(/Export All/)).toBeInTheDocument()
@@ -112,7 +112,7 @@ test("exporting of all playlist", async () => {
112112
const jsZipGenerateAsync = jest.spyOn(JSZip.prototype, 'generateAsync')
113113
jsZipGenerateAsync.mockResolvedValue("zip_content")
114114

115-
render(<PlaylistTable />);
115+
render(<PlaylistTable accessToken="TEST_ACCESS_TOKEN" />);
116116

117117
await waitFor(() => {
118118
expect(screen.getByText(/Export All/)).toBeInTheDocument()

src/mocks/handlers.jsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { rest } from 'msw'
22

33
export const handlers = [
44
rest.get('https://api.spotify.com/v1/me', (req, res, ctx) => {
5+
if (req.headers.get("Authorization") !== "Bearer TEST_ACCESS_TOKEN") {
6+
return res(ctx.status(401), ctx.json({ message: 'Not authorized' }))
7+
}
8+
59
return res(ctx.json(
610
{
711
"display_name" : "watsonbox",
@@ -22,6 +26,10 @@ export const handlers = [
2226
}),
2327

2428
rest.get('https://api.spotify.com/v1/users/watsonbox/tracks', (req, res, ctx) => {
29+
if (req.headers.get("Authorization") !== "Bearer TEST_ACCESS_TOKEN") {
30+
return res(ctx.status(401), ctx.json({ message: 'Not authorized' }))
31+
}
32+
2533
return res(ctx.json(
2634
{
2735
"href" : "https://api.spotify.com/v1/me/tracks?offset=0&limit=20",
@@ -108,6 +116,10 @@ export const handlers = [
108116

109117
// FIXME: Duplication of data
110118
rest.get('https://api.spotify.com/v1/me/tracks', (req, res, ctx) => {
119+
if (req.headers.get("Authorization") !== "Bearer TEST_ACCESS_TOKEN") {
120+
return res(ctx.status(401), ctx.json({ message: 'Not authorized' }))
121+
}
122+
111123
return res(ctx.json(
112124
{
113125
"href" : "https://api.spotify.com/v1/me/tracks?offset=0&limit=20",
@@ -193,6 +205,10 @@ export const handlers = [
193205
}),
194206

195207
rest.get('https://api.spotify.com/v1/users/watsonbox/playlists', (req, res, ctx) => {
208+
if (req.headers.get("Authorization") !== "Bearer TEST_ACCESS_TOKEN") {
209+
return res(ctx.status(401), ctx.json({ message: 'Not authorized' }))
210+
}
211+
196212
return res(ctx.json(
197213
{
198214
"href" : "https://api.spotify.com/v1/users/watsonbox/playlists?offset=0&limit=20",
@@ -240,6 +256,10 @@ export const handlers = [
240256
}),
241257

242258
rest.get('https://api.spotify.com/v1/playlists/4XOGDpHMrVoH33uJEwHWU5/tracks?offset=0&limit=10', (req, res, ctx) => {
259+
if (req.headers.get("Authorization") !== "Bearer TEST_ACCESS_TOKEN") {
260+
return res(ctx.status(401), ctx.json({ message: 'Not authorized' }))
261+
}
262+
243263
return res(ctx.json(
244264
{
245265
"href" : "https://api.spotify.com/v1/playlists/4XOGDpHMrVoH33uJEwHWU5/tracks?offset=0&limit=100",
@@ -430,6 +450,10 @@ export const handlers = [
430450

431451
export const nullTrackHandlers = [
432452
rest.get('https://api.spotify.com/v1/playlists/4XOGDpHMrVoH33uJEwHWU5/tracks?offset=0&limit=10', (req, res, ctx) => {
453+
if (req.headers.get("Authorization") !== "Bearer TEST_ACCESS_TOKEN") {
454+
return res(ctx.status(401), ctx.json({ message: 'Not authorized' }))
455+
}
456+
433457
return res(ctx.json(
434458
{
435459
"href" : "https://api.spotify.com/v1/playlists/4XOGDpHMrVoH33uJEwHWU5/tracks?offset=0&limit=100",

0 commit comments

Comments
 (0)