/BookRental/node_modules/stormpath/test/sp.resource.directory_test.js

https://gitlab.com/DinaMorad1/NodeTraining · JavaScript · 334 lines · 254 code · 45 blank · 35 comment · 1 complexity · fccd81af205688fa2271dcbcd82423c1 MD5 · raw file

  1. /* jshint -W030 */
  2. "use strict";
  3. var common = require('./common');
  4. var sinon = common.sinon;
  5. var nock = common.nock;
  6. var u = common.u;
  7. var Account = require('../lib/resource/Account');
  8. var Group = require('../lib/resource/Group');
  9. var Tenant = require('../lib/resource/Tenant');
  10. var Provider = require('../lib/resource/Provider');
  11. var Directory = require('../lib/resource/Directory');
  12. var DataStore = require('../lib/ds/DataStore');
  13. describe('Resources: ', function () {
  14. describe('Directory resource', function () {
  15. var dataStore = new DataStore({apiKey: {id: 1, secret: 2}});
  16. describe('get accounts', function () {
  17. describe('if accounts not set', function () {
  18. var directory = new Directory();
  19. function getAccountsWithoutHref() {
  20. directory.getAccounts();
  21. }
  22. it('should throw unhandled exception', function () {
  23. getAccountsWithoutHref.should
  24. .throw(/cannot read property 'href' of undefined/i);
  25. });
  26. });
  27. describe('if accounts are set', function () {
  28. var sandbox, directory, getResourceStub, cbSpy, app, opt;
  29. before(function () {
  30. sandbox = sinon.sandbox.create();
  31. app = {accounts: {href: 'boom!'}};
  32. opt = {};
  33. directory = new Directory(app, dataStore);
  34. getResourceStub = sandbox.stub(dataStore, 'getResource', function (href, options, ctor, cb) {
  35. cb();
  36. });
  37. cbSpy = sandbox.spy();
  38. // call without optional param
  39. directory.getAccounts(cbSpy);
  40. // call with optional param
  41. directory.getAccounts(opt, cbSpy);
  42. });
  43. after(function () {
  44. sandbox.restore();
  45. });
  46. it('should get accounts', function () {
  47. /* jshint -W030 */
  48. getResourceStub.should.have.been.calledTwice;
  49. cbSpy.should.have.been.calledTwice;
  50. /* jshint +W030 */
  51. // call without optional param
  52. getResourceStub.should.have.been
  53. .calledWith(app.accounts.href, null, Account, cbSpy);
  54. // call with optional param
  55. getResourceStub.should.have.been
  56. .calledWith(app.accounts.href, opt, Account, cbSpy);
  57. });
  58. });
  59. });
  60. describe('create account', function () {
  61. describe('if accounts not set', function () {
  62. var directory = new Directory();
  63. function createAccountWithoutHref() {
  64. directory.createAccount();
  65. }
  66. it('should throw unhandled exception', function () {
  67. createAccountWithoutHref.should
  68. .throw(/cannot read property 'href' of undefined/i);
  69. });
  70. });
  71. describe('if accounts are set', function () {
  72. var sandbox, directory, createResourceStub, cbSpy, acc, app, opt;
  73. before(function () {
  74. sandbox = sinon.sandbox.create();
  75. acc = {};
  76. app = {accounts: {href: 'boom!'}};
  77. opt = {};
  78. directory = new Directory(app, dataStore);
  79. createResourceStub = sandbox.stub(dataStore, 'createResource',
  80. function (href, options, account, ctor, cb) {
  81. cb();
  82. });
  83. cbSpy = sandbox.spy();
  84. // call without optional param
  85. directory.createAccount(acc, cbSpy);
  86. // call with optional param
  87. directory.createAccount(acc, opt, cbSpy);
  88. });
  89. after(function () {
  90. sandbox.restore();
  91. });
  92. it('should create account', function () {
  93. /* jshint -W030 */
  94. createResourceStub.should.have.been.calledTwice;
  95. cbSpy.should.have.been.calledTwice;
  96. /* jshint +W030 */
  97. // call without optional param
  98. createResourceStub.should.have.been
  99. .calledWith(app.accounts.href, null, acc, Account, cbSpy);
  100. // call with optional param
  101. createResourceStub.should.have.been
  102. .calledWith(app.accounts.href, opt, acc, Account, cbSpy);
  103. });
  104. });
  105. });
  106. describe('get groups', function () {
  107. describe('if groups href not set', function () {
  108. var directory = new Directory();
  109. function getAccountsWithoutHref() {
  110. directory.getGroups();
  111. }
  112. it('should throw unhandled exception', function () {
  113. getAccountsWithoutHref.should
  114. .throw(/cannot read property 'href' of undefined/i);
  115. });
  116. });
  117. describe('if groups href are set', function () {
  118. var sandbox, directory, getResourceStub, cbSpy, app, opt;
  119. before(function () {
  120. sandbox = sinon.sandbox.create();
  121. app = {groups: {href: 'boom!'}};
  122. opt = {};
  123. directory = new Directory(app, dataStore);
  124. getResourceStub = sandbox.stub(dataStore, 'getResource', function (href, options, ctor, cb) {
  125. cb();
  126. });
  127. cbSpy = sandbox.spy();
  128. // call without optional param
  129. directory.getGroups(cbSpy);
  130. // call with optional param
  131. directory.getGroups(opt, cbSpy);
  132. });
  133. after(function () {
  134. sandbox.restore();
  135. });
  136. it('should get groups', function () {
  137. /* jshint -W030 */
  138. getResourceStub.should.have.been.calledTwice;
  139. cbSpy.should.have.been.calledTwice;
  140. /* jshint +W030 */
  141. // call without optional param
  142. getResourceStub.should.have.been
  143. .calledWith(app.groups.href, null, Group, cbSpy);
  144. // call with optional param
  145. getResourceStub.should.have.been
  146. .calledWith(app.groups.href, opt, Group, cbSpy);
  147. });
  148. });
  149. });
  150. describe('create group', function () {
  151. describe('if groups href not set', function () {
  152. var directory = new Directory();
  153. function createGroupWithoutHref() {
  154. directory.createGroup();
  155. }
  156. it('should throw unhandled exception', function () {
  157. createGroupWithoutHref.should
  158. .throw(/cannot read property 'href' of undefined/i);
  159. });
  160. });
  161. describe('if groups href are set', function () {
  162. var sandbox, directory, createResourceStub, cbSpy, group, app, opt;
  163. before(function () {
  164. sandbox = sinon.sandbox.create();
  165. group = {};
  166. app = {groups: {href: 'boom!'}};
  167. opt = {};
  168. directory = new Directory(app, dataStore);
  169. createResourceStub = sandbox.stub(dataStore, 'createResource',
  170. function (href, options, groups, ctor, cb) {
  171. cb();
  172. });
  173. cbSpy = sandbox.spy();
  174. // call without optional param
  175. directory.createGroup(group, cbSpy);
  176. // call with optional param
  177. directory.createGroup(group, opt, cbSpy);
  178. });
  179. after(function () {
  180. sandbox.restore();
  181. });
  182. it('should create account', function () {
  183. /* jshint -W030 */
  184. createResourceStub.should.have.been.calledTwice;
  185. cbSpy.should.have.been.calledTwice;
  186. /* jshint +W030 */
  187. // call without optional param
  188. createResourceStub.should.have.been
  189. .calledWith(app.groups.href, null, group, Group, cbSpy);
  190. // call with optional param
  191. createResourceStub.should.have.been
  192. .calledWith(app.groups.href, opt, group, Group, cbSpy);
  193. });
  194. });
  195. });
  196. describe('get tenant', function () {
  197. describe('if tenants href not set', function () {
  198. var directory = new Directory();
  199. function getAccountsWithoutHref() {
  200. directory.getTenant();
  201. }
  202. it('should throw unhandled exception', function () {
  203. getAccountsWithoutHref.should
  204. .throw(/cannot read property 'href' of undefined/i);
  205. });
  206. });
  207. describe('if tenants href are set', function () {
  208. var sandbox, directory, getResourceStub, cbSpy, app, opt;
  209. before(function () {
  210. sandbox = sinon.sandbox.create();
  211. app = {tenant: {href: 'boom!'}};
  212. opt = {};
  213. directory = new Directory(app, dataStore);
  214. getResourceStub = sandbox.stub(dataStore, 'getResource', function (href, options, ctor, cb) {
  215. cb();
  216. });
  217. cbSpy = sandbox.spy();
  218. // call without optional param
  219. directory.getTenant(cbSpy);
  220. // call with optional param
  221. directory.getTenant(opt, cbSpy);
  222. });
  223. after(function () {
  224. sandbox.restore();
  225. });
  226. it('should get tenants', function () {
  227. /* jshint -W030 */
  228. getResourceStub.should.have.been.calledTwice;
  229. cbSpy.should.have.been.calledTwice;
  230. /* jshint +W030 */
  231. // call without optional param
  232. getResourceStub.should.have.been
  233. .calledWith(app.tenant.href, null, Tenant, cbSpy);
  234. // call with optional param
  235. getResourceStub.should.have.been
  236. .calledWith(app.tenant.href, opt, Tenant, cbSpy);
  237. });
  238. });
  239. });
  240. describe('get provider', function(){
  241. function getProvider(data) {
  242. return function () {
  243. var dirObj, providerObj, app, provider;
  244. before(function (done) {
  245. // assert
  246. providerObj = {href: '/provider/href', name: 'provider name'};
  247. dirObj = {provider: {href: providerObj.href}};
  248. app = new Directory(dirObj, dataStore);
  249. nock(u.BASE_URL).get(u.v1(providerObj.href)).reply(200, providerObj);
  250. var args = [];
  251. if (data) {
  252. args.push(data);
  253. }
  254. args.push(function cb(err, prov) {
  255. provider = prov;
  256. done();
  257. });
  258. // act
  259. app.getProvider.apply(app, args);
  260. });
  261. it('should get provider', function () {
  262. provider.href.should.be.equal(provider.href);
  263. provider.name.should.be.equal(provider.name);
  264. });
  265. it('should be an instance of Provider', function () {
  266. provider.should.be.an.instanceOf(Provider);
  267. });
  268. };
  269. }
  270. describe('without options', getProvider());
  271. describe('with options', getProvider({}));
  272. describe('if provider not set', function () {
  273. var accObj, app, cbSpy;
  274. before(function () {
  275. // assert
  276. accObj = {providerData: null};
  277. app = new Account(accObj, dataStore);
  278. cbSpy = sinon.spy();
  279. // act
  280. app.getProviderData(cbSpy);
  281. });
  282. it('should call cb without options', function () {
  283. cbSpy.should.have.been.calledOnce;
  284. cbSpy.should.have.been.calledWith(undefined, undefined);
  285. });
  286. });
  287. });
  288. });
  289. });