test-passwords.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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");
  6. exports["test store requires `password` field"] = function(assert, done) {
  7. store({
  8. username: "foo",
  9. realm: "bar",
  10. onComplete: function onComplete() {
  11. assert.fail("onComplete should not be called");
  12. },
  13. onError: function onError() {
  14. assert.pass("'`password` is required");
  15. done();
  16. }
  17. });
  18. };
  19. exports["test store requires `username` field"] = function(assert, done) {
  20. store({
  21. password: "foo",
  22. realm: "bar",
  23. onComplete: function onComplete() {
  24. assert.fail("onComplete should not be called");
  25. },
  26. onError: function onError() {
  27. assert.pass("'`username` is required");
  28. done();
  29. }
  30. });
  31. };
  32. exports["test onComplete is optional"] = function(assert, done) {
  33. store({
  34. realm: "bla",
  35. username: "bla",
  36. password: "bla",
  37. onError: function onError() {
  38. assert.fail("onError was called");
  39. }
  40. });
  41. assert.pass("exception is not thrown if `onComplete is missing")
  42. done();
  43. };
  44. exports["test exceptions in onComplete are reported"] = function(assert, done) {
  45. store({
  46. realm: "throws",
  47. username: "error",
  48. password: "boom!",
  49. onComplete: function onComplete(error) {
  50. throw new Error("Boom!")
  51. },
  52. onError: function onError(error) {
  53. assert.equal(error.message, "Boom!", "Error thrown is reported");
  54. done();
  55. }
  56. });
  57. };
  58. exports["test store requires `realm` field"] = function(assert, done) {
  59. store({
  60. username: "foo",
  61. password: "bar",
  62. onComplete: function onComplete() {
  63. assert.fail("onComplete should not be called");
  64. },
  65. onError: function onError() {
  66. assert.pass("'`realm` is required");
  67. done();
  68. }
  69. });
  70. };
  71. exports["test can't store same login twice"] = function(assert, done) {
  72. store({
  73. username: "user",
  74. password: "pass",
  75. realm: "realm",
  76. onComplete: function onComplete() {
  77. assert.pass("credential saved");
  78. store({
  79. username: "user",
  80. password: "pass",
  81. realm: "realm",
  82. onComplete: function onComplete() {
  83. assert.fail("onComplete should not be called");
  84. },
  85. onError: function onError() {
  86. assert.pass("re-saving credential failed");
  87. remove({
  88. username: "user",
  89. password: "pass",
  90. realm: "realm",
  91. onComplete: function onComplete() {
  92. assert.pass("credential was removed");
  93. done();
  94. },
  95. onError: function onError() {
  96. assert.fail("remove should not fail");
  97. }
  98. });
  99. }
  100. });
  101. },
  102. onError: function onError() {
  103. assert.fail("onError should not be called");
  104. }
  105. });
  106. };
  107. exports["test remove fails if no login found"] = function(assert, done) {
  108. remove({
  109. username: "foo",
  110. password: "bar",
  111. realm: "baz",
  112. onComplete: function onComplete() {
  113. assert.fail("should not be able to remove unstored credentials");
  114. },
  115. onError: function onError() {
  116. assert.pass("can't remove unstored credentials");
  117. done();
  118. }
  119. });
  120. };
  121. exports["test addon associated credentials"] = function(assert, done) {
  122. store({
  123. username: "foo",
  124. password: "bar",
  125. realm: "baz",
  126. onComplete: function onComplete() {
  127. search({
  128. username: "foo",
  129. password: "bar",
  130. realm: "baz",
  131. onComplete: function onComplete([credential]) {
  132. assert.equal(credential.url.indexOf("addon:"), 0,
  133. "`addon:` uri is used for add-on credentials");
  134. assert.equal(credential.username, "foo",
  135. "username matches");
  136. assert.equal(credential.password, "bar",
  137. "password matches");
  138. assert.equal(credential.realm, "baz", "realm matches");
  139. assert.equal(credential.formSubmitURL, null,
  140. "`formSubmitURL` is `null` for add-on credentials");
  141. assert.equal(credential.usernameField, "", "usernameField is empty");
  142. assert.equal(credential.passwordField, "", "passwordField is empty");
  143. remove({
  144. username: credential.username,
  145. password: credential.password,
  146. realm: credential.realm,
  147. onComplete: function onComplete() {
  148. assert.pass("credential is removed");
  149. done();
  150. },
  151. onError: function onError() {
  152. assert.fail("onError should not be called");
  153. }
  154. });
  155. },
  156. onError: function onError() {
  157. assert.fail("onError should not be called");
  158. }
  159. });
  160. },
  161. onError: function onError() {
  162. assert.fail("onError should not be called");
  163. }
  164. });
  165. };
  166. exports["test web page associated credentials"] = function(assert, done) {
  167. store({
  168. url: "http://bar.foo.com/authentication/?login",
  169. formSubmitURL: "http://login.foo.com/authenticate.cgi",
  170. username: "user",
  171. password: "pass",
  172. usernameField: "user-f",
  173. passwordField: "pass-f",
  174. onComplete: function onComplete() {
  175. search({
  176. username: "user",
  177. password: "pass",
  178. url: "http://bar.foo.com",
  179. formSubmitURL: "http://login.foo.com",
  180. onComplete: function onComplete([credential]) {
  181. assert.equal(credential.url, "http://bar.foo.com", "url matches");
  182. assert.equal(credential.username, "user", "username matches");
  183. assert.equal(credential.password, "pass", "password matches");
  184. assert.equal(credential.realm, null, "realm is null");
  185. assert.equal(credential.formSubmitURL, "http://login.foo.com",
  186. "formSubmitURL matches");
  187. assert.equal(credential.usernameField, "user-f",
  188. "usernameField is matches");
  189. assert.equal(credential.passwordField, "pass-f",
  190. "passwordField matches");
  191. remove({
  192. url: credential.url,
  193. formSubmitURL: credential.formSubmitURL,
  194. username: credential.username,
  195. password: credential.password,
  196. usernameField: credential.usernameField,
  197. passwordField: credential.passwordField,
  198. onComplete: function onComplete() {
  199. assert.pass("credential is removed");
  200. done();
  201. },
  202. onError: function onError(e) {
  203. assert.fail("onError should not be called");
  204. }
  205. });
  206. },
  207. onError: function onError() {
  208. assert.fail("onError should not be called");
  209. }
  210. });
  211. },
  212. onError: function onError() {
  213. assert.fail("onError should not be called");
  214. }
  215. });
  216. };
  217. exports["test site authentication credentials"] = function(assert, done) {
  218. store({
  219. url: "http://authentication.com",
  220. username: "U",
  221. password: "P",
  222. realm: "R",
  223. onComplete: function onComplete() {
  224. search({
  225. url: "http://authentication.com",
  226. username: "U",
  227. password: "P",
  228. realm: "R",
  229. onComplete: function onComplete([credential]) {
  230. assert.equal(credential.url,"http://authentication.com",
  231. "url matches");
  232. assert.equal(credential.username, "U", "username matches");
  233. assert.equal(credential.password, "P", "password matches");
  234. assert.equal(credential.realm, "R", "realm matches");
  235. assert.equal(credential.formSubmitURL, null, "formSubmitURL is null");
  236. assert.equal(credential.usernameField, "", "usernameField is empty");
  237. assert.equal(credential.passwordField, "", "passwordField is empty");
  238. remove({
  239. url: credential.url,
  240. username: credential.username,
  241. password: credential.password,
  242. realm: credential.realm,
  243. onComplete: function onComplete() {
  244. assert.pass("credential is removed");
  245. done();
  246. },
  247. onError: function onError() {
  248. assert.fail("onError should not be called");
  249. }
  250. });
  251. },
  252. onError: function onError() {
  253. assert.fail("onError should not be called");
  254. }
  255. });
  256. },
  257. onError: function onError() {
  258. assert.fail("onError should not be called");
  259. }
  260. });
  261. };
  262. require("test").run(exports);