forked from madhums/node-express-mongoose-demo
-
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.
- Loading branch information
Showing
6 changed files
with
214 additions
and
284 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
const mongoose = require('mongoose'); | ||
const test = require('ava'); | ||
const request = require('supertest'); | ||
const app = require('../server'); | ||
const cleanup = require('./helper').cleanup; | ||
const User = mongoose.model('User'); | ||
const Article = mongoose.model('Article'); | ||
const agent = request.agent(app); | ||
|
||
test.beforeEach(t => cleanup(t.end)); | ||
|
||
test('GET /articles - should respond with Content-Type text/html', t => { | ||
agent | ||
.get('/articles') | ||
.expect('Content-Type', /html/) | ||
.expect(200) | ||
.expect(/Articles/) | ||
.end(t.end); | ||
}); | ||
|
||
test('GET /articles/new - When not logged in - should redirect to /login', t => { | ||
agent | ||
.get('/articles/new') | ||
.expect('Content-Type', /plain/) | ||
.expect(302) | ||
.expect('Location', '/login') | ||
.expect(/Moved Temporarily/) | ||
.end(t.end); | ||
}); | ||
|
||
test.before(t => { | ||
// login the user | ||
agent | ||
.post('/users/session') | ||
.field('email', '[email protected]') | ||
.field('password', 'foobar') | ||
.end(t.end); | ||
}); | ||
|
||
test('GET /articles/new - When logged in - should respond with Content-Type text/html', t => { | ||
agent | ||
.get('/articles/new') | ||
.expect('Content-Type', /html/) | ||
.expect(200) | ||
.expect(/New Article/) | ||
.end(t.end); | ||
}); | ||
|
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,86 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
const mongoose = require('mongoose'); | ||
const test = require('ava'); | ||
const request = require('supertest'); | ||
const app = require('../server'); | ||
const cleanup = require('./helper').cleanup; | ||
const User = mongoose.model('User'); | ||
const Article = mongoose.model('Article'); | ||
const agent = request.agent(app); | ||
|
||
const _user = { | ||
email: '[email protected]', | ||
name: 'Foo bar', | ||
username: 'foobar', | ||
password: 'foobar' | ||
}; | ||
|
||
test.beforeEach(t => cleanup(t.end)); | ||
|
||
test('POST /articles - when not logged in - should redirect to /login', t => { | ||
request(app) | ||
.get('/articles/new') | ||
.expect('Content-Type', /plain/) | ||
.expect(302) | ||
.expect('Location', '/login') | ||
.expect(/Redirecting/) | ||
.end(t.end); | ||
}); | ||
|
||
// user login | ||
test.before(async t => { | ||
const user = new User(_user); | ||
await user.save(); | ||
|
||
agent | ||
.post('/users/session') | ||
.field('email', _user.email) | ||
.field('password', _user.password) | ||
.expect('Location', '/') | ||
.expect('Content-Type', /html/) | ||
.end(err => { | ||
console.log(err); | ||
t.ifError(err); | ||
t.end(err); | ||
}); | ||
}); | ||
|
||
test('POST /articles - invalid form - should respond with error', t => { | ||
agent | ||
.post('/articles') | ||
.field('title', '') | ||
.field('body', 'foo') | ||
.expect('Content-Type', /text/) | ||
.expect(302) | ||
.expect(/Article title cannot be blank/) | ||
.end(async (err, res) => { | ||
const count = await Article.count().exec(); | ||
t.ifError(err); | ||
t.same(count, 0, 'Count should be 0'); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
test('POST /articles - valid form - should redirect to the new article page', t => { | ||
agent | ||
.post('/articles') | ||
.field('title', 'foo') | ||
.field('body', 'bar') | ||
.expect('Content-Type', /plain/) | ||
.expect('Location', /\/articles\//) | ||
.expect(302) | ||
.expect(/Redirecting/) | ||
.end(async err => { | ||
const count = await Article.count().exec(); | ||
t.ifError(err); | ||
t.same(count, 1, 'Count should be 0'); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
test.afterEach(t => cleanup(t.end)); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.