Skip to content

Commit

Permalink
Updated tests. See #39
Browse files Browse the repository at this point in the history
  • Loading branch information
MKHenson committed Mar 27, 2017
1 parent 27e0a8f commit 664a1e7
Show file tree
Hide file tree
Showing 4 changed files with 291 additions and 428 deletions.
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if ( args.server === undefined || isNaN( parseInt( args.server ) ) ) {

const startup = require( '../dist/startup.js' );
const header = require( './tests/header.js' );

header.TestManager.get;

describe( 'Initializing tests', function() {

Expand Down
39 changes: 28 additions & 11 deletions test/tests/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,37 @@ class TestManager {
*/
constructor() {
TestManager._singleton = this;
this.initialized = false;
this.cookies = {
admin: ''
};

this.config = JSON.parse( fs.readFileSync( args.config ) );
this.serverConfig = this.config.servers[ parseInt( args.server ) ];
this.agent = test.httpAgent( "http://" + this.serverConfig.host + ":" + this.serverConfig.portHTTP );
}

post( url, json, who = 'admin' ) {
post( url, json, who = 'admin', code = 200 ) {
return new Promise(( resolve, reject ) => {
this.agent.post( url )
.set( 'Accept', 'application/json' )
.expect( 200 ).expect( 'Content-Type', /json/ )
.expect( code ).expect( 'Content-Type', /json/ )
.send( json )
.set( 'Cookie', this.cookies[ who ] || '' )
.end(( err, res ) => {
if ( err )
return reject( err );

return ( resolve( res ) );
} );
} );
}

put( url, json, who = 'admin', code = 200 ) {
return new Promise(( resolve, reject ) => {
this.agent.put( url )
.set( 'Accept', 'application/json' )
.expect( code ).expect( 'Content-Type', /json/ )
.send( json )
.set( 'Cookie', this.cookies[ who ] || '' )
.end(( err, res ) => {
Expand All @@ -34,11 +55,11 @@ class TestManager {
} );
}

delete( url, json, who = 'admin' ) {
delete( url, json, who = 'admin', code = 200 ) {
return new Promise(( resolve, reject ) => {
this.agent.delete( url )
.set( 'Accept', 'application/json' )
.expect( 200 ).expect( 'Content-Type', /json/ )
.expect( code ).expect( 'Content-Type', /json/ )
.send( json )
.set( 'Cookie', this.cookies[ who ] || '' )
.end(( err, res ) => {
Expand All @@ -50,11 +71,11 @@ class TestManager {
} );
}

get( url, who = 'admin' ) {
get( url, who = 'admin', code = 200 ) {
return new Promise(( resolve, reject ) => {
this.agent.get( url )
.set( 'Accept', 'application/json' )
.expect( 200 ).expect( 'Content-Type', /json/ )
.expect( code ).expect( 'Content-Type', /json/ )
.set( 'Cookie', this.cookies[ who ] || '' )
.end(( err, res ) => {
if ( err )
Expand All @@ -73,12 +94,8 @@ class TestManager {
* Initialize the manager
*/
async initialize() {

try {
const config = this.config = JSON.parse( fs.readFileSync( args.config ) );
const serverConfig = this.serverConfig = config.servers[ parseInt( args.server ) ];
this.agent = test.httpAgent( "http://" + serverConfig.host + ":" + serverConfig.portHTTP );

const config = this.config;
const resp = await this.post( '/auth/login', { username: config.adminUser.username, password: config.adminUser.password } );
this.cookies.admin = resp.headers[ "set-cookie" ][ 0 ].split( ";" )[ 0 ];
}
Expand Down
13 changes: 6 additions & 7 deletions test/tests/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ var header = require( './header.js' );
var lastPost = null;
var tempPost = null;
var numPosts = 0;
const manager = header.TestManager.get;

/**
* Tests all post related endpoints
*/
describe( 'Testing all post related endpoints', function() {
it( 'Fetched all posts', function( done ) {
header.modepressAgent
.get( '/api/posts' ).expect( 200 ).expect( 'Content-Type', /json/ )
.end( function( err, res ) {
test.bool( res.body.error ).isNotTrue()
.number( res.body.count );

manager.get( `/api/posts`, null )
.then( res => {
test.bool( res.body.error ).isNotTrue();
test.number( res.body.count );
numPosts = res.body.count;
done();
} );
} ).catch( err => done( err ) );
} )

it( 'Cannot create post when not logged in', function( done ) {
Expand Down
Loading

0 comments on commit 664a1e7

Please sign in to comment.