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") }) }) }) }) }) }
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) }) }) }) }
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) }) }) }) }
func withStubData(endpoint string, f func(username string, groupName string, sessionID string)) { acc, _, groupName := models.CreateRandomGroupDataWithChecks() group, err := modelhelper.GetGroup(groupName) tests.ResultedWithNoErrorCheck(group, err) err = modelhelper.MakeAdmin(bson.ObjectIdHex(acc.OldId), group.Id) So(err, ShouldBeNil) ses, err := modelhelper.FetchOrCreateSession(acc.Nick, groupName) tests.ResultedWithNoErrorCheck(ses, err) f(acc.Nick, groupName, ses.ClientId) }
func TestCreditCardDeleteNonAdmin(t *testing.T) { Convey("When a non admin user request comes", t, func() { withTestServer(t, func(endpoint string) { acc, _, groupName := models.CreateRandomGroupDataWithChecks() ses, err := modelhelper.CreateSessionForAccount(acc.Nick, groupName) tests.ResultedWithNoErrorCheck(ses, err) Convey("Endpoint should return error", func() { ccdeleteURL := endpoint + EndpointCreditCardDelete _, err := rest.DoRequestWithAuth("DELETE", ccdeleteURL, nil, ses.ClientId) So(err, ShouldNotBeNil) }) }) }) }
func withStubData(f func(username string, groupName string, sessionID string)) { acc, _, groupName := models.CreateRandomGroupDataWithChecks() group, err := modelhelper.GetGroup(groupName) tests.ResultedWithNoErrorCheck(group, err) err = modelhelper.MakeAdmin(bson.ObjectIdHex(acc.OldId), group.Id) So(err, ShouldBeNil) ses, err := modelhelper.FetchOrCreateSession(acc.Nick, groupName) tests.ResultedWithNoErrorCheck(ses, err) cus, err := EnsureCustomerForGroup(acc.Nick, groupName, &stripe.CustomerParams{}) tests.ResultedWithNoErrorCheck(cus, err) f(acc.Nick, groupName, ses.ClientId) err = DeleteCustomerForGroup(groupName) So(err, ShouldBeNil) }
func TestCheckOwnership(t *testing.T) { tests.WithRunner(t, func(r *runner.Runner) { Convey("accounts can own things", t, func() { account, groupChannel, groupName := models.CreateRandomGroupDataWithChecks() ses, err := modelhelper.FetchOrCreateSession(account.Nick, groupName) tests.ResultedWithNoErrorCheck(ses, err) tedsAccount, err := models.CreateAccountInBothDbsWithNick("ted") tests.ResultedWithNoErrorCheck(tedsAccount, err) bobsPost, err := rest.CreatePost(groupChannel.Id, ses.ClientId) tests.ResultedWithNoErrorCheck(bobsPost, err) Convey("it should say when an account owns a post", func() { isOwner, err := rest.CheckPostOwnership(account, bobsPost) So(err, ShouldBeNil) So(isOwner, ShouldBeTrue) }) Convey("it should say when an account doesn't own a post", func() { isOwner, err := rest.CheckPostOwnership(tedsAccount, bobsPost) So(err, ShouldBeNil) So(isOwner, ShouldBeFalse) }) bobsChannel, err := rest.CreateChannelByGroupNameAndType(account.Id, groupName, models.Channel_TYPE_TOPIC, ses.ClientId) So(err, ShouldBeNil) Convey("it should say when an account owns a channel", func() { isOwner, err := rest.CheckChannelOwnership(account, bobsChannel) So(err, ShouldBeNil) So(isOwner, ShouldBeTrue) }) Convey("it should say when an account doesn't own a channel", func() { isOwner, err := rest.CheckChannelOwnership(tedsAccount, bobsChannel) So(err, ShouldBeNil) So(isOwner, ShouldBeFalse) }) }) }) }
func TestAccountGroupChannels(t *testing.T) { tests.WithRunner(t, func(r *runner.Runner) { Convey("While fetching account activity count in profile page", t, func() { account, _, groupName := models.CreateRandomGroupDataWithChecks() ses, err := modelhelper.FetchOrCreateSession(account.Nick, groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) channel, err := rest.CreateChannelByGroupNameAndType(account.Id, groupName, models.Channel_TYPE_TOPIC, ses.ClientId) So(err, ShouldBeNil) So(channel, ShouldNotBeNil) cc, err := rest.FetchAccountChannels(ses.ClientId) So(err, ShouldBeNil) ccs := []models.ChannelContainer(*cc) So(len(ccs), ShouldEqual, 2) }) }) }
func TestStoreGetDeleteCredentials(t *testing.T) { tests.WithRunner(t, func(r *runner.Runner) { Convey("While storing credentials", t, func() { Convey("after create account requirements", func() { ownerAccount, _, groupName := models.CreateRandomGroupDataWithChecks() ownerSes, err := modelhelper.FetchOrCreateSession(ownerAccount.Nick, groupName) So(err, ShouldBeNil) So(ownerSes, ShouldNotBeNil) pathName := "testcredential" Convey("We should be able to store credentials", func() { keyValue := make(credential.KeyValue, 0) keyValue["test-key"] = "test-value" err := rest.StoreCredentialWithAuth(pathName, keyValue, ownerSes.ClientId) So(err, ShouldBeNil) }) Convey("We should be able to get credentials after storing", func() { res, err := rest.GetCredentialWithAuth(pathName, ownerSes.ClientId) So(err, ShouldBeNil) So(res, ShouldNotBeNil) So(res["test-key"], ShouldEqual, "test-value") }) Convey("We should be able to delete credentials after storing", func() { err := rest.DeleteCredentialWithAuth(pathName, ownerSes.ClientId) So(err, ShouldBeNil) }) Convey("We should not be able to get credentials after deletion process", func() { res, err := rest.GetCredentialWithAuth(pathName, ownerSes.ClientId) So(err.Error(), ShouldContainSubstring, "The specified key does not exist") So(res, ShouldBeNil) }) }) }) }) }
func withStubData(endpoint string, f func(username string, groupName string, sessionID string)) { createURL := endpoint + EndpointCustomerCreate acc, _, groupName := models.CreateRandomGroupDataWithChecks() group, err := modelhelper.GetGroup(groupName) tests.ResultedWithNoErrorCheck(group, err) err = modelhelper.MakeAdmin(bson.ObjectIdHex(acc.OldId), group.Id) So(err, ShouldBeNil) ses, err := modelhelper.FetchOrCreateSession(acc.Nick, groupName) tests.ResultedWithNoErrorCheck(ses, err) req, err := json.Marshal(&stripe.CustomerParams{}) tests.ResultedWithNoErrorCheck(req, err) res, err := rest.DoRequestWithAuth("POST", createURL, req, ses.ClientId) tests.ResultedWithNoErrorCheck(res, err) f(acc.Nick, groupName, ses.ClientId) So(payment.DeleteCustomerForGroup(groupName), ShouldBeNil) }
func TestAccountProfilePostCount(t *testing.T) { tests.WithRunner(t, func(r *runner.Runner) { Convey("While fetching account activity count 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 fetch all post count when they are not troll", func() { cr, err := rest.FetchAccountActivityCount(account.Id, ses.ClientId) So(err, ShouldBeNil) So(cr, ShouldNotBeNil) So(cr.TotalCount, ShouldEqual, 1) post, err := rest.CreatePost(channel.Id, ses.ClientId) So(err, ShouldBeNil) So(post, ShouldNotBeNil) cr, err = rest.FetchAccountActivityCount(account.Id, ses.ClientId) So(err, ShouldBeNil) So(cr, ShouldNotBeNil) So(cr.TotalCount, ShouldEqual, 2) }) }) }) }
func TestChannelParticipantOperations(t *testing.T) { tests.WithRunner(t, func(r *runner.Runner) { Convey("while testing channel participants", t, func() { Convey("First Create Users and initiate conversation", func() { ownerAccount, groupChannel, groupName := models.CreateRandomGroupDataWithChecks() ownerSes, err := modelhelper.FetchOrCreateSession(ownerAccount.Nick, groupName) So(err, ShouldBeNil) So(ownerSes, ShouldNotBeNil) secondAccount, err := models.CreateAccountInBothDbs() tests.ResultedWithNoErrorCheck(secondAccount, err) _, err = groupChannel.AddParticipant(secondAccount.Id) So(err, ShouldBeNil) thirdAccount, err := models.CreateAccountInBothDbs() tests.ResultedWithNoErrorCheck(thirdAccount, err) _, err = groupChannel.AddParticipant(thirdAccount.Id) So(err, ShouldBeNil) forthAccount, err := models.CreateAccountInBothDbs() tests.ResultedWithNoErrorCheck(forthAccount, err) _, err = groupChannel.AddParticipant(forthAccount.Id) So(err, ShouldBeNil) devrim, err := models.CreateAccountInBothDbsWithNick("devrim") tests.ResultedWithNoErrorCheck(devrim, err) _, err = groupChannel.AddParticipant(devrim.Id) So(err, ShouldBeNil) ses, err := modelhelper.FetchOrCreateSession(ownerAccount.Nick, groupName) tests.ResultedWithNoErrorCheck(ses, err) secondSes, err := modelhelper.FetchOrCreateSession(secondAccount.Nick, groupName) tests.ResultedWithNoErrorCheck(secondSes, err) pmr := models.ChannelRequest{} pmr.AccountId = ownerAccount.Id pmr.Body = "new conversation" pmr.GroupName = groupName pmr.Recipients = []string{"devrim"} channelContainer, err := rest.SendPrivateChannelRequest(pmr, ownerSes.ClientId) So(err, ShouldBeNil) So(channelContainer, ShouldNotBeNil) Convey("First user should be able to add second and third users to conversation", func() { _, err = rest.AddChannelParticipant(channelContainer.Channel.Id, ownerSes.ClientId, secondAccount.Id, thirdAccount.Id) So(err, ShouldBeNil) participants, err := rest.ListChannelParticipants(channelContainer.Channel.Id, ownerSes.ClientId) So(err, ShouldBeNil) So(participants, ShouldNotBeNil) // it is four because first user is "devrim" here So(len(participants), ShouldEqual, 4) Convey("First user should not be able to re-add second participant", func() { _, err = rest.AddChannelParticipant(channelContainer.Channel.Id, ownerSes.ClientId, secondAccount.Id) So(err, ShouldBeNil) participants, err := rest.ListChannelParticipants(channelContainer.Channel.Id, ownerSes.ClientId) So(err, ShouldBeNil) So(participants, ShouldNotBeNil) So(len(participants), ShouldEqual, 4) }) Convey("Second user should be able to leave conversation", func() { // token of account -> secondAccount _, err = rest.DeleteChannelParticipant(channelContainer.Channel.Id, secondSes.ClientId, secondAccount.Id) So(err, ShouldBeNil) participants, err := rest.ListChannelParticipants(channelContainer.Channel.Id, ownerSes.ClientId) So(err, ShouldBeNil) So(participants, ShouldNotBeNil) So(len(participants), ShouldEqual, 3) Convey("A user who is not participant of a conversation should not be able to add another user to the conversation", func() { _, err = rest.AddChannelParticipant(channelContainer.Channel.Id, secondSes.ClientId, forthAccount.Id) So(err, ShouldNotBeNil) participants, err := rest.ListChannelParticipants(channelContainer.Channel.Id, ownerSes.ClientId) So(err, ShouldBeNil) So(participants, ShouldNotBeNil) So(len(participants), ShouldEqual, 3) }) }) Convey("Channel owner should be able to kick another conversation participant", func() { _, err = rest.DeleteChannelParticipant(channelContainer.Channel.Id, ownerSes.ClientId, secondAccount.Id) So(err, ShouldBeNil) participants, err := rest.ListChannelParticipants(channelContainer.Channel.Id, ownerSes.ClientId) So(err, ShouldBeNil) So(participants, ShouldNotBeNil) So(len(participants), ShouldEqual, 3) }) Convey("when a user is blocked", func() { _, err = rest.BlockChannelParticipant(channelContainer.Channel.Id, ownerSes.ClientId, secondAccount.Id) So(err, ShouldBeNil) Convey("it should not be in channel participant list", func() { participants, err := rest.ListChannelParticipants(channelContainer.Channel.Id, ownerSes.ClientId) So(err, ShouldBeNil) So(participants, ShouldNotBeNil) So(len(participants), ShouldEqual, 3) }) Convey("should not be able to add it back", func() { _, err = rest.AddChannelParticipant(channelContainer.Channel.Id, ownerSes.ClientId, secondAccount.Id) So(err, ShouldNotBeNil) }) Convey("should be able to unblock", func() { _, err = rest.UnblockChannelParticipant(channelContainer.Channel.Id, ownerSes.ClientId, secondAccount.Id) So(err, ShouldBeNil) Convey("it should not be in channel participant list still", func() { participants, err := rest.ListChannelParticipants(channelContainer.Channel.Id, ownerSes.ClientId) So(err, ShouldBeNil) So(participants, ShouldNotBeNil) So(len(participants), ShouldEqual, 3) }) Convey("when we add the same user as participant", func() { _, err = rest.AddChannelParticipant(channelContainer.Channel.Id, ownerSes.ClientId, secondAccount.Id, thirdAccount.Id) So(err, ShouldBeNil) Convey("it should be in channel participant list", func() { participants, err := rest.ListChannelParticipants(channelContainer.Channel.Id, ownerSes.ClientId) So(err, ShouldBeNil) So(participants, ShouldNotBeNil) So(len(participants), ShouldEqual, 4) }) }) }) }) Convey("Second user should not be able to kick another conversation participant", func() { _, err = rest.DeleteChannelParticipant(channelContainer.Channel.Id, secondSes.ClientId, thirdAccount.Id) So(err, ShouldNotBeNil) }) }) Convey("First user should be able to invite second user", func() { _, err = rest.InviteChannelParticipant(channelContainer.Channel.Id, ownerSes.ClientId, secondAccount.Id) So(err, ShouldBeNil) participants, err := rest.ListChannelParticipants(channelContainer.Channel.Id, ownerSes.ClientId) So(err, ShouldBeNil) So(participants, ShouldNotBeNil) // it is four because first user is "devrim" here So(len(participants), ShouldEqual, 2) Convey("Second user should be able to reject invitation", func() { ses, err := modelhelper.FetchOrCreateSession(secondAccount.Nick, groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) err = rest.RejectInvitation(channelContainer.Channel.Id, ses.ClientId) So(err, ShouldBeNil) participants, err := rest.ListChannelParticipants(channelContainer.Channel.Id, ownerSes.ClientId) So(err, ShouldBeNil) So(participants, ShouldNotBeNil) So(len(participants), ShouldEqual, 2) }) Convey("Second user should be able to accept invitation", func() { ses, err := modelhelper.FetchOrCreateSession(secondAccount.Nick, groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) err = rest.AcceptInvitation(channelContainer.Channel.Id, ses.ClientId) So(err, ShouldBeNil) participants, err := rest.ListChannelParticipants(channelContainer.Channel.Id, ownerSes.ClientId) So(err, ShouldBeNil) So(participants, ShouldNotBeNil) So(len(participants), ShouldEqual, 3) }) }) // TODO Until we find a better way for handling async stuff, this test is skipped. Instead of sleep, we should use some // timeouts for testing these kind of stuff. SkipConvey("All private messages must be deleted when all participant users leave the channel", func() { account := models.NewAccount() err = account.ByNick("devrim") So(err, ShouldBeNil) _, err = rest.DeleteChannelParticipant(channelContainer.Channel.Id, ses.ClientId, account.Id) So(err, ShouldBeNil) _, err = rest.DeleteChannelParticipant(channelContainer.Channel.Id, ownerSes.ClientId, ownerAccount.Id) So(err, ShouldBeNil) time.Sleep(1 * time.Second) testChannel := models.NewChannel() err := testChannel.ById(channelContainer.Channel.Id) So(err, ShouldEqual, bongo.RecordNotFound) testChannelList := models.NewChannelMessageList() err = bongo.B.Unscoped().Where("channel_id = ?", channelContainer.Channel.Id).Find(testChannelList).Error So(err, ShouldEqual, bongo.RecordNotFound) testMessage := models.NewChannelMessage() err = bongo.B.Unscoped().Where("initial_channel_id = ?", channelContainer.Channel.Id).Find(testMessage).Error So(err, ShouldEqual, bongo.RecordNotFound) }) Convey("Users should not be able to add/remove users to/from bot channels", func() { ownerAccount, _, groupName := models.CreateRandomGroupDataWithChecks() participant := models.NewAccount() participant.OldId = AccountOldId.Hex() participant, err = rest.CreateAccount(participant) So(err, ShouldBeNil) So(participant, ShouldNotBeNil) ses, err := modelhelper.FetchOrCreateSession(ownerAccount.Nick, groupName) So(err, ShouldBeNil) ch, err := rest.CreateChannelByGroupNameAndType(ownerAccount.Id, groupName, models.Channel_TYPE_BOT, ses.ClientId) So(err, ShouldBeNil) So(ch, ShouldNotBeNil) // account is -> ownerAccount.Id _, err = rest.AddChannelParticipant(ch.Id, ses.ClientId, participant.Id) So(strings.Contains(err.Error(), "can not add participants for bot channel"), ShouldBeTrue) }) Convey("Users should be able to add/remove users to/from topic channels", func() { ownerAccount, _, groupName := models.CreateRandomGroupDataWithChecks() participant := models.NewAccount() participant.OldId = AccountOldId.Hex() participant, err = rest.CreateAccount(participant) So(err, ShouldBeNil) So(participant, ShouldNotBeNil) ses, err := modelhelper.FetchOrCreateSession(ownerAccount.Nick, groupName) So(err, ShouldBeNil) ch, err := rest.CreateChannelByGroupNameAndType(ownerAccount.Id, groupName, models.Channel_TYPE_TOPIC, ses.ClientId) So(err, ShouldBeNil) So(ch, ShouldNotBeNil) // account is -> ownerAccount.Id _, err = rest.AddChannelParticipant(ch.Id, ses.ClientId, participant.Id) So(err, ShouldBeNil) Convey("adding same user again should success", func() { _, err = rest.AddChannelParticipant(ch.Id, ses.ClientId, participant.Id) So(err, ShouldBeNil) }) _, err = rest.DeleteChannelParticipant(ch.Id, ses.ClientId, participant.Id) So(err, ShouldBeNil) Convey("removing same user again should success", func() { _, err = rest.DeleteChannelParticipant(ch.Id, ses.ClientId, participant.Id) So(err, ShouldBeNil) }) }) Convey("while removing users from group channels", func() { ownerAccount, _, groupName := models.CreateRandomGroupDataWithChecks() participant := models.NewAccount() participant.OldId = AccountOldId.Hex() participant, err = rest.CreateAccount(participant) So(err, ShouldBeNil) So(participant, ShouldNotBeNil) participant2 := models.NewAccount() participant2.OldId = AccountOldId.Hex() participant2, err = rest.CreateAccount(participant2) So(err, ShouldBeNil) So(participant2, ShouldNotBeNil) ownerSes, err := modelhelper.FetchOrCreateSession(ownerAccount.Nick, groupName) So(err, ShouldBeNil) ses, err := modelhelper.FetchOrCreateSession(participant.Nick, groupName) So(err, ShouldBeNil) ch, err := rest.CreateChannelByGroupNameAndType(ownerAccount.Id, groupName, models.Channel_TYPE_GROUP, ownerSes.ClientId) So(err, ShouldBeNil) So(ch, ShouldNotBeNil) // ownerSes session is admin's session data _, err = rest.AddChannelParticipant(ch.Id, ownerSes.ClientId, participant.Id) So(err, ShouldBeNil) _, err = rest.AddChannelParticipant(ch.Id, ownerSes.ClientId, participant2.Id) So(err, ShouldBeNil) Convey("owner should be able to remove user from group channel", func() { // ownerSes session is admin's session data _, err = rest.DeleteChannelParticipant(ch.Id, ownerSes.ClientId, participant2.Id) So(err, ShouldBeNil) }) Convey("nonOwner should not be able to remove user from group channel", func() { // ses session is participant's session data _, err = rest.DeleteChannelParticipant(ch.Id, ses.ClientId, participant2.Id) So(err, ShouldNotBeNil) }) }) }) }) }) }
func TestChannelByParticipants(t *testing.T) { tests.WithRunner(t, func(r *runner.Runner) { Convey("while fetching channels by their participants", t, func() { admin1, _, groupName1 := models.CreateRandomGroupDataWithChecks() ses1, err := modelhelper.FetchOrCreateSession(admin1.Nick, groupName1) So(err, ShouldBeNil) So(ses1, ShouldNotBeNil) acc1, err := models.CreateAccountInBothDbs() So(err, ShouldBeNil) So(acc1, ShouldNotBeNil) acc2, err := models.CreateAccountInBothDbs() So(err, ShouldBeNil) So(acc1, ShouldNotBeNil) tc1 := createChannelAndParticipants(admin1, groupName1, models.Channel_TYPE_TOPIC, ses1.ClientId, acc1.Id, acc2.Id) tc2 := createChannelAndParticipants(admin1, groupName1, models.Channel_TYPE_TOPIC, ses1.ClientId, acc1.Id, acc2.Id) Convey("valid request should return valid response", func() { channels, err := rest.FetchChannelsByParticipants([]int64{acc1.Id, acc2.Id}, models.Channel_TYPE_TOPIC, ses1.ClientId) // there should be an err So(err, ShouldBeNil) So(channels, ShouldNotBeNil) So(len(channels), ShouldEqual, 2) So(tc1.Id, ShouldEqual, channels[0].Channel.Id) So(tc2.Id, ShouldEqual, channels[1].Channel.Id) }) Convey("other group's content should not be in the result set", func() { groupName2 := models.RandomGroupName() models.CreateTypedGroupedChannelWithTest( admin1.Id, models.Channel_TYPE_GROUP, groupName2, ) ses2, err := modelhelper.FetchOrCreateSession(admin1.Nick, groupName2) So(err, ShouldBeNil) So(ses2, ShouldNotBeNil) gtc1 := createChannelAndParticipants(admin1, groupName2, models.Channel_TYPE_TOPIC, ses2.ClientId, acc1.Id, acc2.Id) gtc2 := createChannelAndParticipants(admin1, groupName2, models.Channel_TYPE_TOPIC, ses2.ClientId, acc1.Id, acc2.Id) channels, err := rest.FetchChannelsByParticipants([]int64{acc1.Id, acc2.Id}, models.Channel_TYPE_TOPIC, ses1.ClientId) // there should be an err So(err, ShouldBeNil) So(channels, ShouldNotBeNil) So(len(channels), ShouldEqual, 2) So(channels[0].Channel.GroupName, ShouldEqual, groupName1) So(channels[1].Channel.GroupName, ShouldEqual, groupName1) So(tc1.Id, ShouldEqual, channels[0].Channel.Id) So(tc2.Id, ShouldEqual, channels[1].Channel.Id) channels, err = rest.FetchChannelsByParticipants([]int64{acc1.Id, acc2.Id}, models.Channel_TYPE_TOPIC, ses2.ClientId) // there should be an err So(err, ShouldBeNil) So(channels, ShouldNotBeNil) So(len(channels), ShouldEqual, 2) So(channels[0].Channel.GroupName, ShouldEqual, groupName2) So(channels[1].Channel.GroupName, ShouldEqual, groupName2) So(gtc1.Id, ShouldEqual, channels[0].Channel.Id) So(gtc2.Id, ShouldEqual, channels[1].Channel.Id) }) }) }) }
func TestAccountTesting(t *testing.T) { runner, handler := getTestHandler() defer runner.Close() // init mongo connection appConfig := config.MustRead(runner.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() Convey("given some fake account", t, func() { acc, _, name := models.CreateRandomGroupDataWithChecks() So(name, ShouldNotBeNil) So(acc, ShouldNotBeNil) Convey("it should save the document to algolia", func() { err := handler.AccountCreated(acc) So(err, ShouldBeNil) Convey("it should be able to fetch algolia data", func() { // make sure account is there So(doBasicTestForAccount(handler, acc.OldId), ShouldBeNil) _, err = modelhelper.GetUser(acc.Nick) So(err, ShouldBeNil) // update user's email selector := bson.M{"username": acc.Nick} newEmail := "mehmetalixsavasx1x2x" + models.RandomGroupName() + "@koding.com" updateQuery := bson.M{"email": newEmail} err = modelhelper.UpdateUser(selector, updateQuery) So(err, ShouldBeNil) err = handler.AccountUpdated(acc) So(err, ShouldBeNil) index, err := handler.indexes.GetIndex(IndexAccounts) So(err, ShouldBeNil) params := make(map[string]interface{}) record, err := index.Search("mehmetalixsavasx1x2x", params) So(err, ShouldBeNil) hist, ok := record.(map[string]interface{})["hits"] usernames := make([]string, 0) objects := make([]string, 0) if ok { hinter, ok := hist.([]interface{}) if ok { for _, v := range hinter { val, k := v.(map[string]interface{}) if k { object := val["objectID"].(string) value := val["nick"].(string) usernames = append(usernames, value) objects = append(objects, object) _, err = index.DeleteObject(object) So(err, ShouldBeNil) } } } } So(usernames, ShouldNotBeNil) So(objects, ShouldNotBeNil) }) Convey("it should be able to fetch many account with given query", func() { So(doBasicTestForAccount(handler, acc.OldId), ShouldBeNil) // we create 10 acc for algolia test for i := 0; i < 10; i++ { ac, _, _ := models.CreateRandomGroupDataWithChecks() err := handler.AccountCreated(ac) So(err, ShouldBeNil) selector := bson.M{"username": ac.Nick} newEmail := "mehmetali-test" + models.RandomGroupName() + "@koding.com" updateQuery := bson.M{"email": newEmail} err = modelhelper.UpdateUser(selector, updateQuery) So(err, ShouldBeNil) err = handler.AccountUpdated(ac) So(err, ShouldBeNil) time.Sleep(1 * time.Second) } //required for getting algolia datas correctly time.Sleep(5 * time.Second) _, err = modelhelper.GetUser(acc.Nick) So(err, ShouldBeNil) index, err := handler.indexes.GetIndex(IndexAccounts) So(err, ShouldBeNil) params := make(map[string]interface{}) record, err := index.Search("mehmetali-test", params) So(err, ShouldBeNil) hist, ok := record.(map[string]interface{})["hits"] usernames := make([]string, 0) objects := make([]string, 0) if ok { hinter, ok := hist.([]interface{}) if ok { for _, v := range hinter { val, k := v.(map[string]interface{}) if k { object := val["objectID"].(string) value := val["nick"].(string) usernames = append(usernames, value) objects = append(objects, object) } } } } So(len(usernames), ShouldBeGreaterThan, 0) So(len(objects), ShouldBeGreaterThan, 0) Convey("it should be able to delete many account with given query", func() { So(doBasicTestForAccount(handler, acc.OldId), ShouldBeNil) for i := 0; i < 10; i++ { ac, _, _ := models.CreateRandomGroupDataWithChecks() err := handler.AccountCreated(ac) So(err, ShouldBeNil) selector := bson.M{"username": ac.Nick} newEmail := "mehmetali-test" + models.RandomGroupName() + "@koding.com" updateQuery := bson.M{"email": newEmail} err = modelhelper.UpdateUser(selector, updateQuery) So(err, ShouldBeNil) err = handler.AccountUpdated(ac) So(err, ShouldBeNil) time.Sleep(1 * time.Second) } time.Sleep(5 * time.Second) _, err = modelhelper.GetUser(acc.Nick) So(err, ShouldBeNil) _, err := handler.indexes.GetIndex(IndexAccounts) So(err, ShouldBeNil) usernames := make([]string, 0) objects := make([]string, 0) nbHits, _ := record.(map[string]interface{})["nbHits"] nbPages, _ := record.(map[string]interface{})["nbPages"] var pages float64 = nbPages.(float64) var nbHit float64 = nbHits.(float64) for pages > 0 && nbHit != 0 { record, err := index.Search("mehmetali-test", params) hist, ok := record.(map[string]interface{})["hits"] // fmt.Println("hist is :", hist) nbHits, _ := record.(map[string]interface{})["nbHits"] nbPages, _ := record.(map[string]interface{})["nbPages"] pages = nbPages.(float64) nbHit = nbHits.(float64) if ok { hinter, ok := hist.([]interface{}) if ok { for _, v := range hinter { val, k := v.(map[string]interface{}) if k { object := val["objectID"].(string) value := val["nick"].(string) usernames = append(usernames, value) objects = append(objects, object) _, err = index.DeleteObject(object) So(err, ShouldBeNil) } } } } } lenghtUsernames := len(usernames) lenghtObjects := len(objects) So(lenghtUsernames, ShouldBeGreaterThan, 10) So(lenghtObjects, ShouldBeGreaterThan, 10) }) Convey("it should have delete algolia accounts", func() { fmt.Println("=============>>>>>>>>>>>>>>>>>>>") fmt.Println("=============>>>>>>>>>>>>>>>>>>>") fmt.Println("it should have delete algolia accounts") fmt.Println("=============>>>>>>>>>>>>>>>>>>>") fmt.Println("=============>>>>>>>>>>>>>>>>>>>") // make sure account is there So(doBasicTestForAccount(handler, acc.OldId), ShouldBeNil) for i := 0; i < 10; i++ { rand.Seed(time.Now().UnixNano()) strconv.FormatInt(rand.Int63(), 10) name := "guter-" + strconv.FormatInt(rand.Int63(), 10) ac, _ := models.CreateAccountInBothDbsWithNick(name) err := handler.AccountCreated(ac) So(err, ShouldBeNil) selector := bson.M{"username": ac.Nick} newEmail := "mehmetali-test" + models.RandomGroupName() + "@koding.com" updateQuery := bson.M{"email": newEmail} err = modelhelper.UpdateUser(selector, updateQuery) So(err, ShouldBeNil) err = handler.AccountUpdated(ac) So(err, ShouldBeNil) time.Sleep(1 * time.Second) } time.Sleep(5 * time.Second) _, err = handler.indexes.GetIndex(IndexAccounts) So(err, ShouldBeNil) // record, _ := index.Search("mehmetalisa", map[string]interface{}{"restrictSearchableAttributes": "email"}) // params := make(map[string]interface{}) params := map[string]interface{}{"restrictSearchableAttributes": "nick"} record, _ := index.Search("guter-", params) hits, _ := record.(map[string]interface{})["nbHits"] hit := hits.(float64) So(hit, ShouldBeGreaterThan, 0) err = handler.DeleteNicksWithQuery("guter-") So(err, ShouldBeNil) // necessary for getting datas from algolia, time.Sleep(5 * time.Second) r, err := index.Search("guter-", params) So(err, ShouldBeNil) nbHits, _ := r.(map[string]interface{})["nbHits"] nbHit := nbHits.(float64) So(nbHit, ShouldBeLessThan, 10) }) }) }) }) }
func TestChannelMessage(t *testing.T) { tests.WithRunner(t, func(r *runner.Runner) { Convey("While testing channel messages given a channel", t, func() { account, groupChannel, groupName := models.CreateRandomGroupDataWithChecks() nonOwnerAccount := models.CreateAccountInBothDbsWithCheck() _, err := modelhelper.FetchOrCreateSession(nonOwnerAccount.Nick, groupName) So(err, ShouldBeNil) ses, err := modelhelper.FetchOrCreateSession(account.Nick, groupName) So(err, ShouldBeNil) Convey("message should be able added to the group channel", func() { post, err := rest.CreatePost(groupChannel.Id, ses.ClientId) So(err, ShouldBeNil) So(post, ShouldNotBeNil) So(post.Id, ShouldNotEqual, 0) So(post.Body, ShouldNotEqual, "") Convey("message can be edited by owner", func() { initialPostBody := post.Body post.Body = "edited message" editedPost, err := rest.UpdatePost(post, ses.ClientId) So(err, ShouldBeNil) So(editedPost, ShouldNotBeNil) // body should not be same So(initialPostBody, ShouldNotEqual, editedPost.Body) }) // for now social worker handles this issue Convey("message can be edited by an admin", nil) Convey("message can not be edited by non-owner", nil) }) Convey("topic messages initialChannelId must be set as owner group channel id", func() { ses, err = modelhelper.FetchOrCreateSession(account.Nick, groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) topicChannel, err := rest.CreateChannelByGroupNameAndType(account.Id, "koding", models.Channel_TYPE_TOPIC, ses.ClientId) So(err, ShouldBeNil) So(topicChannel, ShouldNotBeNil) post, err := rest.CreatePost(topicChannel.Id, ses.ClientId) So(err, ShouldBeNil) So(post, ShouldNotBeNil) So(post.InitialChannelId, ShouldNotEqual, topicChannel.Id) publicChannel, err := rest.GetChannelWithToken(post.InitialChannelId, ses.ClientId) So(err, ShouldBeNil) So(publicChannel.TypeConstant, ShouldEqual, models.Channel_TYPE_GROUP) }) Convey("message can be deleted by owner", func() { post, err := rest.CreatePost(groupChannel.Id, ses.ClientId) So(err, ShouldBeNil) So(post, ShouldNotBeNil) err = rest.DeletePost(post.Id, ses.ClientId) So(err, ShouldBeNil) post2, err := rest.GetPost(post.Id, ses.ClientId) So(err, ShouldNotBeNil) So(post2, ShouldBeNil) }) Convey("message should not have payload, if user does not allow", func() { h := http.Header{} h.Add("X-Forwarded-For", "208.72.139.54") post, err := rest.CreatePostWithHeader(groupChannel.Id, h, ses.ClientId) So(err, ShouldBeNil) So(post, ShouldNotBeNil) So(post.Payload, ShouldBeNil) }) Convey("Message should have location if user allowed", func() { payload := make(map[string]interface{}) payload["saveLocation"] = "Manisa" post, err := rest.CreatePostWithPayload(groupChannel.Id, payload, ses.ClientId) So(err, ShouldBeNil) So(post, ShouldNotBeNil) So(post.Payload, ShouldNotBeNil) So(*(post.Payload["saveLocation"]), ShouldEqual, "Manisa") }) // handled by social worker Convey("message can be deleted by an admin", nil) Convey("message can not be edited by non-owner", nil) Convey("we should be able to get replies with \"from\" query param", nil) Convey("while deleting messages, they should be removed from all channels", nil) Convey("message can contain payload", func() { payload := make(map[string]interface{}) payload["key1"] = "value1" payload["key2"] = 2 payload["key3"] = true payload["key4"] = 3.4 post, err := rest.CreatePostWithPayload(groupChannel.Id, payload, ses.ClientId) So(err, ShouldBeNil) So(post, ShouldNotBeNil) So(post.Payload, ShouldNotBeNil) So(*(post.Payload["key1"]), ShouldEqual, "value1") So(*(post.Payload["key2"]), ShouldEqual, "2") So(*(post.Payload["key3"]), ShouldEqual, "true") So(*(post.Payload["key4"]), ShouldEqual, "3.4") }) }) }) }
func TestFollowedTopics(t *testing.T) { tests.WithRunner(t, func(r *runner.Runner) { Convey("While testing followed topics", t, func() { Convey("First Create User", func() { account, _, groupName := models.CreateRandomGroupDataWithChecks() ses, err := modelhelper.FetchOrCreateSession(account.Nick, groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) nonOwnerAccount := models.NewAccount() nonOwnerAccount.OldId = AccountOldId.Hex() nonOwnerAccount, err = rest.CreateAccount(nonOwnerAccount) So(err, ShouldBeNil) So(nonOwnerAccount, ShouldNotBeNil) topicChannel1, err := rest.CreateChannelByGroupNameAndType( account.Id, groupName, models.Channel_TYPE_TOPIC, ses.ClientId, ) So(err, ShouldBeNil) topicChannel2, err := rest.CreateChannelByGroupNameAndType( account.Id, groupName, models.Channel_TYPE_TOPIC, ses.ClientId, ) So(err, ShouldBeNil) Convey("user should be able to follow one topic", func() { channelParticipant, err := rest.AddChannelParticipant(topicChannel1.Id, ses.ClientId, account.Id) // there should be an err So(err, ShouldBeNil) // channel should be nil So(channelParticipant, ShouldNotBeNil) followedChannels, err := rest.FetchFollowedChannels(account.Id, ses.ClientId) So(err, ShouldBeNil) So(followedChannels, ShouldNotBeNil) So(len(followedChannels), ShouldBeGreaterThanOrEqualTo, 1) }) Convey("user should be able to follow two topic", func() { channelParticipant, err := rest.AddChannelParticipant(topicChannel1.Id, ses.ClientId, account.Id) So(err, ShouldBeNil) So(channelParticipant, ShouldNotBeNil) channelParticipant, err = rest.AddChannelParticipant(topicChannel2.Id, ses.ClientId, account.Id) So(err, ShouldBeNil) So(channelParticipant, ShouldNotBeNil) followedChannels, err := rest.FetchFollowedChannels(account.Id, ses.ClientId) So(err, ShouldBeNil) So(followedChannels, ShouldNotBeNil) So(len(followedChannels), ShouldBeGreaterThanOrEqualTo, 2) }) Convey("user should be participant of the followed topic", func() { channelParticipant, err := rest.AddChannelParticipant(topicChannel1.Id, ses.ClientId, account.Id) // there should be an err So(err, ShouldBeNil) // channel should be nil So(channelParticipant, ShouldNotBeNil) followedChannels, err := rest.FetchFollowedChannels(account.Id, ses.ClientId) So(err, ShouldBeNil) So(followedChannels, ShouldNotBeNil) So(len(followedChannels), ShouldBeGreaterThanOrEqualTo, 1) So(followedChannels[0].IsParticipant, ShouldBeTrue) }) Convey("user should not be a participant of the un-followed topic", func() { channelParticipant, err := rest.AddChannelParticipant(topicChannel1.Id, ses.ClientId, account.Id) So(err, ShouldBeNil) So(channelParticipant, ShouldNotBeNil) followedChannels, err := rest.FetchFollowedChannels(account.Id, ses.ClientId) So(err, ShouldBeNil) So(followedChannels, ShouldNotBeNil) currentParticipatedChannelCount := len(followedChannels) channelParticipant, err = rest.DeleteChannelParticipant(topicChannel1.Id, ses.ClientId, account.Id) So(err, ShouldBeNil) So(channelParticipant, ShouldNotBeNil) followedChannels, err = rest.FetchFollowedChannels(account.Id, ses.ClientId) So(err, ShouldBeNil) So(followedChannels, ShouldNotBeNil) lastParticipatedChannelCount := len(followedChannels) So(currentParticipatedChannelCount-lastParticipatedChannelCount, ShouldEqual, 1) }) Convey("participant count of the followed topic should be greater than 0", func() { channelParticipant, err := rest.AddChannelParticipant(topicChannel1.Id, ses.ClientId, account.Id) // there should be an err So(err, ShouldBeNil) // channel should be nil So(channelParticipant, ShouldNotBeNil) followedChannels, err := rest.FetchFollowedChannels(account.Id, ses.ClientId) So(err, ShouldBeNil) So(followedChannels, ShouldNotBeNil) So(len(followedChannels), ShouldBeGreaterThanOrEqualTo, 1) So(followedChannels[0].ParticipantCount, ShouldBeGreaterThanOrEqualTo, 1) }) }) }) }) }