api.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. (function () {
  2. 'use strict';
  3. var service = getMisagoService('api');
  4. var container = {
  5. setup: {
  6. api: '/test-api/'
  7. }
  8. };
  9. QUnit.module("API", {
  10. afterEach: function() {
  11. $.mockjax.clear();
  12. }
  13. });
  14. QUnit.test("service factory", function(assert) {
  15. var api = service({});
  16. assert.ok(api, "service factory has returned service instance.");
  17. });
  18. QUnit.test("model", function(assert) {
  19. var api = service(container);
  20. assert.equal(api.model('user', 'admin').url, '/test-api/users/admin/',
  21. "model constructed valid url with string pk.");
  22. assert.equal(api.model('user', 123).url, '/test-api/users/123/',
  23. "model constructed valid url with integer pk.");
  24. assert.equal(
  25. api.model('user', {token: 'abc&df', uid: 123}).url,
  26. '/test-api/users/?token=abc%26df&uid=123',
  27. "model constructed valid url with querystring.");
  28. assert.equal(api.model('user', 123).related('follows', 1).url,
  29. '/test-api/users/123/follows/1/',
  30. "model constructed valid related url.");
  31. assert.equal(api.model('user', 123).endpoint('avatar').url,
  32. '/test-api/users/123/avatar/',
  33. "model constructed valid endpoint url.");
  34. assert.ok(
  35. !api.model('user', 123).endpoint('avatar').related,
  36. "model can't have relation to endpoint.");
  37. assert.ok(
  38. !api.model('user', 123).related('avatar').related,
  39. "model can't have relation to relation.");
  40. });
  41. QUnit.test("endpoint", function(assert) {
  42. var api = service(container);
  43. assert.equal(api.endpoint('auth').url, '/test-api/auth/',
  44. "endpoint constructed valid url.");
  45. assert.equal(
  46. api.endpoint('auth', 'string-pk').url,
  47. '/test-api/auth/string-pk/',
  48. "endpoint constructed valid url with string pk.");
  49. assert.equal(
  50. api.endpoint('auth', 124).url,
  51. '/test-api/auth/124/',
  52. "endpoint constructed valid url with integer pk.");
  53. assert.equal(
  54. api.endpoint('auth', {token: 'abc&df', uid: 123}).url,
  55. '/test-api/auth/?token=abc%26df&uid=123',
  56. "endpoint constructed valid url with querystring.");
  57. assert.ok(
  58. !api.endpoint('auth', 124).related,
  59. "endpoint can't have nested relation.");
  60. assert.equal(
  61. api.endpoint('auth', 124).endpoint('change-password').url,
  62. '/test-api/auth/124/change-password/',
  63. "nested endpoint constructed valid url with integer pk.");
  64. assert.ok(
  65. !api.endpoint('auth', 124).endpoint('change-password').endpoint,
  66. "nested endpoint can't be nested further.");
  67. });
  68. QUnit.test("alert", function(assert) {
  69. var unknownError = assert.async();
  70. var disconnectedError = assert.async();
  71. var deniedError = assert.async();
  72. var notFoundError = assert.async();
  73. var container = {
  74. setup: {
  75. api: '/test-api/'
  76. },
  77. alert: {
  78. error: function(message) {
  79. if (message === "Unknown error has occured.") {
  80. assert.ok(true, "unknown error was handled.");
  81. unknownError();
  82. }
  83. if (message === "Lost connection with application.") {
  84. assert.ok(true, "error 0 was handled.");
  85. disconnectedError();
  86. }
  87. if (message === "You don't have permission to perform this action.") {
  88. assert.ok(true, "error 403 was handled.");
  89. deniedError();
  90. }
  91. if (message === "Action link is invalid.") {
  92. assert.ok(true, "error 404 was handled.");
  93. notFoundError();
  94. }
  95. }
  96. }
  97. };
  98. var api = service(container);
  99. api.alert({
  100. status: 500
  101. });
  102. api.alert({
  103. status: 0
  104. });
  105. api.alert({
  106. status: 403,
  107. detail: "Permission denied"
  108. });
  109. api.alert({
  110. status: 404,
  111. detail: "Not found"
  112. });
  113. });
  114. }());