Public API for a society manager application

society-ops-spec.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. var request = require("request");
  2. var base_url = "http://localhost:3000";
  3. describe("Society Operations", function () {
  4. var create_society = function (soc_name, auth_key) {
  5. return {
  6. url: base_url + "/society/create/",
  7. method: "POST",
  8. json: {
  9. society: soc_name,
  10. admins: JSON.stringify([]),
  11. description: "A test society.",
  12. auth: auth_key
  13. }
  14. };
  15. };
  16. var foo123auth;
  17. var foo456auth;
  18. var foo789auth;
  19. describe("POST /society/create/", function() {
  20. it("can create a new society", function(done) {
  21. request({
  22. url: base_url + "/user/auth/",
  23. method: "POST",
  24. json: {
  25. user: "foo123",
  26. password: "foofoo"
  27. }
  28. }, function (error, response, body) {
  29. foo123auth = body["auth-key"];
  30. request(create_society("foo123soc", foo123auth), function (error, response, body) {
  31. expect(response.statusCode).toBe(200);
  32. expect(body.success).toBe(1);
  33. expect(body.society).not.toBe(null);
  34. expect(typeof body.society).toBe("object");
  35. expect(body.society.name).toBe("foo123soc");
  36. expect(body.society.admins[0]).toBe("foo123");
  37. expect(body.society.description).toBe("A test society.");
  38. expect(body.society.users[0]).toBe("foo123");
  39. expect(body.error).toBe(0);
  40. done();
  41. });
  42. });
  43. });
  44. it("cannot create the same society as already exists", function(done) {
  45. request(create_society("foo123soc", foo123auth), function (error, response, body) {
  46. expect(response.statusCode).toBe(200);
  47. expect(body.success).toBe(0);
  48. expect(body.society).toBe(undefined);
  49. expect(body.error).toBe(2);
  50. done();
  51. })
  52. });
  53. it("cannot create a society without valid authentication", function (done) {
  54. request(create_society("foo12soc", "foo12soc"), function (error, response, body) {
  55. expect(response.statusCode).toBe(200);
  56. expect(body.success).toBe(0);
  57. expect(body.society).toBe(undefined);
  58. expect(body.error).toBe(3);
  59. done();
  60. });
  61. });
  62. it("cannot accept malformed requests", function (done) {
  63. request({
  64. url: base_url + "/society/create/",
  65. method: "POST",
  66. json: {
  67. yeah: "no"
  68. }
  69. }, function (error, response, body) {
  70. expect(response.statusCode).toBe(200);
  71. expect(body.success).toBe(0);
  72. expect(body.society).toBe(undefined);
  73. expect(body.error).toBe(1);
  74. done();
  75. });
  76. });
  77. }); //end POST /society/create/
  78. describe("GET /society/view/:societyid", function () {
  79. it("shows the full list of societies without an id", function (done) {
  80. request(base_url + "/society/view/", function (error, response, body) {
  81. expect(response.statusCode).toBe(200);
  82. body = JSON.parse(body);
  83. expect(Array.isArray(body.societies)).toBe(true);
  84. expect(body.societies.length).toBe(1);
  85. done();
  86. });
  87. });
  88. it("get an individual society", function (done) {
  89. request(base_url + "/society/view/foo123soc/", function (error, response, body) {
  90. body = JSON.parse(body);
  91. expect(response.statusCode).toBe(200);
  92. expect(body.society).not.toBe(null);
  93. expect(typeof body.society).toBe("object");
  94. expect(body.society.name).toBe("foo123soc");
  95. expect(body.society.admins[0]).toBe("foo123");
  96. expect(body.society.description).toBe("A test society.");
  97. expect(body.society.users[0]).toBe("foo123");
  98. expect(body.error).toBe(0);
  99. done();
  100. });
  101. });
  102. it("cannot get a non existant society", function (done) {
  103. request(base_url + "/society/view/foo12soc", function (error, response, body) {
  104. body = JSON.parse(body);
  105. expect(response.statusCode).toBe(200);
  106. expect(body.society).not.toBe(null);
  107. expect(typeof body.society).toBe("object");
  108. expect(JSON.stringify(body.society)).toEqual(JSON.stringify({}));
  109. done();
  110. });
  111. });
  112. }); //end GET /society/view/:societyid
  113. });