ElggSecurityTest.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. define(function(require) {
  2. var elgg = require('elgg');
  3. describe('elgg.security', function() {
  4. var ts, token;
  5. beforeEach(function() {
  6. ts = elgg.security.token.__elgg_ts = 12345;
  7. token = elgg.security.token.__elgg_token = 'abcdef';
  8. });
  9. describe("setToken", function() {
  10. it("sets global security token state", function() {
  11. var json = {
  12. __elgg_ts: 4567,
  13. __elgg_token: 'abcdef'
  14. };
  15. elgg.security.setToken(json);
  16. expect(elgg.security.token).toBe(json);
  17. });
  18. });
  19. describe("addToken", function() {
  20. it("accepts undefined", function() {
  21. var expected = {
  22. __elgg_ts: ts,
  23. __elgg_token: token
  24. };
  25. expect(elgg.security.addToken(undefined)).toEqual(expected);
  26. });
  27. it("accepts an object", function() {
  28. var expected = {
  29. __elgg_ts: ts,
  30. __elgg_token: token
  31. };
  32. expect(elgg.security.addToken({})).toEqual(expected);
  33. });
  34. it("accepts relative urls", function() {
  35. var str = "__elgg_ts=" + ts + "&__elgg_token=" + token;
  36. expect(elgg.security.addToken("/test"), '/test?' + str);
  37. });
  38. it("accepts full urls", function() {
  39. var str = "__elgg_ts=" + ts + "&__elgg_token=" + token;
  40. var url = "http://elgg.org/";
  41. expect(elgg.security.addToken(url)).toEqual(url + '?' + str);
  42. });
  43. it("accepts query strings", function() {
  44. var str = "__elgg_ts=" + ts + "&__elgg_token=" + token;
  45. var url;
  46. url = "?data=sofar";
  47. expect(elgg.security.addToken(url), url + '&' + str);
  48. url = "test?data=sofar";
  49. expect(elgg.security.addToken(url), url + '&' + str);
  50. url = "http://elgg.org/?data=sofar";
  51. expect(elgg.security.addToken(url), url + '&' + str);
  52. });
  53. it("overwrites existing query string tokens", function() {
  54. var expectedUrl = "http://elgg.org/?__elgg_ts=" + ts + "&__elgg_token=" + token + "&data=sofar";
  55. var inputUrl = "http://elgg.org/?__elgg_ts=54321&__elgg_token=fedcba&data=sofar"
  56. expect(elgg.security.addToken(inputUrl)).toBe(expectedUrl);
  57. });
  58. });
  59. });
  60. });