Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions models/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,26 @@ Chatter.Message = ChatterMessage = Astro.Class({

fields: {
userId: {
type: "string"
type: "string",
validator: [
reqStrNotNull
]
},

roomId: {
type: "string"
type: "string",
validator: [
reqStrNotNull
]
},

message: {
type: "string"
type: "string",
validator: [
Validators.minLength(1, 'The message must not be empty!'),
Validators.maxLength(1000),
reqStrNotNull
]
},

nickname: {
Expand All @@ -30,22 +41,6 @@ Chatter.Message = ChatterMessage = Astro.Class({
}
},

validators: {
userId: [
reqStrNotNull
],

roomId: [
reqStrNotNull
],

message: [
Validators.minLength(1, 'The message must not be empty!'),
Validators.maxLength(1000),
reqStrNotNull
]
},

events: {
beforeSave: function(e) {
increaseCounter(this);
Expand Down
39 changes: 16 additions & 23 deletions models/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@ Chatter.Room = ChatterRoom = Astro.Class({
collection: new Mongo.Collection("chatterroom"),

fields: {
name: "string",
name: {
type: "string",
validator: [
Validators.required(),
Validators.minLength(1),
Validators.maxLength(30)
]
},

description: "string",
description: {
type: "string",
validator: [
Validators.required(),
Validators.minLength(1),
Validators.maxLength(150)
]
},

roomType: {
type: "string"
Expand All @@ -18,30 +32,9 @@ Chatter.Room = ChatterRoom = Astro.Class({
}
},

archived: {
type: "boolean",
default: function() {
return false;
}
},

createdBy: "string"
},

validators: {
name: [
Validators.required(),
Validators.minLength(1),
Validators.maxLength(30)
],

description: [
Validators.required(),
Validators.minLength(1),
Validators.maxLength(150)
]
},

events: {

},
Expand Down
9 changes: 8 additions & 1 deletion models/userroom.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ Chatter.UserRoom = ChatterUserRoom = Astro.Class({
default: function() {
return 0;
}
}
},

archived: {
type: "boolean",
default: function() {
return false;
}
},
},

validators: {
Expand Down
164 changes: 85 additions & 79 deletions server-tests/methods.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const callbackWrapper = function(fn) {
describe("chatter meteor methods", function() {
let room;
let user;
let userRoom;
const assert = chai.assert;

const roomAttributes = {
Expand All @@ -20,7 +21,7 @@ describe("chatter meteor methods", function() {
user = Meteor.users.findOne();
const roomId = new Chatter.Room({name: "test_room" }).save();
room = Chatter.Room.findOne(roomId)
const userRoom = new Chatter.UserRoom({userId: user._id, roomId: roomId }).save();
userRoom = new Chatter.UserRoom({userId: user._id, roomId: roomId }).save();
// Simulate user has been added to chatter and has made admin
stubs.findOne.returns({
_id: "meteor_user_one_id",
Expand Down Expand Up @@ -143,6 +144,74 @@ describe("chatter meteor methods", function() {
}));
});
});
});

describe("userRoom methods", function() {

describe("room.join method", function() {

describe("when parameters are missing or wrong", function() {

it("returns exception when parameters are missing ", function(done) {
const params = {};
Meteor.call("room.join", params, callbackWrapper((error, response) => {
assert.isUndefined(response);
assert.equal(error.errorType, "Match.Error");
}));

params.roomId = room._id;

Meteor.call("room.join", params, callbackWrapper((error, response) => {
assert.isUndefined(response);
assert.equal(error.errorType, "Match.Error");
done();
}));
});

it("returns 'room does not exist' error when roomId does not exist", function(done) {
const params = {};
params.roomId = "non existent roomId";
params.invitees = [user._id];

Meteor.call("room.join", params, callbackWrapper((error, response) => {
assert.isUndefined(response);
assert.equal(error.error, "non-existing-room");
done();
}));
});

it("returns 'user does not exist' error when userId does not exist", function(done) {
const params = {};
params.invitees = ["non existent userId"];
params.roomId = room._id;

Meteor.call("room.join", params, callbackWrapper((error, response) => {
assert.isUndefined(response);
assert.equal(error.error, "non-existing-users");
done();
}));
});
});

describe("when no parameters are missing or wrong", function() {

it("creates a userRoom instance", function(done) {
const params = {};
params.invitees = [user._id];
params.roomId = room._id;

Meteor.call("room.join", params, callbackWrapper((error, response) => {
assert.isUndefined(error);
assert.isString(response);
done();
}));
});

it("userRoom is built with archived set to default", function() {
const ur = assert.equal(Chatter.UserRoom.findOne({userId: user._id, roomId: room._id}).archived, false);
});
});
});

describe("room.archive", function() {

Expand Down Expand Up @@ -171,28 +240,27 @@ describe("chatter meteor methods", function() {
}));
});

it("throws an error when roomId does not exist", function(done) {
it("throws an error when user is not in room does not exist", function(done) {
const params = {
roomId: "non-existing-room",
userId: user._id,
archived: true
};

Meteor.call("room.archive", params, callbackWrapper((error, response) => {
assert.isUndefined(response);
assert.equal(error.error, "non-existing-room");
assert.equal(error.error, "user-not-in-room");
done();
}));
});

it("room is built with archived set to default", function() {
assert.equal(room.archived, false);
});

it("returns true after succesfully calling method with true parameter", function(done) {
const params = {
archived: true,
roomId: room._id
roomId: room._id,
userId: user._id
};

Meteor.call("room.archive", params, callbackWrapper((error, response) => {
assert.isUndefined(error);
assert.equal(response, true);
Expand All @@ -201,87 +269,25 @@ describe("chatter meteor methods", function() {
});

it("actually changed archived status to true", function() {
assert.equal(Chatter.Room.findOne(room._id).archived, true);
assert.equal(Chatter.UserRoom.findOne({userId: user._id, roomId: room._id}).archived, true);
});

it("returns false after succesfully calling method with false parameter", function(done) {
const params = {
archived: false,
roomId: room._id
roomId: room._id,
userId: user._id
};

Meteor.call("room.archive", params, callbackWrapper((error, response) => {
assert.isUndefined(error);
assert.equal(response, false);
done();
}));
});

it("actually changed archived status to true", function() {
assert.equal(Chatter.Room.findOne(room._id).archived, false);
});
});
});

describe("userRoom methods", function() {

describe("room.join method", function() {

describe("when parameters are missing or wrong", function() {

it("returns exception when parameters are missing ", function(done) {
const params = {};
Meteor.call("room.join", params, callbackWrapper((error, response) => {
assert.isUndefined(response);
assert.equal(error.errorType, "Match.Error");
}));

params.roomId = room._id;

Meteor.call("room.join", params, callbackWrapper((error, response) => {
assert.isUndefined(response);
assert.equal(error.errorType, "Match.Error");
done();
}));
});

it("returns 'room does not exist' error when roomId does not exist", function(done) {
const params = {};
params.roomId = "non existent roomId";
params.invitees = [user._id];

Meteor.call("room.join", params, callbackWrapper((error, response) => {
assert.isUndefined(response);
assert.equal(error.error, "non-existing-room");
done();
}));
});

it("returns 'user does not exist' error when userId does not exist", function(done) {
const params = {};
params.invitees = ["non existent userId"];
params.roomId = room._id;

Meteor.call("room.join", params, callbackWrapper((error, response) => {
assert.isUndefined(response);
assert.equal(error.error, "non-existing-users");
done();
}));
});
});

describe("when no parameters are missing or wrong", function() {

it("creates a userRoom instance", function(done) {
const params = {};
params.invitees = [user._id];
params.roomId = room._id;

Meteor.call("room.join", params, callbackWrapper((error, response) => {
assert.isUndefined(error);
assert.isString(response);
done();
}));
});
it("actually changed archived status to false", function() {
assert.equal(Chatter.UserRoom.findOne({userId: user._id, roomId: room._id}).archived, false);
});
});

Expand Down Expand Up @@ -313,7 +319,7 @@ describe("chatter meteor methods", function() {

it("room counter returns true when called with right parameters", function(done) {

Meteor.call("room.counter.reset", room._id , callbackWrapper((error, response) => {
Meteor.call("room.unreadMsgCount.reset", room._id , callbackWrapper((error, response) => {
assert.isUndefined(error);
assert.equal(response, true);
done()
Expand All @@ -326,7 +332,7 @@ describe("chatter meteor methods", function() {

it("room counter return error when wrong roomId is passed in", function(done) {

Meteor.call("room.counter.reset", "wrong room id" , callbackWrapper((error, response) => {
Meteor.call("room.unreadMsgCount.reset", "wrong room id" , callbackWrapper((error, response) => {
assert.isUndefined(response);
assert.equal(error.error, "non-existing-room");
done()
Expand All @@ -336,7 +342,7 @@ describe("chatter meteor methods", function() {
it("room counter returns error when user is not in room", function(done) {
Chatter.UserRoom.remove({userId: user._id, roomId: room._id});

Meteor.call("room.counter.reset", room._id , callbackWrapper((error, response) => {
Meteor.call("room.unreadMsgCount.reset", room._id , callbackWrapper((error, response) => {
assert.isUndefined(response);
assert.equal(error.error, "user-not-in-room");
done()
Expand Down
3 changes: 2 additions & 1 deletion server-tests/models.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ describe("chatter models", function() {
});

it("room is inserted with correct defaults", function() {
assert.equal(room.archived, false);
const now = new Date();
assert.equal(now.getTime() - room.lastActive.getTime() < 20000, true);
});
Expand All @@ -91,6 +90,8 @@ describe("chatter models", function() {
it("User Room is inserted with correct attributes", function() {
assert.equal(userRoom.userId, attributes.userId);
assert.equal(userRoom.roomId, attributes.roomId);
assert.equal(userRoom.archived, false);

});

it("User Room is inserted with correct defaults", function() {
Expand Down
Loading