diff --git a/test/test.jwt.js b/test/test.jwt.js index 6ae7cf4..ed58dbe 100644 --- a/test/test.jwt.js +++ b/test/test.jwt.js @@ -20,7 +20,6 @@ var assert = require('assert'); var googleAuth = require('../lib/auth/googleauth.js'); var nock = require('nock'); var fs = require('fs'); -var LoginTicket = require('../lib/auth/loginticket.js'); nock.disableNetConnect(); @@ -44,28 +43,11 @@ describe('Initial credentials', function() { var jwt = new auth.JWT(); assert.equal('jwt-placeholder', jwt.credentials.refresh_token); }); + }); describe('JWT auth client', function() { - it('should return null userId even if no payload', function() { - var ticket = new LoginTicket(null, null); - assert.equal(ticket.getUserId(), null); - }); - - it('should return envelope', function() { - var ticket = new LoginTicket('myenvelope'); - assert.equal(ticket.getEnvelope(), 'myenvelope'); - }); - - it('should return attributes from getAttributes', function() { - var ticket = new LoginTicket('myenvelope', 'mypayload'); - assert.deepEqual(ticket.getAttributes(), { - envelope: 'myenvelope', - payload: 'mypayload' - }); - }); - it('should get an initial access token', function(done) { var auth = new googleAuth(); var jwt = new auth.JWT( @@ -251,11 +233,17 @@ describe('JWT auth client', function() { done(); }); }); + }); describe('.createScoped', function() { + // set up the auth module. + var auth; + beforeEach(function() { + auth = new googleAuth(); + }); + it('should clone stuff', function() { - var auth = new googleAuth(); var jwt = new auth.JWT( 'foo@serviceaccount.com', '/path/to/key.pem', @@ -272,7 +260,6 @@ describe('.createScoped', function() { }); it('should handle string scope', function() { - var auth = new googleAuth(); var jwt = new auth.JWT( 'foo@serviceaccount.com', '/path/to/key.pem', @@ -285,7 +272,6 @@ describe('.createScoped', function() { }); it('should handle array scope', function() { - var auth = new googleAuth(); var jwt = new auth.JWT( 'foo@serviceaccount.com', '/path/to/key.pem', @@ -301,7 +287,6 @@ describe('.createScoped', function() { }); it('should handle null scope', function() { - var auth = new googleAuth(); var jwt = new auth.JWT( 'foo@serviceaccount.com', '/path/to/key.pem', @@ -314,7 +299,6 @@ describe('.createScoped', function() { }); it('should set scope when scope was null', function() { - var auth = new googleAuth(); var jwt = new auth.JWT( 'foo@serviceaccount.com', '/path/to/key.pem', @@ -327,7 +311,6 @@ describe('.createScoped', function() { }); it('should handle nulls', function() { - var auth = new googleAuth(); var jwt = new auth.JWT(); var clone = jwt.createScoped('hi'); @@ -339,7 +322,6 @@ describe('.createScoped', function() { }); it('should not return the original instance', function() { - var auth = new googleAuth(); var jwt = new auth.JWT( 'foo@serviceaccount.com', '/path/to/key.pem', @@ -350,11 +332,17 @@ describe('.createScoped', function() { var clone = jwt.createScoped('hi'); assert.notEqual(jwt, clone); }); + }); describe('.createScopedRequired', function() { + // set up the auth module. + var auth; + beforeEach(function() { + auth = new googleAuth(); + }); + it('should return true when scopes is null', function () { - var auth = new googleAuth(); var jwt = new auth.JWT( 'foo@serviceaccount.com', '/path/to/key.pem', @@ -366,7 +354,6 @@ describe('.createScopedRequired', function() { }); it('should return true when scopes is an empty array', function () { - var auth = new googleAuth(); var jwt = new auth.JWT( 'foo@serviceaccount.com', '/path/to/key.pem', @@ -378,7 +365,6 @@ describe('.createScopedRequired', function() { }); it('should return true when scopes is an empty string', function () { - var auth = new googleAuth(); var jwt = new auth.JWT( 'foo@serviceaccount.com', '/path/to/key.pem', @@ -390,7 +376,6 @@ describe('.createScopedRequired', function() { }); it('should return false when scopes is a filled-in string', function () { - var auth = new googleAuth(); var jwt = new auth.JWT( 'foo@serviceaccount.com', '/path/to/key.pem', @@ -429,10 +414,15 @@ describe('.createScopedRequired', function() { }); describe('.fromJson', function () { + // set up the test json and the jwt instance being tested. + var jwt, json; + beforeEach(function() { + json = createJSON(); + var auth = new googleAuth(); + jwt = new auth.JWT(); + }); it('should error on null json', function (done) { - var auth = new googleAuth(); - var jwt = new auth.JWT(); jwt.fromJSON(null, function (err) { assert.equal(true, err instanceof Error); done(); @@ -440,8 +430,6 @@ describe('.fromJson', function () { }); it('should error on empty json', function (done) { - var auth = new googleAuth(); - var jwt = new auth.JWT(); jwt.fromJSON({}, function (err) { assert.equal(true, err instanceof Error); done(); @@ -449,11 +437,8 @@ describe('.fromJson', function () { }); it('should error on missing client_email', function (done) { - var json = createJSON(); delete json.client_email; - var auth = new googleAuth(); - var jwt = new auth.JWT(); jwt.fromJSON(json, function (err) { assert.equal(true, err instanceof Error); done(); @@ -461,11 +446,8 @@ describe('.fromJson', function () { }); it('should error on missing private_key', function (done) { - var json = createJSON(); delete json.private_key; - var auth = new googleAuth(); - var jwt = new auth.JWT(); jwt.fromJSON(json, function (err) { assert.equal(true, err instanceof Error); done(); @@ -473,9 +455,6 @@ describe('.fromJson', function () { }); it('should create JWT with client_email', function (done) { - var json = createJSON(); - var auth = new googleAuth(); - var jwt = new auth.JWT(); jwt.fromJSON(json, function (err) { assert.equal(null, err); assert.equal(json.client_email, jwt.email); @@ -484,9 +463,6 @@ describe('.fromJson', function () { }); it('should create JWT with private_key', function (done) { - var json = createJSON(); - var auth = new googleAuth(); - var jwt = new auth.JWT(); jwt.fromJSON(json, function (err) { assert.equal(null, err); assert.equal(json.private_key, jwt.key); @@ -495,9 +471,6 @@ describe('.fromJson', function () { }); it('should create JWT with null scopes', function (done) { - var json = createJSON(); - var auth = new googleAuth(); - var jwt = new auth.JWT(); jwt.fromJSON(json, function (err) { assert.equal(null, err); assert.equal(null, jwt.scopes); @@ -506,9 +479,6 @@ describe('.fromJson', function () { }); it('should create JWT with null subject', function (done) { - var json = createJSON(); - var auth = new googleAuth(); - var jwt = new auth.JWT(); jwt.fromJSON(json, function (err) { assert.equal(null, err); assert.equal(null, jwt.subject); @@ -517,22 +487,24 @@ describe('.fromJson', function () { }); it('should create JWT with null keyFile', function (done) { - var json = createJSON(); - var auth = new googleAuth(); - var jwt = new auth.JWT(); jwt.fromJSON(json, function (err) { assert.equal(null, err); assert.equal(null, jwt.keyFile); done(); }); }); + }); describe('.fromStream', function () { + // set up the jwt instance being tested. + var jwt; + beforeEach(function() { + var auth = new googleAuth(); + jwt = new auth.JWT(); + }); it('should error on null stream', function (done) { - var auth = new googleAuth(); - var jwt = new auth.JWT(); jwt.fromStream(null, function (err) { assert.equal(true, err instanceof Error); done(); @@ -548,8 +520,6 @@ describe('.fromStream', function () { var stream = fs.createReadStream('./test/fixtures/private.json'); // And pass it into the fromStream method. - var auth = new googleAuth(); - var jwt = new auth.JWT(); jwt.fromStream(stream, function (err) { assert.equal(null, err); @@ -563,4 +533,5 @@ describe('.fromStream', function () { done(); }); }); + }); diff --git a/test/test.loginticket.js b/test/test.loginticket.js new file mode 100644 index 0000000..e859085 --- /dev/null +++ b/test/test.loginticket.js @@ -0,0 +1,43 @@ +/** + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +var assert = require('assert'); +var LoginTicket = require('../lib/auth/loginticket.js'); + +describe('LoginTicket', function() { + + it('should return null userId even if no payload', function() { + var ticket = new LoginTicket(null, null); + assert.equal(ticket.getUserId(), null); + }); + + it('should return envelope', function() { + var ticket = new LoginTicket('myenvelope'); + assert.equal(ticket.getEnvelope(), 'myenvelope'); + }); + + it('should return attributes from getAttributes', function() { + var ticket = new LoginTicket('myenvelope', 'mypayload'); + assert.deepEqual(ticket.getAttributes(), { + envelope: 'myenvelope', + payload: 'mypayload' + }); + + }); + +});