test-passwords-utils.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const { store, search, remove } = require("sdk/passwords/utils");
  6. exports["test store requires `password` field"] = function(assert) {
  7. assert.throws(function() {
  8. store({ username: "foo", realm: "bar" });
  9. }, "`password` is required");
  10. };
  11. exports["test store requires `username` field"] = function(assert) {
  12. assert.throws(function() {
  13. store({ password: "foo", realm: "bar" });
  14. }, "`username` is required");
  15. };
  16. exports["test store requires `realm` field"] = function(assert) {
  17. assert.throws(function() {
  18. store({ username: "foo", password: "bar" });
  19. }, "`password` is required");
  20. };
  21. exports["test can't store same login twice"] = function(assert) {
  22. let options = { username: "user", password: "pass", realm: "realm" };
  23. store(options);
  24. assert.throws(function() {
  25. store(options);
  26. }, "can't store same pass twice");
  27. remove(options);
  28. };
  29. exports["test remove throws if no login found"] = function(assert) {
  30. assert.throws(function() {
  31. remove({ username: "foo", password: "bar", realm: "baz" });
  32. }, "can't remove unstored credentials");
  33. };
  34. exports["test addon associated credentials"] = function(assert) {
  35. let options = { username: "foo", password: "bar", realm: "baz" };
  36. store(options);
  37. assert.ok(search().length, "credential was stored");
  38. assert.ok(search(options).length, "stored credential found");
  39. assert.ok(search({ username: options.username }).length, "found by username");
  40. assert.ok(search({ password: options.password }).length, "found by password");
  41. assert.ok(search({ realm: options.realm }).length, "found by realm");
  42. let credential = search(options)[0];
  43. assert.equal(credential.url.indexOf("addon:"), 0,
  44. "`addon:` uri is used for add-on associated credentials");
  45. assert.equal(credential.username, options.username, "username matches");
  46. assert.equal(credential.password, options.password, "password matches");
  47. assert.equal(credential.realm, options.realm, "realm matches");
  48. assert.equal(credential.formSubmitURL, null,
  49. "`formSubmitURL` is `null` for add-on associated credentials");
  50. assert.equal(credential.usernameField, "", "usernameField is empty");
  51. assert.equal(credential.passwordField, "", "passwordField is empty");
  52. remove(search(options)[0]);
  53. assert.ok(!search(options).length, "remove worked");
  54. };
  55. exports["test web page associated credentials"] = function(assert) {
  56. let options = {
  57. url: "http://www.example.com",
  58. formSubmitURL: "http://login.example.com",
  59. username: "user",
  60. password: "pass",
  61. usernameField: "user-f",
  62. passwordField: "pass-f"
  63. };
  64. store({
  65. url: "http://www.example.com/login",
  66. formSubmitURL: "http://login.example.com/foo/authenticate.cgi",
  67. username: options.username,
  68. password: options.password,
  69. usernameField: options.usernameField,
  70. passwordField: options.passwordField
  71. });
  72. assert.ok(search().length, "credential was stored");
  73. assert.ok(search(options).length, "stored credential found");
  74. assert.ok(search({ username: options.username }).length, "found by username");
  75. assert.ok(search({ password: options.password }).length, "found by password");
  76. assert.ok(search({ formSubmitURL: options.formSubmitURL }).length,
  77. "found by formSubmitURL");
  78. assert.ok(search({ usernameField: options.usernameField }).length,
  79. "found by usernameField");
  80. assert.ok(search({ passwordField: options.passwordField }).length,
  81. "found by passwordField");
  82. let credential = search(options)[0];
  83. assert.equal(credential.url, options.url, "url matches");
  84. assert.equal(credential.username, options.username, "username matches");
  85. assert.equal(credential.password, options.password, "password matches");
  86. assert.equal(credential.realm, null, "realm is ");
  87. assert.equal(credential.formSubmitURL, options.formSubmitURL,
  88. "`formSubmitURL` matches");
  89. assert.equal(credential.usernameField, options.usernameField,
  90. "usernameField matches");
  91. assert.equal(credential.passwordField, options.passwordField,
  92. "passwordField matches");
  93. remove(search(options)[0]);
  94. assert.ok(!search(options).length, "remove worked");
  95. };
  96. exports["test site authentication credentials"] = function(assert) {
  97. let options = {
  98. url: "http://test.authentication.com",
  99. username: "u",
  100. password: "p",
  101. realm: "r"
  102. };
  103. store(options);
  104. assert.ok(search().length, "credential was stored");
  105. assert.ok(search(options).length, "stored credential found");
  106. assert.ok(search({ username: options.username }).length, "found by username");
  107. assert.ok(search({ password: options.password }).length, "found by password");
  108. assert.ok(search({ realm: options.realm }).length, "found by realm");
  109. assert.ok(search({ url: options.url }).length, "found by url");
  110. let credential = search(options)[0];
  111. assert.equal(credential.url, options.url, "url matches");
  112. assert.equal(credential.username, options.username, "username matches");
  113. assert.equal(credential.password, options.password, "password matches");
  114. assert.equal(credential.realm, options.realm, "realm matches");
  115. assert.equal(credential.formSubmitURL, null,
  116. "`formSubmitURL` is `null` for site authentication credentials");
  117. assert.equal(credential.usernameField, "", "usernameField is empty");
  118. assert.equal(credential.passwordField, "", "passwordField is empty");
  119. remove(search(options)[0]);
  120. assert.ok(!search(options).length, "remove worked");
  121. };
  122. require("test").run(exports);