Skip to content

Commit

Permalink
Merge pull request #58 from ASparton/feat/57-harvest-articles-of-feed…
Browse files Browse the repository at this point in the history
…-by-id

feat(harvest-feed-by-id): harvest articles from feed by id done
  • Loading branch information
ASparton authored Nov 25, 2023
2 parents 7a3e5fc + 0697593 commit f264211
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
68 changes: 67 additions & 1 deletion back/__tests__/controllers/articlesHarvest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Articles controller tests', () => {
registeredUserId = res.body.user.id;
});

test('POST articles harvest without unauthenticated', async () => {
test('POST articles harvest unauthenticated', async () => {
const res = await request(app).post('/api/articles/harvest');
expect(res.statusCode).toStrictEqual(HttpStatusCode.UNAUTHORIZED_401);
});
Expand Down Expand Up @@ -62,4 +62,70 @@ describe('Articles controller tests', () => {
await deleteAllUsers(database);
});
});

describe('POST articles harvest', () => {
beforeAll(async () => {
await populateFeeds(database);
const res = await request(app).post('/api/users/register').send({
email: 'alexis.moins@epitech.eu',
password: 'mySecretPassword',
username: 'Alexis',
});
authToken = res.body.token;
registeredUserId = res.body.user.id;
});

test('POST articles harvest by feed id unauthenticated', async () => {
const res = await request(app).post('/api/articles/harvest/1');
expect(res.statusCode).toStrictEqual(HttpStatusCode.UNAUTHORIZED_401);
});

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

test('POST articles harvest by feed id authenticated as admin', async () => {
await setRegisteredUserAdmin(database, registeredUserId);
const res = await request(app)
.post('/api/articles/harvest/1')
.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 () => {
await deleteAllFeeds(database);
await deleteAllUsers(database);
});

test('POST articles harvest by feed id with invalid id format', async () => {
await setRegisteredUserAdmin(database, registeredUserId);
const res = await request(app)
.post('/api/articles/harvest/cnbezjknckj')
.set('Authorization', `Bearer ${authToken}`);
expect(res.statusCode).toStrictEqual(HttpStatusCode.BAD_REQUEST_400);
});

test('POST articles harvest by feed id with unexsting feed id', async () => {
await setRegisteredUserAdmin(database, registeredUserId);
const res = await request(app)
.post('/api/articles/harvest/-1')
.set('Authorization', `Bearer ${authToken}`);
expect(res.statusCode).toStrictEqual(HttpStatusCode.NOT_FOUND_404);
});

afterAll(async () => {
await deleteAllFeeds(database);
await deleteAllUsers(database);
});
});
});
Loading

0 comments on commit f264211

Please sign in to comment.