Skip to content

Commit 7f27dd6

Browse files
committed
handled simulating user granting application access, getting rsvp, and app exchanging rsvp for a user ticket for #8
1 parent 0673d42 commit 7f27dd6

File tree

7 files changed

+90
-12
lines changed

7 files changed

+90
-12
lines changed

data/auth/grants.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"id": "0",
4+
"app": "foo",
5+
"user": "travi",
6+
"exp": "1485498119999"
7+
}
8+
]

lib/api/auth/grants.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
const grants = require('../../../data/auth/grants');
4+
5+
function getById(id, callback) {
6+
callback(null, grants[id]);
7+
}
8+
9+
module.exports = {
10+
getById
11+
};

lib/api/auth/strategy.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
const apps = require('./apps');
1+
const
2+
apps = require('./apps'),
3+
grants = require('./grants');
24

35
exports.register = function (server, options, next) {
46
server.auth.strategy('oz', 'oz', 'optional', {
57
oz: {
68
encryptionPassword: 'password',
7-
loadAppFunc: apps.getById
9+
loadAppFunc: apps.getById,
10+
loadGrantFunc: grants.getById
811
}
912
});
1013

test/integration/features/step_definitions/auth.js

+38-8
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,60 @@
22

33
const
44
oz = require('oz'),
5-
apps = require('../../../../data/auth/apps');
5+
hoek = require('hoek'),
6+
apps = require('../../../../data/auth/apps'),
7+
grants = require('../../../../data/auth/grants');
68

7-
function getOzTicket(callback) {
9+
function makeOzRequest(requestDetails, appDetails, callback) {
810
const
9-
url = `http://example.com/oz/app`,
1011
method = 'POST',
11-
appTicket = apps.foo;
12+
url = `http://example.com${requestDetails.endpoint}`;
1213

1314
this.requestTo(
1415
{
1516
url,
1617
method,
1718
headers: {
18-
authorization: oz.client.header(url, method, appTicket).field
19-
}
19+
authorization: oz.client.header(url, method, appDetails).field
20+
},
21+
payload: requestDetails.payload
2022
},
2123
() => {
24+
//console.log(this.getResponseBody());
2225
assert.equals(this.getResponseStatus(), 200);
2326

24-
callback();
27+
callback(null, this.serverResponse.result.entity);
2528
}
2629
);
2730
}
2831

32+
function requestAppTicket(appDetails, callback) {
33+
makeOzRequest.call(this, {endpoint: '/oz/app'}, appDetails, callback);
34+
}
35+
36+
function simulateUserGettingRsvpByGrantingScopes(callback) {
37+
oz.ticket.rsvp(apps.foo, grants[0], 'password', {}, callback);
38+
}
39+
40+
function exchangeRsvpForUserTicket(appTicket, rsvp, callback) {
41+
makeOzRequest.call(this, {
42+
endpoint: '/oz/rsvp',
43+
payload: JSON.stringify({rsvp})
44+
}, appTicket, callback);
45+
}
46+
47+
function getUserTicket(callback) {
48+
requestAppTicket.call(this, apps.foo, (err, appTicket) => {
49+
hoek.assert(!err, err);
50+
51+
simulateUserGettingRsvpByGrantingScopes.call(this, (err, rsvp) => {
52+
hoek.assert(!err, err);
53+
54+
exchangeRsvpForUserTicket.call(this, appTicket, rsvp, callback);
55+
});
56+
});
57+
}
58+
2959
module.exports = function () {
3060
this.World = require('../support/world.js').World;
3161

@@ -34,6 +64,6 @@ module.exports = function () {
3464
});
3565

3666
this.Given(/^request includes oz ticket$/, function (callback) {
37-
getOzTicket.call(this, callback);
67+
getUserTicket.call(this, callback);
3868
});
3969
};

test/integration/features/support/world.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports.World = function World() {
1616
server.inject({
1717
method: options.method,
1818
url: options.url,
19+
payload: options.payload,
1920
headers
2021
}, (response) => {
2122
this.serverResponse = response;

test/unit/api/auth/grants-test.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
const
4+
proxyquire = require('proxyquire'),
5+
any = require('../../../helpers/any');
6+
7+
suite('api authorization', function () {
8+
const
9+
grantList = any.listOf(any.simpleObject),
10+
grants = proxyquire('../../../../lib/api/auth/grants', {
11+
'../../../data/auth/grants': grantList
12+
});
13+
14+
test('that grant is retrieved by id', function () {
15+
const
16+
callback = sinon.spy(),
17+
grantId = any.int(grantList.length - 1);
18+
19+
grants.getById(grantId, callback);
20+
21+
assert.calledWith(callback, null, grantList[grantId]);
22+
});
23+
});

test/unit/api/auth/strategy-test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const
22
auth = require('../../../../lib/api/auth/strategy'),
3-
apps = require('../../../../lib/api/auth/apps');
3+
apps = require('../../../../lib/api/auth/apps'),
4+
grants = require('../../../../lib/api/auth/grants');
45

56
suite('api authorization', function () {
67
test('that the plugin is defined', () => {
@@ -19,7 +20,8 @@ suite('api authorization', function () {
1920

2021
assert.calledWith(strategy, 'oz', 'oz', 'optional', {oz: {
2122
encryptionPassword: 'password',
22-
loadAppFunc: apps.getById
23+
loadAppFunc: apps.getById,
24+
loadGrantFunc: grants.getById
2325
}});
2426
assert.calledOnce(next);
2527
});

0 commit comments

Comments
 (0)