Comparing version 0.0.5 to 0.0.6
@@ -8,9 +8,3 @@ | ||
var Session = function ( opts ) { | ||
if ( process.env.NODE_ENV !== 'production' ) { | ||
require( 'request-debug' )( request ); | ||
} | ||
this.options( opts ); | ||
}; | ||
@@ -17,0 +11,0 @@ |
{ | ||
"name": "wialon", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "NodeJS wrapper for Wialon Remote API", | ||
@@ -22,4 +22,3 @@ "main": "index.js", | ||
"extend": "^2.0.0", | ||
"request": "^2.54.0", | ||
"request-debug": "^0.1.1" | ||
"request": "^2.54.0" | ||
}, | ||
@@ -31,4 +30,5 @@ "devDependencies": { | ||
"jshint": "^2.6.3", | ||
"mocha": "^2.2.4" | ||
"mocha": "^2.2.4", | ||
"nock": "^1.6.0" | ||
} | ||
} |
@@ -5,8 +5,23 @@ /* jshint expr: true */ | ||
var nock = require( 'nock' ); | ||
var chai = require( 'chai' ); | ||
var expect = chai.expect; | ||
var session = require( '../' ).session(); | ||
var wialon = require( '../' ); | ||
describe( 'session', function() { | ||
// session object placeholder | ||
var session = {}; | ||
beforeEach( function () { | ||
// destroy session data | ||
wialon._session = {}; | ||
// create new session object | ||
session = wialon.session(); | ||
} ); | ||
it( 'should have a wialon object', function() { | ||
@@ -20,3 +35,4 @@ expect( session.wialon ).to.be.an( 'object' ); | ||
context( 'endpoint', function () { | ||
describe( 'endpoint', function () { | ||
it( 'should have a default url', function() { | ||
@@ -29,3 +45,11 @@ expect( session._options.url ).to.not.exist; | ||
context( 'start', function () { | ||
describe( 'start', function () { | ||
var credentials = { | ||
username : 'dummy', | ||
password : 'pass' | ||
}; | ||
it( 'should validate credentials', function ( done ) { | ||
@@ -38,6 +62,145 @@ session.start( {}, function ( err, data ) { | ||
} ); | ||
it( 'should make a login request', function ( done ) { | ||
var scope = nock( session.endpoint() ) | ||
.post( '?svc=core/login' ) | ||
.reply( 200, { eid : 'cfdf5e9dc900991577c10e3934b6c8f0' } ); | ||
session.start( credentials, function ( err, session ) { | ||
expect( err ).to.be.null; | ||
expect( scope.isDone() ).to.be.true; | ||
done(); | ||
} ); | ||
} ); | ||
context( 'when there is no callback', function () { | ||
it( 'should use the internal callback method', function ( done ) { | ||
var scope = nock( session.endpoint() ) | ||
.post( '?svc=core/login' ) | ||
.reply( 200, { eid : 'cfdf5e9dc900991577c10e3934b6c8f0' } ); | ||
session.start( credentials ); | ||
done(); | ||
} ); | ||
} ); | ||
context( 'when API credentials are incorrect', function () { | ||
it( 'should return an error object', function ( done ) { | ||
var scope = nock( session.endpoint() ) | ||
.post( '?svc=core/login' ) | ||
.reply( 200, { error : 8 } ); | ||
session.start( credentials, function ( err, sess ) { | ||
expect( err ).to.be.an.instanceof( Error ); | ||
expect( err.message ).to.be.string( 'API error: 8' ); | ||
done(); | ||
} ); | ||
} ); | ||
} ); | ||
} ); | ||
describe( 'request', function () { | ||
it( 'should call the endpoint url', function ( done ) { | ||
var svc = 'core/login'; | ||
var scope = nock( session.endpoint() ) | ||
.post( '?svc=' + svc ) | ||
.reply( 200, { error : 0 } ); | ||
session.request( svc, {}, function ( err, data ) { | ||
expect( scope.isDone() ).to.be.true; | ||
done(); | ||
} ); | ||
} ); | ||
it( 'should return API errors', function ( done ) { | ||
var svc = 'core/login'; | ||
var scope = nock( session.endpoint() ) | ||
.post( '?svc=' + svc ) | ||
.reply( 200, { error : 8 } ); | ||
session.request( svc, {}, function ( err, data ) { | ||
expect( err ).to.be.an.instanceof( Error ); | ||
expect( err.message ).to.be.string( 'API error: 8' ); | ||
done(); | ||
} ); | ||
} ); | ||
it( 'should validate the session when not making a login request', function ( done ) { | ||
session.request( 'dummy', {}, function ( err, data ) { | ||
expect( err ).to.be.an.instanceof( Error ); | ||
expect( err.message ).to.be.string( 'Invalid session' ); | ||
done(); | ||
} ); | ||
} ); | ||
context( 'when there is no callback', function () { | ||
it( 'should use the internal callback method', function ( done ) { | ||
var svc = 'core/login'; | ||
var scope = nock( session.endpoint() ) | ||
.post( '?svc=' + svc ) | ||
.reply( 200, { error : 0 } ); | ||
session.request( svc, {} ); | ||
done(); | ||
} ); | ||
} ); | ||
context( 'when failed to reach the API endpoint', function () { | ||
it( 'should return an error object', function ( done ) { | ||
var svc = 'core/login'; | ||
var scope = nock( session.endpoint() ) | ||
.post( '?svc=' + svc ) | ||
.replyWithError( 'API request failed' ); | ||
session.request( svc, {}, function ( err, data ) { | ||
expect( err ).to.be.an.instanceof( Error ); | ||
expect( err.message ).to.be.string( 'API request failed' ); | ||
// clear all nocks which seems to be needed after replyWithError() | ||
nock.cleanAll(); | ||
done(); | ||
} ); | ||
} ); | ||
} ); | ||
context( 'when API response is not JSON', function () { | ||
it( 'should return an error object', function ( done ) { | ||
var svc = 'core/login'; | ||
var scope = nock( session.endpoint() ) | ||
.post( '?svc=' + svc ) | ||
.reply( 200, 'not JSON' ); | ||
session.request( svc, {}, function ( err, data ) { | ||
expect( err ).to.be.an.instanceof( SyntaxError ); | ||
done(); | ||
} ); | ||
} ); | ||
} ); | ||
} ); | ||
describe( 'end', function () { | ||
it( 'should make a logout request', function ( done ) { | ||
var scope = nock( session.endpoint() ) | ||
.post( '?svc=core/logout' ) | ||
.reply( 200, { error : 0 } ); | ||
// session data mock | ||
session.wialon._session = { | ||
eid : 'cfdf5e9dc900991577c10e3934b6c8f0' | ||
}; | ||
session.end( function ( err, data ) { | ||
expect( err ).to.be.null; | ||
expect( scope.isDone() ).to.be.true; | ||
done(); | ||
} ); | ||
} ); | ||
} ); | ||
} ); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
23216
3
16
767
2
6
1
- Removedrequest-debug@^0.1.1
- Removedclone@0.1.19(transitive)
- Removedrequest-debug@0.1.1(transitive)