Skip to content
This repository was archived by the owner on Jan 5, 2022. It is now read-only.

Commit 1a17682

Browse files
committed
Separated client tests into modules, removed 1.0 tests, fixes #1
1 parent ce4ca27 commit 1a17682

12 files changed

Lines changed: 1069 additions & 991 deletions

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ node_js:
33
- '5.1.0'
44
install:
55
- 'npm install'
6-
- 'npm install -g grunt-cli'
76
- 'npm -g install mocha'
87
script:
9-
- 'mocha tests --bail --target=1.0'
108
- 'mocha tests --bail --target=2.1'
119
notifications:
1210
email:

helpers/config.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
'use strict'
22

3-
var _ = require('lodash')
3+
var util = require('util'),
4+
_ = require('lodash')
45

56
if (/mocha$/i.test(process.argv[1])) {
67
var target = _(_.last(process.argv)).startsWith('--target=') ? _.last(process.argv).replace(/--target=/, '') : '1.0'
7-
var config = require('../tests/config.test.json')[target] || require('../config.json')
8-
if (target) {
8+
var config = require('../tests/config.test.json')[target]
9+
if (config && target) {
910
config.target = target
11+
} else {
12+
throw new Error(util.format("Could not load target '%s' from /tests/config.test.json", target))
1013
}
1114
module.exports = config
1215
} else {

lib/client.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,16 @@ var UsergridClient = function(options) {
4242
set: function(options) {
4343
if (options instanceof UsergridAppAuth) {
4444
__appAuth = options
45-
} else if (options !== undefined) {
45+
} else if (typeof options !== "undefined") {
4646
__appAuth = new UsergridAppAuth(options)
4747
}
4848
}
4949
})
5050

51+
Object.defineProperty(self, 'test', {
52+
enumerable: false
53+
})
54+
5155
return self
5256
}
5357

@@ -115,28 +119,33 @@ UsergridClient.prototype = {
115119
var self = this
116120
callback = helpers.cb(callback || options)
117121

118-
options = (options instanceof UsergridAppAuth) ? options : self.appAuth || new UsergridAppAuth(options)
122+
var auth = _.first([options, self.appAuth, new UsergridAppAuth(options)].filter(function(auth) {
123+
return (auth instanceof UsergridAppAuth)
124+
}))
119125

120-
if (!(options instanceof UsergridAppAuth)) {
126+
if (!(auth instanceof UsergridAppAuth)) {
121127
throw new Error('App auth context was not defined when attempting to call .authenticateApp()')
122-
} else if (!options.clientId || !options.clientSecret) {
128+
} else if (!auth.clientId || !auth.clientSecret) {
123129
throw new Error('authenticateApp() failed because clientId or clientSecret are missing')
124130
}
125131

126-
options.type = 'token'
132+
auth.type = 'token'
127133

128134
request({
129-
uri: helpers.build.url(self, options),
135+
uri: helpers.build.url(self, auth),
130136
headers: helpers.userAgent,
131137
body: {
132138
grant_type: 'client_credentials',
133-
client_id: options.clientId,
134-
client_secret: options.clientSecret
139+
client_id: auth.clientId,
140+
client_secret: auth.clientSecret
135141
},
136142
method: 'POST',
137143
json: true
138144
}, function(error, response, body) {
139-
if (self.appAuth && response.statusCode === 200) {
145+
if (response.statusCode === 200) {
146+
if (!self.appAuth) {
147+
self.appAuth = auth
148+
}
140149
self.appAuth.token = body.access_token
141150
self.appAuth.expiry = helpers.time.expiry(body.expires_in)
142151
self.appAuth.tokenTtl = body.expires_in
@@ -157,7 +166,7 @@ UsergridClient.prototype = {
157166
}
158167

159168
options.type = 'token'
160-
options.client = self
169+
161170
request({
162171
uri: helpers.build.url(self, options),
163172
headers: helpers.userAgent,

lib/responseError.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ var UsergridResponseError = function(responseErrorObject) {
66
if (ok(responseErrorObject).has('error') === false) {
77
return
88
}
9-
var self = this
10-
self.name = responseErrorObject.error
11-
self.description = responseErrorObject.description
12-
self.exception = responseErrorObject.exception
13-
return self
9+
this.name = responseErrorObject.error
10+
this.description = responseErrorObject.description || responseErrorObject.error_description
11+
this.exception = responseErrorObject.exception
12+
return this
1413
}
1514

1615
module.exports = UsergridResponseError

tests/config.test.json

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
11
{
2-
"1.0": {
3-
"appId": "sandbox",
4-
"baseUrl": "https://api.usergrid.com",
5-
"clientId": "YXA6GXSAACS2EeOYd20aP4G6Lw",
6-
"clientSecret": "YXA66BeEvgNpJBwc4PAbvZZGTVS_SSw",
7-
"orgId": "brandon.apigee",
8-
"test": {
9-
"collection": "tests",
10-
"email": "authtest@test.com",
11-
"password": "P@ssw0rd",
12-
"username": "authtest"
13-
}
14-
},
152
"2.1": {
163
"appId": "sdksandbox",
174
"baseUrl": "https://api-connectors-prod.apigee.net/appservices",

tests/lib/client.auth.test.js

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
'use strict'
2+
3+
var should = require('should'),
4+
urljoin = require('url-join'),
5+
config = require('../../helpers').config,
6+
UsergridClient = require('../../lib/client'),
7+
UsergridEntity = require('../../lib/entity'),
8+
UsergridQuery = require('../../lib/query'),
9+
UsergridAuth = require('../../lib/auth'),
10+
UsergridAppAuth = require('../../lib/appAuth'),
11+
_ = require('lodash')
12+
13+
_.mixin(require('lodash-uuid'))
14+
15+
var _uuid,
16+
_slow = 500,
17+
_timeout = 4000
18+
19+
describe('authenticateApp()', function() {
20+
21+
this.slow(_slow)
22+
this.timeout(_timeout)
23+
24+
var response, token, client = new UsergridClient()
25+
before(function(done) {
26+
client.setAppAuth(config.clientId, config.clientSecret)
27+
client.authenticateApp(function(err, r, t) {
28+
response = r
29+
token = t
30+
done()
31+
})
32+
})
33+
34+
it('should fail when called without a clientId and clientSecret', function() {
35+
should(function() {
36+
var client = new UsergridClient()
37+
client.setAppAuth(undefined, undefined, 0)
38+
client.authenticateApp()
39+
}).throw()
40+
})
41+
42+
it('should authenticate by passing clientId and clientSecret in an object', function(done) {
43+
var isolatedClient = new UsergridClient()
44+
isolatedClient.authenticateApp(config, function(err, reponse, token) {
45+
isolatedClient.appAuth.should.have.property('token').equal(token)
46+
done()
47+
})
48+
})
49+
50+
it('should not set client.appAuth when authenticating with a bad clientId and clientSecret in an object', function(done) {
51+
var failClient = new UsergridClient()
52+
failClient.authenticateApp({
53+
clientId: 'BADCLIENTID',
54+
clientSecret: 'BADCLIENTSECRET'
55+
}, function(e, r, token) {
56+
e.should.containDeep({
57+
name: 'invalid_grant',
58+
description: 'invalid username or password'
59+
})
60+
should(token).be.undefined()
61+
should(failClient.appAuth).be.undefined()
62+
done()
63+
})
64+
})
65+
66+
it('should not set client.appAuth when authenticating with a bad UsergridAppAuth instance (using an object)', function(done) {
67+
var failClient = new UsergridClient()
68+
failClient.authenticateApp(new UsergridAppAuth({
69+
clientId: 'BADCLIENTID',
70+
clientSecret: 'BADCLIENTSECRET'
71+
}), function(e, r, token) {
72+
e.should.containDeep({
73+
name: 'invalid_grant',
74+
description: 'invalid username or password'
75+
})
76+
should(token).be.undefined()
77+
should(failClient.appAuth).be.undefined()
78+
done()
79+
})
80+
})
81+
82+
83+
it('should not set client.appAuth when authenticating with a bad UsergridAppAuth instance (using arguments)', function(done) {
84+
var failClient = new UsergridClient()
85+
failClient.authenticateApp(new UsergridAppAuth('BADCLIENTID', 'BADCLIENTSECRET'), function(e, r, token) {
86+
e.should.containDeep({
87+
name: 'invalid_grant',
88+
description: 'invalid username or password'
89+
})
90+
should(token).be.undefined()
91+
should(failClient.appAuth).be.undefined()
92+
done()
93+
})
94+
})
95+
96+
it('should return a 200 ok', function() {
97+
response.statusCode.should.equal(200)
98+
})
99+
100+
it('should have a valid token', function() {
101+
token.should.be.a.String()
102+
token.length.should.be.greaterThan(10)
103+
})
104+
105+
it('client.appAuth.token should be set to the token returned from Usergrid', function() {
106+
client.appAuth.should.have.property('token').equal(token)
107+
})
108+
109+
it('client.appAuth.isValid should be true', function() {
110+
client.appAuth.should.have.property('isValid').which.is.true()
111+
})
112+
113+
it('client.appAuth.expiry should be set to a future date', function() {
114+
client.appAuth.should.have.property('expiry').greaterThan(Date.now())
115+
})
116+
})
117+
118+
describe('authenticateUser()', function() {
119+
120+
this.slow(_slow)
121+
this.timeout(_timeout)
122+
123+
var response, token, client = new UsergridClient()
124+
before(function(done) {
125+
client.authenticateUser({
126+
username: config.test.username,
127+
password: config.test.password
128+
}, function(err, r, t) {
129+
response = r
130+
token = t
131+
done()
132+
})
133+
})
134+
135+
it('should fail when called without a email (or username) and password', function() {
136+
should(function() {
137+
var client = new UsergridClient()
138+
client.authenticateUser({})
139+
}).throw()
140+
})
141+
142+
it('should return a 200 ok', function() {
143+
response.statusCode.should.equal(200)
144+
})
145+
146+
it('should have a valid token', function() {
147+
token.should.be.a.String()
148+
token.length.should.be.greaterThan(10)
149+
})
150+
151+
it('client.currentUser.auth.token should be set to the token returned from Usergrid', function() {
152+
client.currentUser.auth.should.have.property('token').equal(token)
153+
})
154+
155+
it('client.currentUser.auth.isValid should be true', function() {
156+
client.currentUser.auth.should.have.property('isValid').which.is.true()
157+
})
158+
159+
it('client.currentUser.auth.expiry should be set to a future date', function() {
160+
client.currentUser.auth.should.have.property('expiry').greaterThan(Date.now())
161+
})
162+
163+
it('client.currentUser should have a username', function() {
164+
client.currentUser.should.have.property('username')
165+
})
166+
167+
it('client.currentUser should have an email', function() {
168+
client.currentUser.should.have.property('email')
169+
})
170+
171+
it('client.currentUser and client.currentUser.auth should not store password', function() {
172+
client.currentUser.should.not.have.property('password')
173+
client.currentUser.auth.should.not.have.property('password')
174+
})
175+
})
176+
177+
describe('appAuth, setAppAuth()', function() {
178+
it('should initialize by passing a list of arguments', function() {
179+
var client = new UsergridClient()
180+
client.setAppAuth(config.clientId, config.clientSecret, config.tokenTtl)
181+
client.appAuth.should.be.instanceof(UsergridAppAuth)
182+
})
183+
184+
it('should be a subclass of UsergridAuth', function() {
185+
var client = new UsergridClient()
186+
client.setAppAuth(config.clientId, config.clientSecret, config.tokenTtl)
187+
client.appAuth.should.be.instanceof(UsergridAuth)
188+
})
189+
190+
it('should initialize by passing an object', function() {
191+
var client = new UsergridClient()
192+
client.setAppAuth({
193+
clientId: config.clientId,
194+
clientSecret: config.clientSecret,
195+
tokenTtl: config.tokenTtl
196+
})
197+
client.appAuth.should.be.instanceof(UsergridAppAuth)
198+
})
199+
200+
it('should initialize by passing an instance of UsergridAppAuth', function() {
201+
var client = new UsergridClient()
202+
client.setAppAuth(new UsergridAppAuth(config.clientId, config.clientSecret, config.tokenTtl))
203+
client.appAuth.should.be.instanceof(UsergridAppAuth)
204+
})
205+
206+
it('should initialize by setting to an instance of UsergridAppAuth', function() {
207+
var client = new UsergridClient()
208+
client.appAuth = new UsergridAppAuth(config.clientId, config.clientSecret, config.tokenTtl)
209+
client.appAuth.should.be.instanceof(UsergridAppAuth)
210+
})
211+
})

0 commit comments

Comments
 (0)