| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- var request = require("request");
- var base_url = "http://localhost:3000";
- describe("Society Operations", function () {
- var create_society = function (soc_name, auth_key) {
- return {
- url: base_url + "/society/create/",
- method: "POST",
- json: {
- society: soc_name,
- admins: JSON.stringify([]),
- description: "A test society.",
- auth: auth_key
- }
- };
- };
- var foo123auth;
- var foo456auth;
- var foo789auth;
- describe("POST /society/create/", function() {
- it("can create a new society", function(done) {
- request({
- url: base_url + "/user/auth/",
- method: "POST",
- json: {
- user: "foo123",
- password: "foofoo"
- }
- }, function (error, response, body) {
- foo123auth = body["auth-key"];
- request(create_society("foo123soc", foo123auth), function (error, response, body) {
- expect(response.statusCode).toBe(200);
- expect(body.success).toBe(1);
- expect(body.society).not.toBe(null);
- expect(typeof body.society).toBe("object");
- expect(body.society.name).toBe("foo123soc");
- expect(body.society.admins[0]).toBe("foo123");
- expect(body.society.description).toBe("A test society.");
- expect(body.society.users[0]).toBe("foo123");
- expect(body.error).toBe(0);
- done();
- });
- });
- });
- it("cannot create the same society as already exists", function(done) {
- request(create_society("foo123soc", foo123auth), function (error, response, body) {
- expect(response.statusCode).toBe(200);
- expect(body.success).toBe(0);
- expect(body.society).toBe(undefined);
- expect(body.error).toBe(2);
- done();
- })
- });
- it("cannot create a society without valid authentication", function (done) {
- request(create_society("foo12soc", "foo12soc"), function (error, response, body) {
- expect(response.statusCode).toBe(200);
- expect(body.success).toBe(0);
- expect(body.society).toBe(undefined);
- expect(body.error).toBe(3);
- done();
- });
- });
- it("cannot accept malformed requests", function (done) {
- request({
- url: base_url + "/society/create/",
- method: "POST",
- json: {
- yeah: "no"
- }
- }, function (error, response, body) {
- expect(response.statusCode).toBe(200);
- expect(body.success).toBe(0);
- expect(body.society).toBe(undefined);
- expect(body.error).toBe(1);
- done();
- });
- });
- }); //end POST /society/create/
- describe("GET /society/view/:societyid", function () {
- it("shows the full list of societies without an id", function (done) {
- request(base_url + "/society/view/", function (error, response, body) {
- expect(response.statusCode).toBe(200);
- body = JSON.parse(body);
- expect(Array.isArray(body.societies)).toBe(true);
- expect(body.societies.length).toBe(1);
- done();
- });
- });
- it("get an individual society", function (done) {
- request(base_url + "/society/view/foo123soc/", function (error, response, body) {
- body = JSON.parse(body);
- expect(response.statusCode).toBe(200);
- expect(body.society).not.toBe(null);
- expect(typeof body.society).toBe("object");
- expect(body.society.name).toBe("foo123soc");
- expect(body.society.admins[0]).toBe("foo123");
- expect(body.society.description).toBe("A test society.");
- expect(body.society.users[0]).toBe("foo123");
- expect(body.error).toBe(0);
- done();
- });
- });
- it("cannot get a non existant society", function (done) {
- request(base_url + "/society/view/foo12soc", function (error, response, body) {
- body = JSON.parse(body);
- expect(response.statusCode).toBe(200);
- expect(body.society).not.toBe(null);
- expect(typeof body.society).toBe("object");
- expect(JSON.stringify(body.society)).toEqual(JSON.stringify({}));
- done();
- });
- });
- }); //end GET /society/view/:societyid
- describe("GET /society/view/:societyid/events", function () {
- it("shows events for an existing society", function (done) {
- request({
- url: base_url + "/society/view/foo123soc/events/",
- method: "GET",
- qs: {
- auth: foo123auth
- }
- }, function (error, response, body) {
- body = JSON.parse(body);
- expect(response.statusCode).toBe(200);
- expect(Array.isArray(body.events)).toBe(true);
- expect(body.error).toBe(0);
- done();
- });
- });
- it("rejects invalid authentication key", function (done) {
- request({
- url: base_url + "/society/view/foo123soc/events/",
- method: "GET",
- qs: {
- auth: "foo"
- }
- }, function (error, response, body) {
- body = JSON.parse(body);
- expect(response.statusCode).toBe(200);
- expect(Array.isArray(body.events)).toBe(true);
- expect(body.error).toBe(1);
- done();
- });
- });
- it("rejects malformed request", function (done) {
- request({
- url: base_url + "/society/view/foo123soc/events/",
- method: "GET",
- }, function (error, response, body) {
- body = JSON.parse(body);
- expect(response.statusCode).toBe(200);
- expect(Array.isArray(body.events)).toBe(true);
- expect(body.error).toBe(2);
- done();
- });
- });
- }); //end GET /society/view/:societyid/events
- describe("POST /society/join/", function () {
- it("successfully joins the society", function (done) {
- request({
- url: base_url + "/user/auth/",
- method: "POST",
- json: {
- user: "foo456",
- password: "foofoo"
- }
- }, function (error, response, body) {
- foo456auth = body["auth-key"];
- request({
- url: base_url + "/society/join/",
- method: "POST",
- json: {
- society: "foo123soc",
- auth: foo456auth
- }
- }, function (error, response, body) {
- expect(response.statusCode).toBe(200);
- expect(body.success).toBe(1);
- expect(body.error).toBe(0);
- done();
- });
- });
- });
- it("doesn't join the society twice", function (done) {
- request({
- url: base_url + "/society/join/",
- method: "POST",
- json: {
- society: "foo123soc",
- auth: foo456auth
- }
- }, function (error, response, body) {
- expect(response.statusCode).toBe(200);
- expect(body.success).toBe(0);
- expect(body.error).toBe(1);
- done();
- });
- });
- it("rejects invalid authentication keys", function (done) {
- request({
- url: base_url + "/society/join/",
- method: "POST",
- json: {
- society: "foo123soc",
- auth: "nah"
- }
- }, function (error, response, body) {
- expect(response.statusCode).toBe(200);
- expect(body.success).toBe(0);
- expect(body.error).toBe(3);
- done();
- });
- });
- it("rejects malformed requests", function (done) {
- request({
- url: base_url + "/society/join/",
- method: "POST",
- json: {
- society: "foo123soc",
- }
- }, function (error, response, body) {
- expect(response.statusCode).toBe(200);
- expect(body.success).toBe(0);
- expect(body.error).toBe(2);
- done();
- });
- });
- }); //end POST /society/join/
- describe("POST /society/leave/", function() {
- it("correctly leaves the society", function (done) {
- request({
- url: base_url + "/society/leave/",
- method: "POST",
- json: {
- society: "foo123soc",
- auth: foo456auth
- }
- }, function (error, response, body) {
- expect(response.statusCode).toBe(200);
- expect(body.success).toBe(1);
- expect(body.error).toBe(0);
- done();
- });
- });
- it("does not leave the society twice", function (done) {
- request({
- url: base_url + "/society/leave/",
- method: "POST",
- json: {
- society: "foo123soc",
- auth: foo456auth
- }
- }, function (error, response, body) {
- expect(response.statusCode).toBe(200);
- expect(body.success).toBe(0);
- expect(body.error).toBe(1);
- done();
- });
- });
- it("rejects malformed requests", function (done) {
- request({
- url: base_url + "/society/leave/",
- method: "POST",
- json: {
- society: "foo123soc",
- }
- }, function (error, response, body) {
- expect(response.statusCode).toBe(200);
- expect(body.success).toBe(0);
- expect(body.error).toBe(2);
- done();
- });
- });
- }); //end POST /society/leave/
- });
|