Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.

Commit d5d09e6

Browse files
authored
Merge pull request #534 from stormpath/feature-directory-account-schema
Add Directory.getAccountSchema()
2 parents d7a7683 + 665010b commit d5d09e6

File tree

6 files changed

+262
-0
lines changed

6 files changed

+262
-0
lines changed

lib/resource/Directory.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ Directory.prototype.getAccountCreationPolicy = function getAccountCreationPolicy
7676
return this.dataStore.getResource(this.accountCreationPolicy.href, args.options, require('./InstanceResource'), args.callback);
7777
};
7878

79+
/**
80+
* Get the {@link Schema} resource of this Directory resource. The schema allows
81+
* you to control which attributes are required when accounts are created in this
82+
* directory.
83+
*
84+
* @param {ExpansionOptions} [expansionOptions]
85+
* For retrieving linked resources of the {@link Schema} during this request.
86+
*
87+
* @param {Function} callback
88+
* Callback function, will be called with (err, {@link Schema}).
89+
*/
90+
Directory.prototype.getAccountSchema = function getAccountSchema(/* [options,] callback */) {
91+
var args = utils.resolveArgs(arguments, ['options', 'callback'], true);
92+
return this.dataStore.getResource(this.accountSchema.href, args.options, require('./Schema'), args.callback);
93+
};
94+
7995
/**
8096
* Get the {@link PasswordPolicy} resource of this Directory resource.
8197
*

lib/resource/Field.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
var Resource = require('./Resource');
4+
var utils = require('../utils');
5+
6+
/**
7+
* @class Field
8+
*
9+
* @description
10+
* Encapsulates an account field, as part of a {@link Schema}.
11+
*
12+
* This class should not be manually constructed. It should be obtained from one of these methods:
13+
*
14+
* - {@link Schema#getFields Schema.getFields()}.
15+
*/
16+
function Field(){
17+
Field.super_.apply(this, arguments);
18+
}
19+
20+
utils.inherits(Field, Resource);
21+
22+
/**
23+
* Save changes to this resource.
24+
*
25+
* @param {Function} callback
26+
* The function to call when the save operation is complete. Will be called
27+
* with the parameters (err, updatedResource).
28+
*/
29+
Field.prototype.save = function save(callback) {
30+
this.dataStore.saveResource(this, callback);
31+
};
32+
33+
module.exports = Field;

lib/resource/Schema.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
var Resource = require('./Resource');
4+
var utils = require('../utils');
5+
6+
/**
7+
* @class Schema
8+
*
9+
* @description
10+
* Encapsulates the Schema resource of a {@link Directory}. This schema allows you
11+
* to control which Account attributes (referred to as fields) are required when
12+
* creating new accounts in the directory. For full documentation of this resource, please see
13+
* [How to Manage an Account’s Required Attributes](https://docs.stormpath.com/rest/product-guide/latest/accnt_mgmt.html#how-to-manage-an-account-s-required-attributes).
14+
*
15+
* This class should not be manually constructed. It should be obtained from one of these methods:
16+
* - {@link Directory#getAccountSchema Directory.getAccountSchema()}.
17+
*
18+
* @example <caption>Disabling a field requirement.</caption>
19+
* var _ = require('lodash');
20+
*
21+
* schema.getFields(function (err, fieldsCollection) {
22+
* var givenNameField = _.find(fieldsCollection.items, {
23+
* name: 'givenName'
24+
* });
25+
*
26+
* givenNameField.required = false;
27+
*
28+
* givenNameField.save();
29+
* });
30+
*/
31+
function Schema() {
32+
Schema.super_.apply(this, arguments);
33+
}
34+
35+
utils.inherits(Schema, Resource);
36+
37+
/**
38+
* Get the collection of {@link Field Fields} for this schema.
39+
*
40+
* @param {CollectionQueryOptions} [options]
41+
* Options for querying, paginating, and expanding the collection.
42+
*
43+
* @param {Function} callback
44+
* The function to call when the operation is complete. Will be called
45+
* with the parameters (err, {@link CollectionResource}). The collection will
46+
* be a list of {@link Field} objects.
47+
*/
48+
Schema.prototype.getFields = function () {
49+
var args = utils.resolveArgs(arguments, ['options', 'callback'], true);
50+
return this.dataStore.getResource(this.fields.href, args.options, require('./Field'), args.callback);
51+
};
52+
53+
module.exports = Schema;

test/sp.resource.directory_test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var Account = require('../lib/resource/Account');
99
var Group = require('../lib/resource/Group');
1010
var Organization = require('../lib/resource/Organization');
1111
var OrganizationAccountStoreMapping = require('../lib/resource/OrganizationAccountStoreMapping');
12+
var Schema = require('../lib/resource/Schema');
1213
var Tenant = require('../lib/resource/Tenant');
1314
var Provider = require('../lib/resource/Provider');
1415
var Directory = require('../lib/resource/Directory');
@@ -380,6 +381,49 @@ describe('Resources: ', function () {
380381
});
381382
});
382383

384+
describe('get account schema', function () {
385+
describe('if accountSchema href is set', function () {
386+
var opt;
387+
var getResourceStub;
388+
var sandbox;
389+
var app;
390+
var directory;
391+
var cbSpy;
392+
393+
before(function () {
394+
opt = {};
395+
sandbox = sinon.sandbox.create();
396+
app = { accountSchema: { href: 'boom!' } };
397+
directory = new Directory(app, dataStore);
398+
cbSpy = sandbox.spy();
399+
400+
getResourceStub = sandbox.stub(dataStore, 'getResource', function (href, options, ctor, cb) {
401+
cb();
402+
});
403+
404+
directory.getAccountSchema(cbSpy);
405+
directory.getAccountSchema(opt, cbSpy);
406+
});
407+
408+
after(function () {
409+
sandbox.restore();
410+
});
411+
412+
it('should get account schema', function () {
413+
cbSpy.should.have.been.calledTwice;
414+
getResourceStub.should.have.been.calledTwice;
415+
416+
getResourceStub.should.have.been.calledWith(
417+
app.accountSchema.href, null, Schema, cbSpy
418+
);
419+
420+
getResourceStub.should.have.been.calledWith(
421+
app.accountSchema.href, opt, Schema, cbSpy
422+
);
423+
});
424+
});
425+
});
426+
383427
describe('get organization mappings', function () {
384428
describe('if organizationMappings href are set', function () {
385429
var opt;

test/sp.resource.field_test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
var common = require('./common');
4+
var sinon = common.sinon;
5+
6+
var DataStore = require('../lib/ds/DataStore');
7+
var Field = require('../lib/resource/Field');
8+
9+
describe('Field Resource', function () {
10+
describe('save()', function () {
11+
var sandbox;
12+
var field;
13+
var dataStore;
14+
var requestExecutorStub;
15+
16+
var mockField = {
17+
href: 'https://api.stormpath.com/v1/fields/7dDfMOkrekkLhbWBLcGWuN',
18+
createdAt: '2016-08-02T20:16:21.931Z',
19+
modifiedAt: '2016-08-02T20:16:21.931Z',
20+
name: 'givenName',
21+
required: true,
22+
schema: {
23+
href: 'https://api.stormpath.com/v1/schemas/7dDfMLQmkARN4mQK9MPGIJ'
24+
}
25+
};
26+
27+
before(function () {
28+
dataStore = new DataStore({client: {apiKey: {id: 1, secret: 2}}});
29+
sandbox = sinon.sandbox.create();
30+
field = new Field(mockField, dataStore);
31+
requestExecutorStub = sandbox.stub(dataStore.requestExecutor, 'execute');
32+
});
33+
34+
after(function () {
35+
sandbox.restore();
36+
});
37+
38+
it('should post the resource to the REST API', function () {
39+
field.save();
40+
requestExecutorStub.should.have.been.calledWith({
41+
body: mockField,
42+
uri: mockField.href,
43+
method: 'POST'
44+
});
45+
});
46+
});
47+
});

test/sp.resource.schema_test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
3+
var common = require('./common');
4+
var assert = common.assert;
5+
var sinon = common.sinon;
6+
7+
var DataStore = require('../lib/ds/DataStore');
8+
var Field = require('../lib/resource/Field');
9+
var Schema = require('../lib/resource/Schema');
10+
11+
describe('Schema Resource', function(){
12+
13+
describe('getFields()', function(){
14+
15+
var sandbox;
16+
var schema;
17+
var dataStore;
18+
19+
var mockSchema = {
20+
href: 'https://api.stormpath.com/v1/schemas/7cvoYkLuGzpnAuVlKQdiDf/',
21+
fields: {
22+
href: 'https://api.stormpath.com/v1/schemas/7cvoYkLuGzpnAuVlKQdiDf/fields'
23+
}
24+
};
25+
26+
var mockFieldsResponse = {
27+
href: 'https://api.stormpath.com/v1/schemas/7dDfMLQmkARN4mQK9MPGIJ/fields',
28+
offset: 0,
29+
limit: 25,
30+
size: 2,
31+
items: [
32+
{
33+
href: 'https://api.stormpath.com/v1/fields/7dDfMOkrekkLhbWBLcGWuN',
34+
createdAt: '2016-08-02T20:16:21.931Z',
35+
modifiedAt: '2016-08-02T20:16:21.931Z',
36+
name: 'givenName',
37+
required: true,
38+
schema: {
39+
href: 'https://api.stormpath.com/v1/schemas/7dDfMLQmkARN4mQK9MPGIJ'
40+
}
41+
},
42+
]
43+
};
44+
45+
46+
47+
before(function(){
48+
dataStore = new DataStore({client: {apiKey: {id: 1, secret: 2}}});
49+
sandbox = sinon.sandbox.create();
50+
schema = new Schema(mockSchema, dataStore);
51+
52+
sandbox.stub(dataStore.requestExecutor, 'execute', function (req, cb) {
53+
cb(null, mockFieldsResponse);
54+
});
55+
});
56+
57+
after(function(){
58+
sandbox.restore();
59+
});
60+
61+
it('should return Field instances', function(done){
62+
schema.getFields(function(err, result){
63+
assert.equal(result.items, mockFieldsResponse.items);
64+
assert.instanceOf(mockFieldsResponse.items[0], Field);
65+
done();
66+
});
67+
});
68+
});
69+
});

0 commit comments

Comments
 (0)