Public API for a society manager application

society-ops-spec.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. describe("POST /society/leave/", function() {
  229. it("correctly leaves the society", function (done) {
  230. request({
  231. url: base_url + "/society/leave/",
  232. method: "POST",
  233. json: {
  234. society: "foo123soc",
  235. auth: foo456auth
  236. }
  237. }, function (error, response, body) {
  238. expect(response.statusCode).toBe(200);
  239. expect(body.success).toBe(1);
  240. expect(body.error).toBe(0);
  241. done();
  242. });
  243. });
  244. it("does not leave the society twice", function (done) {
  245. request({
  246. url: base_url + "/society/leave/",
  247. method: "POST",
  248. json: {
  249. society: "foo123soc",
  250. auth: foo456auth
  251. }
  252. }, function (error, response, body) {
  253. expect(response.statusCode).toBe(200);
  254. expect(body.success).toBe(0);
  255. expect(body.error).toBe(1);
  256. done();
  257. });
  258. });
  259. it("rejects malformed requests", function (done) {
  260. request({
  261. url: base_url + "/society/leave/",
  262. method: "POST",
  263. json: {
  264. society: "foo123soc",
  265. }
  266. }, function (error, response, body) {
  267. expect(response.statusCode).toBe(200);
  268. expect(body.success).toBe(0);
  269. expect(body.error).toBe(2);
  270. done();
  271. });
  272. });
  273. }); //end POST /society/leave/
  274. describe("POST /society/promote/", function() {
  275. it("rejects invalid admin auth key", function (done) {
  276. request({
  277. url: base_url + "/society/join/",
  278. method: "POST",
  279. json: {
  280. society: "foo123soc",
  281. auth: foo456auth
  282. }
  283. }, function (error, response, body) {
  284. request({
  285. url: base_url + "/society/promote/",
  286. method: "POST",
  287. json: {
  288. user: "foo456",
  289. society: "foo123soc",
  290. auth: "nah"
  291. }
  292. }, function (error, response, body) {
  293. expect(response.statusCode).toBe(200);
  294. expect(body.success).toBe(0);
  295. expect(body.error).toBe(1);
  296. done();
  297. });
  298. });
  299. });
  300. it("successfully promotes a user", function (done) {
  301. request({
  302. url: base_url + "/society/promote/",
  303. method: "POST",
  304. json: {
  305. user: "foo456",
  306. society: "foo123soc",
  307. auth: foo123auth
  308. }
  309. }, function (error, response, body) {
  310. expect(response.statusCode).toBe(200);
  311. expect(body.success).toBe(1);
  312. expect(body.error).toBe(0);
  313. done();
  314. });
  315. });
  316. it("doesn't promote a user twice", function (done) {
  317. request({
  318. url: base_url + "/society/promote/",
  319. method: "POST",
  320. json: {
  321. user: "foo456",
  322. society: "foo123soc",
  323. auth: foo123auth
  324. }
  325. }, function (error, response, body) {
  326. expect(response.statusCode).toBe(200);
  327. expect(body.success).toBe(0);
  328. expect(body.error).toBe(3);
  329. done();
  330. });
  331. });
  332. it("doesn't promote users who aren't in the society", function (done) {
  333. request({
  334. url: base_url + "/society/promote/",
  335. method: "POST",
  336. json: {
  337. user: "foo789",
  338. society: "foo123soc",
  339. auth: foo123auth
  340. }
  341. }, function (error, response, body) {
  342. expect(response.statusCode).toBe(200);
  343. expect(body.success).toBe(0);
  344. expect(body.error).toBe(2);
  345. done();
  346. });
  347. });
  348. it("rejects malformed requests", function (done) {
  349. request({
  350. url: base_url + "/society/promote/",
  351. method: "POST",
  352. json: {
  353. user: "foo456",
  354. society: "foo123soc",
  355. }
  356. }, function (error, response, body) {
  357. expect(response.statusCode).toBe(200);
  358. expect(body.success).toBe(0);
  359. expect(body.error).toBe(4);
  360. done();
  361. });
  362. });
  363. }); //end POST /society/promote/
  364. describe("POST /society/kick/", function() {
  365. it("rejects invalid admin auth key", function (done) {
  366. request({
  367. url: base_url + "/user/auth/",
  368. method: "POST",
  369. json: {
  370. user: "foo789",
  371. password: "foofoo"
  372. }
  373. }, function (error, response, body) {
  374. foo789auth = body["auth-key"];
  375. request({
  376. url: base_url + "/society/join/",
  377. method: "POST",
  378. json: {
  379. society: "foo123soc",
  380. auth: foo789auth
  381. }
  382. }, function (error, response, body) {
  383. request({
  384. url: base_url + "/society/kick/",
  385. method: "POST",
  386. json: {
  387. user: "foo789",
  388. society: "foo123soc",
  389. auth: "nah"
  390. }
  391. }, function (error, response, body) {
  392. expect(response.statusCode).toBe(200);
  393. expect(body.success).toBe(0);
  394. expect(body.error).toBe(1);
  395. done();
  396. });
  397. });
  398. })
  399. });
  400. it("successfully kicks a user", function (done) {
  401. request({
  402. url: base_url + "/society/kick/",
  403. method: "POST",
  404. json: {
  405. user: "foo789",
  406. society: "foo123soc",
  407. auth: foo123auth
  408. }
  409. }, function (error, response, body) {
  410. expect(response.statusCode).toBe(200);
  411. expect(body.success).toBe(1);
  412. expect(body.error).toBe(0);
  413. done();
  414. });
  415. });
  416. it("doesn't kick a user twice", function (done) {
  417. request({
  418. url: base_url + "/society/kick/",
  419. method: "POST",
  420. json: {
  421. user: "foo789",
  422. society: "foo123soc",
  423. auth: foo123auth
  424. }
  425. }, function (error, response, body) {
  426. expect(response.statusCode).toBe(200);
  427. expect(body.success).toBe(0);
  428. expect(body.error).toBe(2);
  429. done();
  430. });
  431. });
  432. it("doesn't kick admins", function (done) {
  433. request({
  434. url: base_url + "/society/kick/",
  435. method: "POST",
  436. json: {
  437. user: "foo456",
  438. society: "foo123soc",
  439. auth: foo123auth
  440. }
  441. }, function (error, response, body) {
  442. expect(response.statusCode).toBe(200);
  443. expect(body.success).toBe(0);
  444. expect(body.error).toBe(3);
  445. done();
  446. });
  447. });
  448. it("rejects malformed requests", function (done) {
  449. request({
  450. url: base_url + "/society/kick/",
  451. method: "POST",
  452. json: {
  453. user: "foo456",
  454. society: "foo123soc",
  455. }
  456. }, function (error, response, body) {
  457. expect(response.statusCode).toBe(200);
  458. expect(body.success).toBe(0);
  459. expect(body.error).toBe(4);
  460. done();
  461. });
  462. });
  463. }); //end POST /society/kick/
  464. });