Public API for a society manager application

society-ops-spec.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. describe("GET /society/view/:societyid/events", function () {
  114. it("shows events for an existing society", function (done) {
  115. request({
  116. url: base_url + "/society/view/foo123soc/events/",
  117. method: "GET",
  118. qs: {
  119. auth: foo123auth
  120. }
  121. }, function (error, response, body) {
  122. body = JSON.parse(body);
  123. expect(response.statusCode).toBe(200);
  124. expect(Array.isArray(body.events)).toBe(true);
  125. expect(body.error).toBe(0);
  126. done();
  127. });
  128. });
  129. it("rejects invalid authentication key", function (done) {
  130. request({
  131. url: base_url + "/society/view/foo123soc/events/",
  132. method: "GET",
  133. qs: {
  134. auth: "foo"
  135. }
  136. }, function (error, response, body) {
  137. body = JSON.parse(body);
  138. expect(response.statusCode).toBe(200);
  139. expect(Array.isArray(body.events)).toBe(true);
  140. expect(body.error).toBe(1);
  141. done();
  142. });
  143. });
  144. it("rejects malformed request", function (done) {
  145. request({
  146. url: base_url + "/society/view/foo123soc/events/",
  147. method: "GET",
  148. }, function (error, response, body) {
  149. body = JSON.parse(body);
  150. expect(response.statusCode).toBe(200);
  151. expect(Array.isArray(body.events)).toBe(true);
  152. expect(body.error).toBe(2);
  153. done();
  154. });
  155. });
  156. }); //end GET /society/view/:societyid/events
  157. describe("POST /society/join/", function () {
  158. it("successfully joins the society", function (done) {
  159. request({
  160. url: base_url + "/user/auth/",
  161. method: "POST",
  162. json: {
  163. user: "foo456",
  164. password: "foofoo"
  165. }
  166. }, function (error, response, body) {
  167. foo456auth = body["auth-key"];
  168. request({
  169. url: base_url + "/society/join/",
  170. method: "POST",
  171. json: {
  172. society: "foo123soc",
  173. auth: foo456auth
  174. }
  175. }, function (error, response, body) {
  176. expect(response.statusCode).toBe(200);
  177. expect(body.success).toBe(1);
  178. expect(body.error).toBe(0);
  179. done();
  180. });
  181. });
  182. });
  183. it("doesn't join the society twice", function (done) {
  184. request({
  185. url: base_url + "/society/join/",
  186. method: "POST",
  187. json: {
  188. society: "foo123soc",
  189. auth: foo456auth
  190. }
  191. }, function (error, response, body) {
  192. expect(response.statusCode).toBe(200);
  193. expect(body.success).toBe(0);
  194. expect(body.error).toBe(1);
  195. done();
  196. });
  197. });
  198. it("rejects invalid authentication keys", function (done) {
  199. request({
  200. url: base_url + "/society/join/",
  201. method: "POST",
  202. json: {
  203. society: "foo123soc",
  204. auth: "nah"
  205. }
  206. }, function (error, response, body) {
  207. expect(response.statusCode).toBe(200);
  208. expect(body.success).toBe(0);
  209. expect(body.error).toBe(3);
  210. done();
  211. });
  212. });
  213. it("rejects malformed requests", function (done) {
  214. request({
  215. url: base_url + "/society/join/",
  216. method: "POST",
  217. json: {
  218. society: "foo123soc",
  219. }
  220. }, function (error, response, body) {
  221. expect(response.statusCode).toBe(200);
  222. expect(body.success).toBe(0);
  223. expect(body.error).toBe(2);
  224. done();
  225. });
  226. });
  227. }); //end POST /society/join/
  228. });