Skip to content

Commit

Permalink
feat(articlesHarvest.test): test added for POST /api/articles/harvest
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Nov 25, 2023
1 parent 0b7bd09 commit 9982b64
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 32 deletions.
45 changes: 29 additions & 16 deletions back/__tests__/controllers/articlesHarvest.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import HttpStatusCode from '#types/HttpStatusCode';
import { Article } from '@prisma/client';
import {
deleteAllFeeds,
deleteAllUsers,
exampleArticles,
populateArticles,
populateFeeds,
setRegisteredUserAdmin,
} from 'prisma/seedingOperatons';
import { database } from 'src/lucia';
import request from 'supertest';
import app from '../../src/app';

let authToken = '';
let registeredUserId = '';

describe('Articles controller tests', () => {
beforeAll(async () => {
await deleteAllUsers(database);
await deleteAllFeeds(database);
});

describe('GET all articles', () => {
describe('POST articles harvest', () => {
beforeAll(async () => {
await populateFeeds(database);
const res = await request(app).post('/api/users/register').send({
Expand All @@ -28,20 +27,34 @@ describe('Articles controller tests', () => {
username: 'Alexis',
});
authToken = res.body.token;
registeredUserId = res.body.user.id;
});

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('POST articles harvest without unauthenticated', async () => {
const res = await request(app).post('/api/articles/harvest');
expect(res.statusCode).toStrictEqual(HttpStatusCode.UNAUTHORIZED_401);
});

test('POST articles harvest authenticated as non admin', async () => {
const res = await request(app)
.post('/api/articles/harvest')
.set('Authorization', `Bearer ${authToken}`);
expect(res.statusCode).toStrictEqual(HttpStatusCode.FORBIDDEN_403);
});

test('POST articles harvest authenticated as admin', async () => {
await setRegisteredUserAdmin(database, registeredUserId);
const res = await request(app)
.post('/api/articles/harvest')
.set('Authorization', `Bearer ${authToken}`);

// Assert response
expect(res.statusCode).toStrictEqual(HttpStatusCode.CREATED_201);
expect(res.body.count).toBeGreaterThan(0);

// Assert database insertions
const nbInsertedArticles = (await database.article.count()).valueOf();
expect(nbInsertedArticles).toStrictEqual(res.body.count);
});

afterAll(async () => {
Expand Down
22 changes: 6 additions & 16 deletions back/__tests__/controllers/feeds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
deleteAllUsers,
exampleFeeds,
populateFeeds,
setRegisteredUserAdmin,
} from 'prisma/seedingOperatons';
import { database } from 'src/lucia';
import request from 'supertest';
Expand Down Expand Up @@ -38,7 +39,7 @@ describe('Feeds controller tests', () => {
});

test('GET all feeds authenticated as admin', async () => {
await setRegisteredUserAdmin(registeredUserId);
await setRegisteredUserAdmin(database, registeredUserId);
const res = await request(app)
.get('/api/feeds')
.set('Authorization', `Bearer ${authToken}`);
Expand Down Expand Up @@ -86,7 +87,7 @@ describe('Feeds controller tests', () => {

test('POST feed authenticated as admin only url', async () => {
const expectedFeed = exampleFeeds[0];
await setRegisteredUserAdmin(registeredUserId);
await setRegisteredUserAdmin(database, registeredUserId);
const res = await request(app)
.post('/api/feeds')
.set('Authorization', `Bearer ${authToken}`)
Expand All @@ -113,7 +114,7 @@ describe('Feeds controller tests', () => {
test('POST feed authenticated as admin with url and minArticlesCount', async () => {
const expectedFeed = exampleFeeds[1];
expectedFeed.min_articles_count = 10;
await setRegisteredUserAdmin(registeredUserId);
await setRegisteredUserAdmin(database, registeredUserId);
const res = await request(app)
.post('/api/feeds')
.set('Authorization', `Bearer ${authToken}`)
Expand Down Expand Up @@ -222,7 +223,7 @@ describe('Feeds controller tests', () => {
test('PUT feed authenticated as admin', async () => {
const expectedFeed = exampleFeeds[0];
expectedFeed.min_articles_count = 10;
await setRegisteredUserAdmin(registeredUserId);
await setRegisteredUserAdmin(database, registeredUserId);
const res = await request(app)
.put('/api/feeds/1')
.set('Authorization', `Bearer ${authToken}`)
Expand Down Expand Up @@ -297,7 +298,7 @@ describe('Feeds controller tests', () => {
});

test('DELETE feed authenticated as admin', async () => {
await setRegisteredUserAdmin(registeredUserId);
await setRegisteredUserAdmin(database, registeredUserId);
const res = await request(app)
.delete('/api/feeds/1')
.set('Authorization', `Bearer ${authToken}`);
Expand Down Expand Up @@ -331,14 +332,3 @@ describe('Feeds controller tests', () => {
});
});
});

async function setRegisteredUserAdmin(userId: string) {
await database.user.update({
where: {
id: userId,
},
data: {
is_admin: true,
},
});
}
14 changes: 14 additions & 0 deletions back/prisma/seedingOperatons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,17 @@ export async function deleteAllArticles(
await prismaClient.article.deleteMany();
console.log('Article data deleted.');
}

export async function setRegisteredUserAdmin(
prismaClient: PrismaClient<Prisma.PrismaClientOptions, never, DefaultArgs>,
userId: string
) {
await prismaClient.user.update({
where: {
id: userId,
},
data: {
is_admin: true,
},
});
}

0 comments on commit 9982b64

Please sign in to comment.