PageRenderTime 48ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/bower_components/auth0.js/test/user-and-pass.tests.js

https://gitlab.com/KevinSleegers/AngularSpace
JavaScript | 438 lines | 371 code | 57 blank | 10 comment | 11 complexity | ae252ecc32b2853254a858a6ef2b9ca4 MD5 | raw file
  1. /**
  2. * Config mocha
  3. */
  4. mocha.timeout(60000);
  5. mocha.globals(['jQuery*', '__auth0jp*']);
  6. /**
  7. * Test User and Password
  8. */
  9. describe('Auth0 - User And Passwords', function () {
  10. var auth0 = new Auth0({
  11. domain: 'mdocs.auth0.com',
  12. callbackURL: 'http://localhost:3000/',
  13. clientID: 'ptR6URmXef0OfBDHK0aCIy7iPKpdCG4t'
  14. });
  15. describe('Login', function () {
  16. describe('with resource owner', function () {
  17. it('should call the callback when user/pass is wrong', function (done) {
  18. auth0.login({
  19. connection: 'tests',
  20. username: 'testttt@wrong.com',
  21. password: '12345'
  22. }, function (err, profile) {
  23. expect(err.status).to.equal(401);
  24. expect(err.details.code).to.equal('invalid_user_password');
  25. done();
  26. });
  27. });
  28. // Fails on IE8. Some bug with errors on XMLHttpRequest handling
  29. // XXX: Fix it!
  30. it.skip('should call the callback with err when the connection doesn\'t exists', function (done) {
  31. auth0.login({
  32. connection: 'testsw3eeasdsadsa',
  33. username: 'testttt@wrong.com',
  34. password: '12345'
  35. }, function (err, profile) {
  36. expect(err.status).to.equal(400);
  37. expect(err.message).to.equal('invalid_connection');
  38. done();
  39. });
  40. });
  41. it('should return profile after successfull authentication', function (done) {
  42. auth0.login({
  43. connection: 'tests',
  44. username: 'johnfoo@gmail.com',
  45. password: '12345'
  46. }, function (err, profile, id_token, access_token) {
  47. expect(profile.name).to.eql('John Foo');
  48. expect(profile.foo).to.eql('bar');
  49. expect(profile.identities.length).to.eql(1);
  50. expect(id_token).to.exist;
  51. expect(access_token).to.exist;
  52. done();
  53. });
  54. });
  55. it('should return refresh_token after successfull authentication with offline_mode', function (done) {
  56. auth0.login({
  57. connection: 'tests',
  58. username: 'johnfoo@gmail.com',
  59. password: '12345',
  60. offline_mode: true
  61. }, function (err, profile, id_token, access_token, state, refresh_token) {
  62. expect(profile.name).to.eql('John Foo');
  63. expect(profile.foo).to.eql('bar');
  64. expect(profile.identities.length).to.eql(1);
  65. expect(id_token).to.exist;
  66. expect(refresh_token).to.exist;
  67. expect(access_token).to.exist;
  68. done();
  69. });
  70. });
  71. });
  72. describe('with wsfed', function () {
  73. it('should call the callback when user/pass is wrong', function (done) {
  74. auth0.login({
  75. connection: 'tests',
  76. username: 'testttt@wrong.com',
  77. password: '12345'
  78. }, function (err) {
  79. expect(err.status).to.equal(401);
  80. expect(err.details.code).to.equal('invalid_user_password');
  81. done();
  82. });
  83. });
  84. it('should call the callback with err when the connection doesn\'t exists', function (done) {
  85. auth0.login({
  86. connection: 'testsw3eeasdsadsa',
  87. username: 'testttt@wrong.com',
  88. password: '12345'
  89. }, function (err) {
  90. expect(err.status).to.equal(404);
  91. expect(err.message).to.match(/connection not found/ig);
  92. done();
  93. });
  94. });
  95. it('should render wsfed form after successfull authentication', function (done) {
  96. auth0._renderAndSubmitWSFedForm = function (options, htmlForm) {
  97. expect(htmlForm).to.match(/<form/);
  98. done();
  99. };
  100. auth0.login({
  101. connection: 'tests',
  102. username: 'johnfoo@gmail.com',
  103. password: '12345'
  104. });
  105. });
  106. });
  107. it('should trim username before login', function (done) {
  108. auth0.login({
  109. connection: 'tests',
  110. username: ' johnfoo@gmail.com ',
  111. password: '12345'
  112. }, function (err, profile, id_token, access_token) {
  113. expect(profile.name).to.eql('John Foo');
  114. expect(profile.foo).to.eql('bar');
  115. expect(profile.identities.length).to.eql(1);
  116. expect(id_token).to.exist;
  117. expect(access_token).to.exist;
  118. done();
  119. });
  120. });
  121. });
  122. describe('Signup', function () {
  123. it('should fail when the username is null', function (done) {
  124. auth0.signup({
  125. connection: 'tests',
  126. username: null,
  127. password: '12345'
  128. }, function (err) {
  129. expect(err.status).to.equal(400);
  130. expect(err.message).to.exist;
  131. expect(err.details).to.exist;
  132. done();
  133. });
  134. });
  135. it('should handle server errors', function (done) {
  136. auth0.signup({
  137. connection: 'tests',
  138. username: 'pepo@example.com',
  139. password: '12345'
  140. }, function (err) {
  141. expect(err.status).to.equal(500);
  142. expect(err.message).to.exist;
  143. expect(err.details).to.exist;
  144. done();
  145. });
  146. });
  147. describe('with resource owner authentication', function () {
  148. it('should return profile after successfull signup', function (done) {
  149. auth0.signup({
  150. connection: 'tests',
  151. username: 'johnfoo@gmail.com',
  152. password: '12345'
  153. }, function (err, profile, id_token, access_token) {
  154. expect(profile.name).to.eql('John Foo');
  155. expect(profile.identities.length).to.eql(1);
  156. expect(id_token).to.exist;
  157. expect(access_token).to.exist;
  158. done();
  159. });
  160. });
  161. it('should not return profile after successfull signup if auto_login is false', function (done) {
  162. auth0._renderAndSubmitWSFedForm = function (options, htmlForm) {
  163. done(new Error('this should not be called'));
  164. };
  165. auth0.signup({
  166. connection: 'tests',
  167. username: 'johnfoo@gmail.com',
  168. password: '12345',
  169. auto_login: false
  170. }, function (err, profile) {
  171. done(profile);
  172. });
  173. });
  174. });
  175. describe('with wsfed authentication', function () {
  176. it('should render wsfed form after successfull signup', function (done) {
  177. auth0._renderAndSubmitWSFedForm = function (options, htmlForm) {
  178. expect(htmlForm).to.match(/<form/);
  179. done();
  180. };
  181. auth0.signup({
  182. connection: 'tests',
  183. username: 'johnfoo@gmail.com',
  184. password: '12345'
  185. }, function (err) {
  186. done(err);
  187. });
  188. });
  189. it('should not render wsfed form after successfull signup if auto_login is false', function (done) {
  190. auth0._renderAndSubmitWSFedForm = function (options, htmlForm) {
  191. done(new Error('this should not be called'));
  192. };
  193. auth0.signup({
  194. connection: 'tests',
  195. username: 'johnfoo@gmail.com',
  196. password: '12345',
  197. auto_login: false
  198. }, function (err) {
  199. done(err);
  200. });
  201. });
  202. });
  203. it('should trim username before signup', function (done) {
  204. auth0.signup({
  205. connection: 'tests',
  206. username: 'johnfoo@gmail.com',
  207. password: '12345'
  208. }, function (err, profile) {
  209. expect(err).to.be(null);
  210. done();
  211. });
  212. });
  213. it('should handle username and email when requires_username enabled', function (done) {
  214. var username = makeUsername(15);
  215. auth0.signup({
  216. connection: 'requires-username',
  217. username: username,
  218. email: username + '@gmail.com',
  219. password: '12345'
  220. }, function (err, profile) {
  221. expect(err).to.be(null);
  222. expect(profile).to.have.property('username');
  223. expect(profile).to.have.property('email');
  224. expect(profile.username).to.be(username);
  225. expect(profile.email).to.be(username + '@gmail.com');
  226. done();
  227. });
  228. });
  229. it('should error when username is missing when requires_username enabled', function (done) {
  230. var username = makeUsername(15);
  231. auth0.signup({
  232. connection: 'requires-username',
  233. email: username + '@gmail.com',
  234. password: '12345'
  235. }, function (err, profile) {
  236. expect(err).to.not.be(null);
  237. expect(err.status).to.be(400);
  238. expect(err).to.have.property('message');
  239. expect(err).to.have.property('details');
  240. expect(err.message).to.match(/missing username/ig);
  241. done();
  242. });
  243. });
  244. });
  245. describe('Change Password', function () {
  246. // TODO: add a test to check that the user can provide a username or email, when `requires_username` is enabled
  247. it('should fail when the username is null', function (done) {
  248. auth0.changePassword({
  249. connection: 'tests',
  250. username: null,
  251. password: '12345'
  252. }, function (err) {
  253. expect(err.status).to.equal(400);
  254. expect(err).to.have.property('message');
  255. expect(err).to.have.property('details');
  256. done();
  257. });
  258. });
  259. //this timeout sometimes. I need to improve.
  260. it('should return OK after successfull operation', function (done) {
  261. auth0.changePassword({
  262. connection: 'tests',
  263. username: 'johnfoo@contoso.com',
  264. password: '12345'
  265. }, function (err) {
  266. expect(err).to.be(null);
  267. done();
  268. });
  269. });
  270. it('should trim username before operation', function (done) {
  271. auth0.changePassword({
  272. connection: 'tests',
  273. username: ' johnfoo@gmail.com ',
  274. password: '12345'
  275. }, function (err) {
  276. expect(err).to.be(null);
  277. done();
  278. });
  279. });
  280. });
  281. describe('Validate User', function () {
  282. it('should return "true" if the credentials are valid', function (done) {
  283. auth0.validateUser({
  284. connection: 'tests',
  285. username: 'johnfoo@gmail.com',
  286. password: '12345'
  287. }, function (err, valid) {
  288. expect(err).to.be(null);
  289. expect(valid).to.equal(true);
  290. done();
  291. });
  292. });
  293. it('should return "true" if the credentials with username and email are valid', function (done) {
  294. auth0.validateUser({
  295. connection: 'tests',
  296. username: 'johnfoo',
  297. email: 'johnfoo@gmail.com',
  298. password: '12345'
  299. }, function (err, valid) {
  300. expect(err).to.be(null);
  301. expect(valid).to.equal(false);
  302. done();
  303. });
  304. });
  305. it('should return "false" if username is invalid', function (done) {
  306. auth0.validateUser({
  307. connection: 'tests',
  308. username: 'invalid-user@gmail.com',
  309. password: '12345'
  310. }, function (err, valid) {
  311. expect(err).to.be(null);
  312. expect(valid).to.equal(false);
  313. done();
  314. });
  315. });
  316. it('should return "false" if email is valid and username is invalid', function (done) {
  317. auth0.validateUser({
  318. connection: 'tests',
  319. username: 'invalid-user',
  320. email: 'johnfoo@gmail.com',
  321. password: '12345'
  322. }, function (err, valid) {
  323. expect(err).to.be(null);
  324. expect(valid).to.equal(false);
  325. done();
  326. });
  327. });
  328. it('should return "false" if email is invalid and username is valid', function (done) {
  329. auth0.validateUser({
  330. connection: 'tests',
  331. username: 'johnfoo',
  332. email: 'invalid#email@gmail.com',
  333. password: '12345'
  334. }, function (err, valid) {
  335. expect(err).to.be(null);
  336. expect(valid).to.equal(false);
  337. done();
  338. });
  339. });
  340. it('should return "false" if connection is invalid', function (done) {
  341. auth0.validateUser({
  342. connection: 'invalid-conn',
  343. username: 'johnfoo@gmail.com',
  344. password: '12345'
  345. }, function (err, valid) {
  346. expect(err).to.be(null);
  347. expect(valid).to.equal(false);
  348. done();
  349. });
  350. });
  351. it('should return error if connection is not specified', function (done) {
  352. auth0.validateUser({
  353. username: 'johnfoo@gmail.com',
  354. password: '12345'
  355. }, function (err) {
  356. if (auth0._use)
  357. expect(err.message).to.equal('connection parameter is mandatory');
  358. done();
  359. });
  360. });
  361. it('should trim username before validation', function (done) {
  362. auth0.validateUser({
  363. connection: 'tests',
  364. username: ' johnfoo@gmail.com ',
  365. password: '12345'
  366. }, function (err, valid) {
  367. expect(err).to.be(null);
  368. expect(valid).to.equal(true);
  369. done();
  370. });
  371. });
  372. });
  373. });
  374. function makeUsername(size) {
  375. var uname = "";
  376. var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  377. for( var i=0; i < size; i++ ) {
  378. uname += possible.charAt(Math.floor(Math.random() * possible.length));
  379. }
  380. return uname.toLowerCase();
  381. }