-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from ASparton/feat/43-add-number-of-articles-t…
…o-return feat: min articles count per feed and authentication taken into account on /api/articles
- Loading branch information
Showing
8 changed files
with
164 additions
and
34 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 |
---|---|---|
@@ -1,50 +1,97 @@ | ||
import HttpStatusCode from '#types/HttpStatusCode'; | ||
import { Article } from '@prisma/client'; | ||
import { | ||
deleteAllFeeds, | ||
deleteAllUsers, | ||
exampleArticles, | ||
populateArticles, | ||
populateFeeds, | ||
} from 'prisma/seedingOperatons'; | ||
import { database } from 'src/lucia'; | ||
import request from 'supertest'; | ||
import app from '../../src/app'; | ||
import HttpStatusCode from '#types/HttpStatusCode'; | ||
import ApiErrors from '~apiErrors'; | ||
|
||
let authToken = ''; | ||
|
||
describe('Articles router tests', () => { | ||
beforeAll(async () => { | ||
await deleteAllUsers(database); | ||
}); | ||
|
||
describe('GET all articles', () => { | ||
beforeAll(async () => { | ||
await populateFeeds(database); | ||
await populateArticles(database); | ||
const res = await request(app).post('/api/users/register').send({ | ||
email: '[email protected]', | ||
password: 'mySecretPassword', | ||
username: 'Alexis', | ||
}); | ||
authToken = res.body.token; | ||
}); | ||
|
||
test('GET all articles without keywords', async () => { | ||
test('GET all articles without keywords unauthenticated', async () => { | ||
const res = await request(app).get('/api/articles'); | ||
expect(res.statusCode).toStrictEqual(HttpStatusCode.OK_200); | ||
for (const expectedArticle of exampleArticles.filter( | ||
(article) => article.id !== 5 | ||
)) { | ||
expect( | ||
(res.body as Article[]).find( | ||
(receivedArticle) => receivedArticle.id === expectedArticle.id | ||
) | ||
).toBeTruthy(); | ||
} | ||
}); | ||
|
||
test('GET all articles with keywords unauthenticated', async () => { | ||
const res = await request(app).get('/api/articles?keyworkds=bitcoin'); | ||
expect(res.statusCode).toStrictEqual(HttpStatusCode.OK_200); | ||
for (const expectedArticle of exampleArticles.filter( | ||
(article) => article.id !== 5 | ||
)) { | ||
expect( | ||
(res.body as Article[]).find( | ||
(receivedArticle) => receivedArticle.id === expectedArticle.id | ||
) | ||
).toBeTruthy(); | ||
} | ||
}); | ||
|
||
test('GET all articles without keywords', async () => { | ||
const res = await request(app) | ||
.get('/api/articles') | ||
.set('Authorization', `Bearer ${authToken}`); | ||
expect(res.statusCode).toStrictEqual(HttpStatusCode.OK_200); | ||
expect(JSON.parse(JSON.stringify(res.body))).toStrictEqual( | ||
JSON.parse(JSON.stringify(exampleArticles)) | ||
); | ||
}); | ||
|
||
test('GET all articles with ignored params', async () => { | ||
const res = await request(app).get( | ||
'/api/articles?date=2023-10-10&orderBy=desc' | ||
); | ||
const res = await request(app) | ||
.get('/api/articles?date=2023-10-10&orderBy=desc') | ||
.set('Authorization', `Bearer ${authToken}`); | ||
expect(res.statusCode).toStrictEqual(HttpStatusCode.OK_200); | ||
expect(JSON.parse(JSON.stringify(res.body))).toStrictEqual( | ||
JSON.parse(JSON.stringify(exampleArticles)) | ||
); | ||
}); | ||
|
||
test('GET all articles with empty keywords', async () => { | ||
const res = await request(app).get('/api/articles?keywords='); | ||
const res = await request(app) | ||
.get('/api/articles?keywords=') | ||
.set('Authorization', `Bearer ${authToken}`); | ||
expect(res.statusCode).toStrictEqual(HttpStatusCode.OK_200); | ||
expect(JSON.parse(JSON.stringify(res.body))).toStrictEqual( | ||
JSON.parse(JSON.stringify(exampleArticles)) | ||
); | ||
}); | ||
|
||
test('GET all articles with one existing keyword', async () => { | ||
const res = await request(app).get('/api/articles?keywords=bitcoin'); | ||
const res = await request(app) | ||
.get('/api/articles?keywords=bitcoin') | ||
.set('Authorization', `Bearer ${authToken}`); | ||
expect(res.statusCode).toStrictEqual(HttpStatusCode.OK_200); | ||
expect(JSON.parse(JSON.stringify(res.body))).toStrictEqual( | ||
JSON.parse( | ||
|
@@ -58,9 +105,9 @@ describe('Articles router tests', () => { | |
}); | ||
|
||
test('GET all articles with multiple existing keywords', async () => { | ||
const res = await request(app).get( | ||
'/api/articles?keywords=bitcoin;wallet' | ||
); | ||
const res = await request(app) | ||
.get('/api/articles?keywords=bitcoin;wallet') | ||
.set('Authorization', `Bearer ${authToken}`); | ||
expect(res.statusCode).toStrictEqual(HttpStatusCode.OK_200); | ||
expect(JSON.parse(JSON.stringify(res.body))).toStrictEqual( | ||
JSON.parse( | ||
|
@@ -70,7 +117,9 @@ describe('Articles router tests', () => { | |
}); | ||
|
||
test('GET all articles with non existing keywords', async () => { | ||
const res = await request(app).get('/api/articles?keywords=alexismoins'); | ||
const res = await request(app) | ||
.get('/api/articles?keywords=alexismoins') | ||
.set('Authorization', `Bearer ${authToken}`); | ||
expect(res.statusCode).toStrictEqual(HttpStatusCode.OK_200); | ||
expect(res.body).toStrictEqual([]); | ||
}); | ||
|
2 changes: 2 additions & 0 deletions
2
back/prisma/migrations/20231121192553_min_articles_count_for_feed_added/migration.sql
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,2 @@ | ||
-- AlterTable | ||
ALTER TABLE "Feed" ADD COLUMN "min_articles_count" INTEGER NOT NULL DEFAULT 3; |
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
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
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