-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature[get user articles]: Enables registered user to get thier arti…
…cles Get All user authored articles they functionality [Finishes #168252582]
- Loading branch information
Adekoreday
committed
Sep 4, 2019
1 parent
5cdf538
commit 5fa3735
Showing
6 changed files
with
266 additions
and
81 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
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,74 @@ | ||
import chai, { expect } from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import sinon from 'sinon'; | ||
import app from '../../server'; | ||
import { getNewUser } from '../users/__mocks__'; | ||
import model from '../../server/database/models'; | ||
import { ArticleData4, ArticleData20 } from './__mocks__'; | ||
|
||
chai.use(chaiHttp); | ||
const { BASE_URL } = process.env; | ||
let userToken; | ||
const userSignUp = getNewUser(); | ||
const { Article } = model; | ||
before(async () => { | ||
await chai | ||
.request(app) | ||
.post(`${BASE_URL}/users/create`) | ||
.send({ ...userSignUp, confirmPassword: userSignUp.password }); | ||
const response = await chai | ||
.request(app) | ||
.post(`${BASE_URL}/sessions/create`) | ||
.send({ userLogin: userSignUp.email, password: userSignUp.password }); | ||
userToken = response.body.token; | ||
|
||
await chai | ||
.request(app) | ||
.post(`${BASE_URL}/articles/create`) | ||
.set('Authorization', userToken) | ||
.send(ArticleData4); | ||
await chai | ||
.request(app) | ||
.post(`${BASE_URL}/articles/create`) | ||
.set('Authorization', userToken) | ||
.send(ArticleData20); | ||
}); | ||
|
||
describe('get User Article Test', () => { | ||
context('when a user enters a valid token', () => { | ||
it('get all his article successfully', async () => { | ||
const response = await chai | ||
.request(app) | ||
.get(`${BASE_URL}/articles/user/articles`) | ||
.set('Authorization', userToken); | ||
expect(response.body.articles.total).to.equal(2); | ||
expect(response.status).to.equal(200); | ||
}); | ||
}); | ||
|
||
context('when a user enter an invalid token', () => { | ||
it('returns an error', async () => { | ||
const response = await chai | ||
.request(app) | ||
.get(`${BASE_URL}/articles/user/articles`); | ||
expect(response.status).to.equal(401); | ||
}); | ||
}); | ||
|
||
context( | ||
'when the get User Articles method encounters a internal error', | ||
() => { | ||
it('throws a server error', async () => { | ||
const userArticles = sinon | ||
.stub(Article, 'findAndCountAll') | ||
.callsFake(() => {}); | ||
const response = await chai | ||
.request(app) | ||
.get(`${BASE_URL}/articles/user/articles`) | ||
.set('Authorization', userToken); | ||
expect(response.status).to.equal(500); | ||
userArticles.restore(); | ||
}); | ||
} | ||
); | ||
}); |
Oops, something went wrong.