-
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 #50 from ASparton/feat/33-get-feeds-route
feat(getAllFeeds): get all feeds route done with only access admin
- Loading branch information
Showing
11 changed files
with
124 additions
and
5 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
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,67 @@ | ||
import HttpStatusCode from '#types/HttpStatusCode'; | ||
import { | ||
deleteAllFeeds, | ||
deleteAllUsers, | ||
exampleFeeds, | ||
populateFeeds, | ||
} from 'prisma/seedingOperatons'; | ||
import { database } from 'src/lucia'; | ||
import request from 'supertest'; | ||
import app from '../../src/app'; | ||
|
||
let authToken = ''; | ||
let registeredUserId = ''; | ||
|
||
describe('Feeds controller tests', () => { | ||
describe('GET all feeds', () => { | ||
beforeAll(async () => { | ||
await populateFeeds(database); | ||
const res = await request(app).post('/api/users/register').send({ | ||
email: '[email protected]', | ||
password: 'mySecretPassword', | ||
username: 'Alexis', | ||
}); | ||
authToken = res.body.token; | ||
registeredUserId = res.body.user.id; | ||
}); | ||
|
||
test('GET all feeds unauthenticated', async () => { | ||
const res = await request(app).get('/api/feeds'); | ||
expect(res.statusCode).toStrictEqual(HttpStatusCode.UNAUTHORIZED_401); | ||
}); | ||
|
||
test('GET all feeds authenticated as non admin', async () => { | ||
const res = await request(app) | ||
.get('/api/feeds') | ||
.set('Authorization', `Bearer ${authToken}`); | ||
expect(res.statusCode).toStrictEqual(HttpStatusCode.FORBIDDEN_403); | ||
}); | ||
|
||
test('GET all feeds authenticated as admin', async () => { | ||
await setRegisteredUserAdmin(registeredUserId); | ||
const res = await request(app) | ||
.get('/api/feeds') | ||
.set('Authorization', `Bearer ${authToken}`); | ||
expect(res.statusCode).toStrictEqual(HttpStatusCode.OK_200); | ||
expect(JSON.parse(JSON.stringify(res.body))).toStrictEqual( | ||
JSON.parse(JSON.stringify(exampleFeeds)) | ||
); | ||
}); | ||
|
||
afterAll(async () => { | ||
await deleteAllFeeds(database); | ||
await deleteAllUsers(database); | ||
}); | ||
}); | ||
}); | ||
|
||
async function setRegisteredUserAdmin(userId: string) { | ||
await database.user.update({ | ||
where: { | ||
id: userId, | ||
}, | ||
data: { | ||
is_admin: true, | ||
}, | ||
}); | ||
} |
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
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,11 @@ | ||
import express from 'express'; | ||
import HttpStatusCode from '#types/HttpStatusCode'; | ||
import { findAllFeeds } from '../database/feeds'; | ||
|
||
const controller = express.Router(); | ||
|
||
controller.get('/', async (req, res) => { | ||
return res.status(HttpStatusCode.OK_200).send(await findAllFeeds()); | ||
}); | ||
|
||
export default controller; |
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,6 @@ | ||
import { Feed } from '@prisma/client'; | ||
import { database } from '~lucia'; | ||
|
||
export async function findAllFeeds(): Promise<Feed[]> { | ||
return await database.feed.findMany(); | ||
} |
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