示例#1
0
func TestChannelParticipantBeforeUpdate(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		Convey("While testing before update", t, func() {
			Convey("LastSeenAt should be updated", func() {
				lastSeenAt := time.Now().UTC()

				account, err := createAccount()
				So(err, ShouldBeNil)
				So(account, ShouldNotBeNil)
				So(account.Id, ShouldNotEqual, 0)

				c := NewChannelParticipant()
				c.LastSeenAt = lastSeenAt
				c.AccountId = account.Id

				// call before update
				err = c.BeforeUpdate()

				// make sure err is nil
				So(err, ShouldBeNil)
				// check preset last seen at is not same after calling
				// before update function
				So(c.LastSeenAt, ShouldNotEqual, lastSeenAt)
			})
		})
	})
}
示例#2
0
func TestChannelIsParticipant(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {

		Convey("while controlling participant is in the channel or not", t, func() {

			Convey("participant in the channel should not give error", func() {
				acc := CreateAccountWithTest()
				c := CreateChannelWithTest(acc.Id)

				// add the created account to the channel
				ap, err := c.AddParticipant(acc.Id)
				So(err, ShouldBeNil)
				So(ap, ShouldNotBeNil)

				part, err := c.IsParticipant(acc.Id)
				So(err, ShouldBeNil)
				So(part, ShouldBeTrue)
			})

			Convey("non-participant in the channel should not give error", func() {
				acc := CreateAccountWithTest()
				c := CreateChannelWithTest(acc.Id)

				// account is created but didn't add to the channel
				// it means that participant is not in the channel
				part, err := c.IsParticipant(acc.Id)
				So(err, ShouldBeNil)
				So(part, ShouldBeFalse)
			})
		})
	})
}
示例#3
0
func TestChannelFetchAllParticipatedChannelIdsInGroup(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		Convey("fetching all channels of an account by group name should succeed", t, func() {
			// create account
			acc := CreateAccountWithTest()
			acc.IsTroll = false
			So(acc.Update(), ShouldBeNil)

			groupName1 := RandomGroupName()
			groupName2 := RandomGroupName()
			for i := 0; i < 5; i++ {
				c := CreateTypedGroupedChannelWithTest(acc.Id, Channel_TYPE_TOPIC, groupName1)
				AddParticipantsWithTest(c.Id, acc.Id)
			}

			for i := 0; i < 5; i++ {
				c := CreateTypedGroupedChannelWithTest(acc.Id, Channel_TYPE_TOPIC, groupName2)
				AddParticipantsWithTest(c.Id, acc.Id)
			}

			cp := NewChannelParticipant()
			ids, err := cp.FetchAllParticipatedChannelIdsInGroup(acc.Id, groupName1)
			So(err, ShouldBeNil)
			So(len(ids), ShouldEqual, 5)

			Convey("fetching non participated channel, should return 0", func() {
				ids, err := cp.FetchAllParticipatedChannelIdsInGroup(acc.Id, RandomGroupName())
				So(err, ShouldBeNil)
				So(len(ids), ShouldEqual, 0)
			})
		})
	})
}
示例#4
0
func TestChannelMessageBuildEmptyMessageContainer(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		Convey("while building empty message container", t, func() {
			Convey("it should have channel message id", func() {
				c := NewChannelMessage()

				_, err := c.BuildEmptyMessageContainer()
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, ErrChannelMessageIdIsNotSet)
			})

			Convey("it should not have error if chanel is exist", func() {
				// create message
				c := CreateMessageWithTest()
				So(c.Create(), ShouldBeNil)

				bem, err := c.BuildEmptyMessageContainer()
				So(err, ShouldBeNil)
				So(bem.Message, ShouldEqual, c)
			})
			Convey("it should not return error when account is not set", func() {
				// create message
				c := CreateMessageWithTest()
				So(c.Create(), ShouldBeNil)

				cm := NewChannelMessage()
				cm.Id = c.Id

				bem, err := cm.BuildEmptyMessageContainer()
				So(err, ShouldBeNil)
				So(bem.Message, ShouldEqual, cm)
			})
		})
	})
}
示例#5
0
func TestChannelMessageGetAccountId(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		Convey("while getting account id ", t, func() {
			Convey("it should have channel id", func() {
				cm := NewChannelMessage()

				_, err := cm.getAccountId()
				So(err, ShouldNotBeNil)
				So(err.Error(), ShouldContainSubstring, "couldnt find accountId from content")
			})

			Convey("it should have error if account is not set", func() {
				cm := NewChannelMessage()
				cm.Id = 13531

				_, err := cm.getAccountId()
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, bongo.RecordNotFound)
			})

			Convey("it should not have error if channel & account are exist", func() {
				cm := NewChannelMessage()
				cm.Id = 2454
				cm.AccountId = 4353

				gid, err := cm.getAccountId()
				So(err, ShouldBeNil)
				So(gid, ShouldEqual, cm.AccountId)
			})
		})
	})
}
示例#6
0
func TestPresenceCancelledStatus(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		Convey("With given presence data", t, func() {
			account, _, groupSlug := models.CreateRandomGroupDataWithChecks()
			Convey("should work properly with invalid payment status", func() {
				today := time.Now().UTC()
				ping := &Ping{
					AccountID:     account.Id,
					GroupName:     groupSlug,
					CreatedAt:     today,
					paymentStatus: "invalid",
				}

				err := verifyRecord(ping, today)
				So(err, ShouldBeNil)

				// we should be able to get it from db
				pd, err := getPresenceInfoFromDB(ping)
				So(err, ShouldBeNil)
				So(pd, ShouldNotBeNil)
				So(pd.IsProcessed, ShouldBeTrue)
			})
		})
	})
}
示例#7
0
func TestChannelMessageListCount(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		Convey("while counting messages", t, func() {
			Convey("it should error if channel id is not set", func() {
				cml := NewChannelMessageList()

				c, err := cml.Count(0)
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, ErrChannelIdIsNotSet)
				So(c, ShouldEqual, 0)
			})

			Convey("it should not error if message in the channel", func() {
				// create message
				cm := CreateMessageWithTest()
				So(cm.Create(), ShouldBeNil)

				acc := CreateAccountWithTest()
				c := CreateChannelWithTest(acc.Id)

				_, err := c.AddMessage(cm)
				So(err, ShouldBeNil)

				cml := NewChannelMessageList()
				cml.ChannelId = c.Id

				cnt, err := cml.Count(cml.ChannelId)
				So(err, ShouldBeNil)
				So(cnt, ShouldEqual, 1)
			})
		})
	})
}
示例#8
0
func TestChannelFetchParticipant(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {

		Convey("while fetching the participants from the channel", t, func() {

			Convey("it should have channel id", func() {
				c := NewChannel()
				_, err := c.FetchParticipant(123)
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, ErrIdIsNotSet)
			})

			Convey("it should have account id", func() {
				c := NewChannel()
				c.Id = 123
				_, err := c.FetchParticipant(0)
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, ErrAccountIdIsNotSet)
			})

			Convey("participant in the channel should not give error", func() {
				acc := CreateAccountWithTest()
				c := CreateChannelWithTest(acc.Id)

				// add account to the channel
				ap, err := c.AddParticipant(acc.Id)
				So(err, ShouldBeNil)
				So(ap, ShouldNotBeNil)

				fp, err := c.FetchParticipant(acc.Id)
				So(err, ShouldBeNil)
				So(fp, ShouldNotBeNil)
				So(fp.AccountId, ShouldEqual, acc.Id)
				So(fp.ChannelId, ShouldEqual, c.Id)
			})

			Convey("non-participant in the channel's status should be left after removing from channel ", func() {
				acc := CreateAccountWithTest()
				c := CreateChannelWithTest(acc.Id)

				// add account to the channel
				ap, err := c.AddParticipant(acc.Id)
				So(err, ShouldBeNil)
				So(ap, ShouldNotBeNil)
				//
				// remove participant from the channel
				err = c.RemoveParticipant(acc.Id)
				So(err, ShouldBeNil)

				// after adding and removing the user to/from channel
				// status constant should be LEFT from the channel
				// not deleted! frmo the channel
				fp, err := c.FetchParticipant(acc.Id)
				So(err, ShouldBeNil)
				So(fp, ShouldNotBeNil)
				So(fp.StatusConstant, ShouldEqual, ChannelParticipant_STATUS_LEFT)
			})
		})
	})
}
示例#9
0
func TestAccountFetchOrCreate(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {

		Convey("while fetching or creating account", t, func() {
			Convey("it should have old id", func() {
				a := NewAccount()
				err := a.FetchOrCreate()
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, ErrOldIdIsNotSet)
			})

			Convey("it should have error if nick contains guest-", func() {
				a := NewAccount()
				a.OldId = "oldestId"
				a.Nick = "guest-test"
				err := a.FetchOrCreate()
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, ErrGuestsAreNotAllowed)
			})

			Convey("it should not have error if required fields are exist", func() {
				a := NewAccount()
				a.OldId = "oldIdOfAccount"
				a.Nick = "WhatANick"
				err := a.FetchOrCreate()
				So(err, ShouldBeNil)
			})

		})
	})
}
示例#10
0
func TestAccountFetchProfile(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		Convey("while fetching account activities in profile page", t, func() {
			account, _, groupName := models.CreateRandomGroupDataWithChecks()

			ses, err := modelhelper.FetchOrCreateSession(account.Nick, groupName)
			So(err, ShouldBeNil)
			So(ses, ShouldNotBeNil)

			// create channel
			channel, err := rest.CreateChannelByGroupNameAndType(account.Id, groupName, models.Channel_TYPE_GROUP, ses.ClientId)
			So(err, ShouldBeNil)
			So(channel, ShouldNotBeNil)

			// create message
			post, err := rest.CreatePost(channel.Id, ses.ClientId)
			So(err, ShouldBeNil)
			So(post, ShouldNotBeNil)

			Convey("it should list latest posts when there is no time interval in query", func() {
				cmc, err := rest.FetchAccountActivities(account.Id, ses.ClientId)
				So(err, ShouldBeNil)
				So(len(cmc), ShouldEqual, 1)
				So(cmc[0].Message.Body, ShouldEqual, post.Body)
			})
		})
	})
}
示例#11
0
func TestAccountFetchChannel(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {

		Convey("while fetching channel", t, func() {
			Convey("it should have account id", func() {
				acc := NewAccount()

				_, err := acc.FetchChannel("ChannelType")
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, ErrAccountIdIsNotSet)
			})

			Convey("it should have error if channel is not exist", func() {
				acc := NewAccount()
				acc.Id = 35135

				_, err := acc.FetchChannel(Channel_TYPE_GROUP)
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, bongo.RecordNotFound)
			})

			Convey("it should not have error if channel type is exist", func() {
				acc := CreateAccountWithTest()
				c := CreateTypedChannelWithTest(acc.Id, Channel_TYPE_TOPIC)

				cha, err := acc.FetchChannel(Channel_TYPE_TOPIC)
				So(err, ShouldBeNil)
				So(cha.TypeConstant, ShouldEqual, c.TypeConstant)
			})
		})
	})
}
示例#12
0
func TestAccountsByNick(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {

		Convey("while fetching account ids by nicknames", t, func() {
			Convey("it should not fetch any accounts when nicknames are empty", func() {
				nicknames := make([]string, 0)

				accounts, err := FetchAccountsByNicks(nicknames)
				So(err, ShouldBeNil)
				So(len(accounts), ShouldEqual, 0)
			})

			Convey("it should fetch accounts by nicknames", func() {
				acc1 := CreateAccountWithTest()
				acc2 := CreateAccountWithTest()
				nicknames := make([]string, 0)
				nicknames = append(nicknames, acc1.Nick, acc2.Nick)
				accounts, err := FetchAccountsByNicks(nicknames)
				So(err, ShouldBeNil)
				So(accounts, ShouldNotBeNil)
				So(len(accounts), ShouldEqual, 2)
			})
		})
	})
}
示例#13
0
func TestGroupChannelFirstCreation(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		Convey("While creating the new group channel for the first time", t, func() {
			Convey("user should be able to create group channel", func() {
				acc, err := models.CreateAccountInBothDbs()
				So(err, ShouldBeNil)

				groupName := models.RandomGroupName()
				ses, err := modelhelper.FetchOrCreateSession(acc.Nick, groupName)
				So(err, ShouldBeNil)
				So(ses, ShouldNotBeNil)

				channel, err := rest.CreateChannelByGroupNameAndType(
					acc.Id,
					groupName,
					models.Channel_TYPE_GROUP,
					ses.ClientId,
				)

				So(err, ShouldBeNil)
				So(channel.GroupName, ShouldEqual, groupName)
			})
		})
	})
}
示例#14
0
func TestPresenceDailyPing(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		groupName1 := models.RandomGroupName()
		Convey("With given presence data", t, func() {

			Convey("should work properly with non existant data", func() {
				today := time.Now().UTC()
				ping := &Ping{
					AccountID:     1, // non existing user
					GroupName:     groupName1,
					CreatedAt:     today,
					paymentStatus: string(mongomodels.SubStatusActive),
				}
				key := getKey(ping, today)
				_, err := pingCache.Get(key)
				So(err, ShouldEqual, cache.ErrNotFound)

				err = verifyRecord(ping, today)
				So(err, ShouldBeNil)

				// it should not be in cache
				_, err = pingCache.Get(key)
				So(err, ShouldEqual, cache.ErrNotFound)

				// we should be able to get it from db
				pd, err := getPresenceInfoFromDB(ping)
				So(err, ShouldBeNil)
				So(pd, ShouldNotBeNil)
			})
		})
	})
}
示例#15
0
func TestChannelNewPrivateChannel(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {

		var creatorId int64 = 123
		groupName := "testGroup"
		c := NewCollaborationChannel(creatorId, groupName)

		Convey("given a NewPrivateMessageChannel", t, func() {
			Convey("it should have group name", func() {
				So(c.GroupName, ShouldEqual, groupName)
			})

			Convey("it should have creator id", func() {
				So(c.CreatorId, ShouldEqual, creatorId)
			})

			Convey("it should have a name", func() {
				So(c.Name, ShouldNotBeBlank)
			})

			Convey("it should have type constant", func() {
				So(c.TypeConstant, ShouldEqual, Channel_TYPE_COLLABORATION)
			})

			Convey("it should have privacy constant", func() {
				So(c.PrivacyConstant, ShouldEqual, Channel_PRIVACY_PRIVATE)
			})

			Convey("it should have purpose", func() {
				So(c.Purpose, ShouldBeBlank)
			})
		})
	})
}
示例#16
0
func TestChannelParticipantisExempt(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		Convey("While testing channel participant is exempt or not", t, func() {
			Convey("it should have error while getting account id from db when channel id is not set", func() {
				cp := NewChannelParticipant()

				ex, err := cp.isExempt()
				So(err, ShouldNotBeNil)
				So(err.Error(), ShouldContainSubstring, "couldnt find accountId from content")
				So(ex, ShouldEqual, false)
			})

			Convey("it should return true if participant is not troll", func() {
				// create account
				acc := CreateAccountWithTest()

				cp := NewChannelParticipant()
				cp.AccountId = acc.Id

				ex, err := cp.isExempt()
				So(err, ShouldBeNil)
				So(ex, ShouldEqual, false)
			})
		})
	})
}
示例#17
0
func TestPresenceGetGroupPaymentStatusFromCache(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		Convey("With non existing group", t, func() {
			groupName := models.RandomGroupName()
			Convey("should get err", func() {

				status, err := getGroupPaymentStatusFromCache(groupName)
				So(err, ShouldEqual, mgo.ErrNotFound)
				So(status, ShouldBeEmpty)

				Convey("With existing group", func() {
					_, _, groupSlug := models.CreateRandomGroupDataWithChecks()

					// make sure it is no in the cache
					_, err = groupCache.Get(groupSlug)
					So(err, ShouldEqual, cache.ErrNotFound)

					Convey("should work properly with invalid payment status", func() {
						status, err := getGroupPaymentStatusFromCache(groupSlug)
						So(err, ShouldBeNil)
						So(status, ShouldEqual, "invalid")
					})
				})
			})
		})
	})
}
示例#18
0
func TestChannelMessageListFetchMessageChannelIds(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		Convey("while fetching message channel ids", t, func() {
			Convey("it should have message id", func() {
				cml := NewChannelMessageList()
				fm, err := cml.FetchMessageChannelIds(0)
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, ErrMessageIdIsNotSet)
				So(fm, ShouldEqual, nil)
			})

			Convey("if message doesnt belong to any channel", func() {
				m := CreateMessageWithTest()
				So(m.Create(), ShouldBeNil)

				cml := NewChannelMessageList()
				cml.MessageId = m.Id

				fm, err := cml.FetchMessageChannelIds(m.Id)
				So(err, ShouldBeNil)
				So(fm, ShouldBeNil)
			})
		})
	})
}
示例#19
0
func TestChannelNewChannel(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {

		c := NewChannel()

		Convey("given a NewChannel", t, func() {

			Convey("it should have Name as set", func() {
				So(c.Name, ShouldNotBeBlank)
			})

			Convey("it should have GroupName as set", func() {
				So(c.GroupName, ShouldNotBeBlank)
			})

			Convey("it should have TypeConstant as set", func() {
				So(c.TypeConstant, ShouldEqual, Channel_TYPE_DEFAULT)
			})

			Convey("it should have PrivacyConstant as set", func() {
				So(c.PrivacyConstant, ShouldEqual, Channel_PRIVACY_PRIVATE)
			})
		})
	})
}
示例#20
0
func TestChannelgetAccountId(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {

		Convey("while getting account id", t, func() {

			Convey("it should have channel id", func() {
				c := NewChannel()
				_, err := c.getAccountId()
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, ErrChannelIdIsNotSet)
			})

			Convey("it should have creator id", func() {
				c := NewChannel()
				c.CreatorId = 123
				ac, err := c.getAccountId()
				So(err, ShouldBeNil)
				So(c.CreatorId, ShouldEqual, ac)
			})

			Convey("it should get id of creator", func() {
				acc := CreateAccountWithTest()
				c := CreateChannelWithTest(acc.Id)
				ac, err := c.getAccountId()
				So(err, ShouldBeNil)
				So(c.CreatorId, ShouldEqual, ac)
			})
		})
	})
}
示例#21
0
func TestChannelMessageAddReply(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {

		Convey("while adding reply message", t, func() {

			Convey("channel message should have an id", func() {
				chm := NewChannelMessage()
				cm, err := chm.AddReply(chm)
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, ErrChannelMessageIdIsNotSet)
				So(cm, ShouldBeNil)
			})

			Convey("channel message id should equal to reply message id ", func() {
				c := CreateMessageWithTest()
				So(c.Create(), ShouldBeNil)
				c2 := CreateMessageWithTest()
				So(c2.Create(), ShouldBeNil)
				cm, err := c.AddReply(c2)
				So(err, ShouldBeNil)
				So(cm.MessageId, ShouldEqual, c.Id)
			})
		})
	})
}
示例#22
0
func TestChannelMessageFetchMessageCount(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		Convey("while fetching message count since the given time", t, func() {
			channel, accounts := CreateChannelWithParticipants()
			cm1 := CreateMessage(channel.Id, accounts[0].Id, ChannelMessage_TYPE_PRIVATE_MESSAGE)
			CreateMessage(channel.Id, accounts[1].Id, ChannelMessage_TYPE_PRIVATE_MESSAGE)
			CreateMessage(channel.Id, accounts[0].Id, ChannelMessage_TYPE_PRIVATE_MESSAGE)
			CreateMessage(channel.Id, accounts[0].Id, ChannelMessage_TYPE_PRIVATE_MESSAGE)

			query := request.NewQuery()
			query.Type = ChannelMessage_TYPE_PRIVATE_MESSAGE
			query.GroupChannelId = channel.Id
			query.From = cm1.CreatedAt

			Convey("first user should see 4 messages when their account is not excluded", func() {
				c := NewChannelMessage()
				count, err := c.FetchTotalMessageCount(query)
				So(err, ShouldBeNil)
				So(count, ShouldEqual, 4)
			})

			Convey("second user should see 3 messages then their account is excluded", func() {
				c := NewChannelMessage()
				query.ExcludeField("AccountId", accounts[1].Id)
				count, err := c.FetchTotalMessageCount(query)
				So(err, ShouldBeNil)
				So(count, ShouldEqual, 3)
			})
		})
	})
}
示例#23
0
func TestChannelMessageFetchLatestMessages(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		Convey("while fetching latest messages of a channel with three participants and four messages", t, func() {
			channel, accounts := CreateChannelWithParticipants()
			cm1 := CreateMessage(channel.Id, accounts[0].Id, ChannelMessage_TYPE_PRIVATE_MESSAGE)
			cm2 := CreateMessage(channel.Id, accounts[1].Id, ChannelMessage_TYPE_PRIVATE_MESSAGE)
			cm3 := CreateMessage(channel.Id, accounts[0].Id, ChannelMessage_TYPE_PRIVATE_MESSAGE)
			cm4 := CreateMessage(channel.Id, accounts[0].Id, ChannelMessage_TYPE_PRIVATE_MESSAGE)

			query := request.NewQuery()
			query.Limit = 3
			query.AddSortField("CreatedAt", request.ORDER_DESC)
			query.Type = ChannelMessage_TYPE_PRIVATE_MESSAGE

			Convey("first user should see last three messages when no parameters are set", func() {
				c := NewChannelMessage()
				cms, err := c.FetchMessagesByChannelId(channel.Id, query)
				So(err, ShouldBeNil)
				So(len(cms), ShouldEqual, 3)
				So(cms[2].Id, ShouldEqual, cm2.Id)
			})

			Convey("first user should see one message when their account id is excluded", func() {
				query.ExcludeField("AccountId", cm1.AccountId)
				c := NewChannelMessage()
				cms, err := c.FetchMessagesByChannelId(channel.Id, query)
				So(err, ShouldBeNil)
				So(len(cms), ShouldEqual, 1)
				So(cms[0].Id, ShouldEqual, cm2.Id)
			})

			Convey("second user should see two messages starting with their own message", func() {
				c := NewChannelMessage()
				query.ExcludeField("AccountId", cm2.AccountId)
				query.From = cm2.CreatedAt

				cms, err := c.FetchMessagesByChannelId(channel.Id, query)
				So(err, ShouldBeNil)
				So(len(cms), ShouldEqual, 2)
				So(cms[0].Id, ShouldEqual, cm4.Id)
				So(cms[1].Id, ShouldEqual, cm3.Id)
			})

			Convey("third user should see three messages when all parameters are set", func() {
				c := NewChannelMessage()
				query.ExcludeField("AccountId", accounts[2].Id)
				query.From = cm1.CreatedAt

				cms, err := c.FetchMessagesByChannelId(channel.Id, query)
				So(err, ShouldBeNil)
				So(len(cms), ShouldEqual, 3)
				So(cms[0].Id, ShouldEqual, cm4.Id)
				So(cms[1].Id, ShouldEqual, cm3.Id)
				So(cms[2].Id, ShouldEqual, cm2.Id)
			})
		})
	})
}
示例#24
0
func TestAccountCreation(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		Convey("while  creating account", t, func() {
			Convey("First Create User", func() {
				Convey("Should not error if you pass old id", func() {
					account := models.NewAccount()
					account.OldId = AccountOldId.Hex()
					account, err := rest.CreateAccount(account)
					So(err, ShouldBeNil)
					So(account, ShouldNotBeNil)
					Convey("User should be in postgre when fetch from postgre", func() {
						accounts, err := models.FetchAccountsWithBongoOffset(100, 0)
						So(err, ShouldBeNil)
						So(accounts, ShouldNotBeNil)
						Convey("Created account should be in account list", func() {
							var doesExist bool
							for _, ac := range accounts {
								if ac.OldId == account.OldId {
									doesExist = true
								}
							}
							So(doesExist, ShouldBeTrue)
						})
					})
					Convey("User should not be in postgre if doesn't exist in mongo", func() {
						err := account.DeleteIfNotInMongo()
						So(err, ShouldBeNil)
						Convey("Created account should not be in account list after checking mongodb", func() {
							accounts, err := models.FetchAccountsWithBongoOffset(100, 0)
							So(err, ShouldBeNil)

							var doesExist bool
							for _, ac := range accounts {
								if ac.OldId == account.OldId {
									doesExist = true
								}
							}
							So(doesExist, ShouldBeFalse)
						})
					})
					Convey("Users creation should be succussfully", func() {
						err := accountCreatorWithCount(10)
						So(err, ShouldBeNil)
						Convey("Users should be in postgre with many accounts", func() {
							accounts, err := models.FetchAccountsWithBongoOffset(100, 0)
							So(err, ShouldBeNil)
							So(len(accounts), ShouldBeGreaterThan, 9)
						})
						Convey("Users should not be in postgre after deleting accounts", func() {
							err := models.DeleteDiffedDBAccounts()
							So(err, ShouldBeNil)
						})
					})
				})
			})
		})
	})
}
示例#25
0
func TestChannelRemoveMessage(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {

		Convey("while removing a message from the channel", t, func() {

			Convey("it should have channel id", func() {
				c := NewChannel()
				_, err := c.RemoveMessage(123)
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, ErrChannelIdIsNotSet)
			})

			Convey("it should have message id", func() {
				c := NewChannel()
				c.Id = 123
				_, err := c.RemoveMessage(0)
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, ErrMessageIdIsNotSet)
			})

			Convey("removing message from the channel should ne successful", func() {
				acc := CreateAccountWithTest()

				c := CreateChannelWithTest(acc.Id)
				cm := CreateMessageWithTest()
				So(cm.Create(), ShouldBeNil)

				_, err := c.AddMessage(cm)
				So(err, ShouldBeNil)

				ch, err := c.RemoveMessage(cm.Id)
				So(err, ShouldBeNil)
				So(ch, ShouldNotBeEmpty)
			})

			Convey("it should return error if message is already removed from the channel", func() {
				acc := CreateAccountWithTest()

				c := CreateChannelWithTest(acc.Id)
				cm := CreateMessageWithTest()
				So(cm.Create(), ShouldBeNil)

				_, err := c.AddMessage(cm)
				So(err, ShouldBeNil)

				ch, err := c.RemoveMessage(cm.Id)
				So(err, ShouldBeNil)
				So(ch, ShouldNotBeEmpty)

				// try to remove the same message again
				ch, err = c.RemoveMessage(cm.Id)
				So(err, ShouldNotBeNil)
				So(ch, ShouldBeNil)
			})
		})
	})
}
示例#26
0
func TestChannelParticipantCheckAccountStatus(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		Convey("While testing account status", t, func() {
			Convey("it should have channel id", func() {
				cp := NewChannelParticipant()
				ip, err := cp.IsParticipant(1123)
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, ErrChannelIdIsNotSet)
				So(ip, ShouldEqual, false)
			})

			Convey("it should return false if account does not exist", func() {
				cp := NewChannelParticipant()
				cp.ChannelId = 120

				ip, err := cp.IsParticipant(112345)
				So(err, ShouldBeNil)
				So(ip, ShouldEqual, false)
			})

			Convey("it should not have error if account is exist", func() {
				acc := CreateAccountWithTest()
				c := CreateChannelWithTest(acc.Id)

				cp := NewChannelParticipant()
				cp.ChannelId = c.Id
				cp.AccountId = acc.Id
				err := cp.Create()
				So(err, ShouldBeNil)

				ip, err := c.IsParticipant(acc.Id)
				So(err, ShouldBeNil)
				So(ip, ShouldEqual, true)
				So(cp.StatusConstant, ShouldEqual, ChannelParticipant_STATUS_ACTIVE)
			})

			Convey("it should return true for pending participant", func() {
				acc := CreateAccountWithTest()
				c := CreateChannelWithTest(acc.Id)

				cp := NewChannelParticipant()
				cp.ChannelId = c.Id
				cp.AccountId = acc.Id
				cp.StatusConstant = ChannelParticipant_STATUS_REQUEST_PENDING
				So(cp.Create(), ShouldBeNil)

				ip, err := cp.IsInvited(acc.Id)
				So(err, ShouldBeNil)
				So(ip, ShouldEqual, true)
				So(cp.StatusConstant, ShouldEqual, ChannelParticipant_STATUS_REQUEST_PENDING)
			})
		})
	})
}
示例#27
0
func TestPresenceDailyOperations(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		groupName1 := models.RandomGroupName()
		Convey("With given presence data", t, func() {
			p1 := &models.PresenceDaily{
				AccountId: 1,
				GroupName: groupName1,
				// just to give some time between two records
				CreatedAt: time.Now().UTC().Add(-time.Millisecond * 100),
			}
			So(p1.Create(), ShouldBeNil)

			p2 := &models.PresenceDaily{
				AccountId: 1,
				GroupName: groupName1,
				CreatedAt: time.Now().UTC(),
			}
			So(p2.Create(), ShouldBeNil)

			Convey("getPresenceInfoFromDB should work properly", func() {
				pi, err := getPresenceInfoFromDB(&Ping{
					AccountID:     p2.AccountId,
					GroupName:     p2.GroupName,
					paymentStatus: string(mongomodels.SubStatusActive),
				})
				So(err, ShouldBeNil)
				So(pi, ShouldNotBeNil)
				// Why .Unix()? postgres omits after 6 decimal - due to our config.
				// Expected: '2016-09-21 13:44:50.695855774 +0000 UTC'
				// Actual:   '2016-09-21 13:44:50.695856 +0000 UTC'
				So(pi.CreatedAt.UTC().Unix(), ShouldEqual, p2.CreatedAt.UTC().Unix())

				pi2, err := getPresenceInfoFromDB(&Ping{
					AccountID:     p2.AccountId,
					GroupName:     "non_existent_group_name",
					paymentStatus: string(mongomodels.SubStatusActive),
				})
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, bongo.RecordNotFound)
				So(pi2, ShouldBeNil)

				pi3, err := getPresenceInfoFromDB(&Ping{
					AccountID:     rand.Int63(),
					GroupName:     groupName1,
					paymentStatus: string(mongomodels.SubStatusActive),
				})
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, bongo.RecordNotFound)
				So(pi3, ShouldBeNil)
			})
		})
	})
}
示例#28
0
func TestChannelMessageContainerGetId(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		Convey("while getting id", t, func() {
			Convey("it should be zero value if channel message is not exist", func() {
				cmc := NewChannelMessageContainer()

				gi := cmc.GetId()
				So(gi, ShouldEqual, 0)
			})
		})
	})
}
示例#29
0
func TestChannelMessageListMarkIfExempt(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		Convey("while marking if channel is exempt", t, func() {
			Convey("it should have error if message id is not set", func() {
				cml := NewChannelMessageList()

				err := cml.MarkIfExempt()
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, ErrMessageIdIsNotSet)
			})
		})
	})
}
示例#30
0
func TestChannelFetchMessageList(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {

		Convey("while fetching channel message list", t, func() {
			Convey("it should have channel id", func() {
				c := NewChannel()
				_, err := c.FetchMessageList(123)
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, ErrChannelIdIsNotSet)
			})

			Convey("it should have message id", func() {
				c := NewChannel()
				c.Id = 123
				_, err := c.FetchMessageList(0)
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, ErrMessageIdIsNotSet)
			})

			Convey("non-existing message list should give error", func() {
				acc := CreateAccountWithTest()
				c := CreateChannelWithTest(acc.Id)

				// 1 is an arbitrary number
				_, err := c.FetchMessageList(1)
				So(err, ShouldNotBeNil)
				So(err, ShouldEqual, bongo.RecordNotFound)
			})

			Convey("existing message list should not give error", func() {
				acc := CreateAccountWithTest()
				c := CreateChannelWithTest(acc.Id)

				// create message
				cm := CreateMessageWithTest()
				So(cm.Create(), ShouldBeNil)

				// add message to the channel
				cml, err := c.AddMessage(cm)
				So(err, ShouldBeNil)
				So(cml, ShouldNotBeNil)

				// try to fetch persisted message
				cml2, err := c.FetchMessageList(cm.Id)
				So(err, ShouldBeNil)
				So(cml2, ShouldNotBeNil)
				So(cml2.MessageId, ShouldEqual, cm.Id)
			})
		})
	})
}