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) }) }) }) }
func TestAccountParticipantAdded(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 account", t, func() { acc, err := models.CreateAccountInBothDbs() So(err, ShouldBeNil) So(acc, ShouldNotBeNil) tc1 := models.CreateTypedGroupedChannelWithTest( acc.Id, models.Channel_TYPE_TOPIC, models.Channel_KODING_NAME, ) models.AddParticipantsWithTest(tc1.Id, acc.Id) cp := &models.ChannelParticipant{ ChannelId: tc1.Id, AccountId: acc.Id, StatusConstant: models.ChannelParticipant_STATUS_ACTIVE, } Convey("when the account is already on algolia", func() { err := handler.AccountCreated(acc) So(err, ShouldBeNil) So(doBasicTestForAccount(handler, acc.OldId), ShouldBeNil) err = handler.ParticipantCreated(cp) So(err, ShouldBeNil) So(doBasicTestForAccount(handler, acc.OldId), ShouldBeNil) So(makeSureHasTag(handler, acc.OldId, strconv.FormatInt(tc1.Id, 10)), ShouldBeNil) Convey("trying to add again should success", func() { err = handler.ParticipantCreated(cp) So(err, ShouldBeNil) So(doBasicTestForAccount(handler, acc.OldId), ShouldBeNil) So(makeSureHasTag(handler, acc.OldId, strconv.FormatInt(tc1.Id, 10)), ShouldBeNil) So(makeSureTagLen(handler, acc.OldId, 2), ShouldBeNil) }) }) Convey("when the account is not on algolia", func() { err = handler.ParticipantCreated(cp) So(err, ShouldBeNil) So(doBasicTestForAccount(handler, acc.OldId), ShouldBeNil) So(makeSureHasTag(handler, acc.OldId, strconv.FormatInt(tc1.Id, 10)), ShouldBeNil) So(makeSureTagLen(handler, acc.OldId, 1), ShouldBeNil) }) }) }
func TestCollaborationSesionEnd(t *testing.T) { r := runner.New("collaboration-tests") err := r.Init() if err != nil { panic(err) } defer r.Close() appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() Convey("while testing collaboration session end", t, func() { Convey("we should be able to create a doc on google drive", func() { Convey("we should be able to delete a created doc", func() { Convey("deleting an already deleted doc should not give error", func() { }) }) }) Convey("trying to delete a non-existing doc should not give error", func() { }) }) Convey("while pinging collaboration", t, func() { // owner owner, err := apimodels.CreateAccountInBothDbs() So(err, ShouldBeNil) So(owner, ShouldNotBeNil) groupName := apimodels.RandomGroupName() apimodels.CreateTypedGroupedChannelWithTest( owner.Id, apimodels.Channel_TYPE_GROUP, groupName, ) ownerSession, err := modelhelper.FetchOrCreateSession(owner.Nick, groupName) So(err, ShouldBeNil) So(ownerSession, ShouldNotBeNil) Convey("reponse should be success", func() { p := &models.Ping{ AccountId: 1, FileId: "hello", } res, err := rest.CollaborationPing(p, ownerSession.ClientId) So(err, ShouldBeNil) So(res, ShouldNotBeNil) }) }) }
func TestGetSocialIdFromEmail(t *testing.T) { r := runner.New("test") if err := r.Init(); err != nil { t.Fatalf("couldnt start bongo %s", err.Error()) } defer r.Close() // init mongo connection appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() Convey("while getting account id in the mail", t, func() { Convey("From fields should be saved in db, otherwise return err", func() { m := &Mail{ From: "mailisnotexist@abo", } gid, err := m.getSocialIdFromEmail() So(err, ShouldNotBeNil) So(err, ShouldEqual, ErrEmailIsNotFetched) So(gid, ShouldEqual, 0) }) Convey("should not be any error if all is well", func() { acc, err := socialapimodels.CreateAccountInBothDbs() So(err, ShouldBeNil) mongoUser, err := modelhelper.GetUser(acc.Nick) So(err, ShouldBeNil) m := &Mail{ From: mongoUser.Email, } gid, err := m.getSocialIdFromEmail() So(err, ShouldBeNil) So(gid, ShouldEqual, acc.Id) }) }) }
func CreateAccountWithDailyDigest() (*models.Account, error) { acc, err := models.CreateAccountInBothDbs() if err != nil { return nil, err } eFreq := kodingmodels.EmailFrequency{ Global: true, Daily: true, Comment: true, } err = modelhelper.UpdateEmailFrequency(acc.OldId, eFreq) if err != nil { return nil, err } return acc, nil }
func TestGetAccount(t *testing.T) { r := runner.New("test") if err := r.Init(); err != nil { t.Fatalf("couldnt start bongo %s", err.Error()) } defer r.Close() // init mongo connection appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() Convey("while testing get account", t, func() { Convey("returns empty if parameter is invalid", func() { acc, err := GetAccount("interestingEmail@somethinglikethat") So(err, ShouldNotBeNil) So(acc, ShouldBeNil) }) Convey("returns empty if parameter is empty", func() { acc, err := GetAccount("") So(err, ShouldNotBeNil) So(acc, ShouldBeNil) }) Convey("Should return blank if parameter is empty", func() { acc, err := socialapimodels.CreateAccountInBothDbs() So(err, ShouldBeNil) mongoUser, err := modelhelper.GetUser(acc.Nick) So(err, ShouldBeNil) m := &Mail{ From: mongoUser.Email, } ga, err := GetAccount(m.From) So(err, ShouldBeNil) So(ga, ShouldNotBeNil) }) }) }
func TestMailParse(t *testing.T) { r := runner.New("test") err := r.Init() if err != nil { panic(err) } defer r.Close() appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() Convey("while sending mail", t, func() { Convey("reponse should be success", func() { acc, err := socialapimodels.CreateAccountInBothDbs() So(err, ShouldBeNil) c := socialapimodels.CreateChannelWithTest(acc.Id) socialapimodels.AddParticipantsWithTest(c.Id, acc.Id) cm := socialapimodels.CreateMessage(c.Id, acc.Id, socialapimodels.ChannelMessage_TYPE_POST) So(cm, ShouldNotBeNil) mongoUser, err := modelhelper.GetUser(acc.Nick) So(err, ShouldBeNil) p := &models.Mail{ From: mongoUser.Email, OriginalRecipient: fmt.Sprintf("*****@*****.**", c.Id), MailboxHash: fmt.Sprintf("messageid.%d", cm.Id), TextBody: "Its an example of text message", StrippedTextReply: "This one is reply message", } res, err := rest.MailParse(p) So(err, ShouldBeNil) So(res, ShouldNotBeNil) }) }) }
func TestCollaborationChannels(t *testing.T) { tests.WithRunner(t, func(r *runner.Runner) { Convey("while testing collaboration channel", t, func() { devrim, err := models.CreateAccountInBothDbsWithNick("devrim") So(err, ShouldBeNil) So(devrim, ShouldNotBeNil) sinan, err := models.CreateAccountInBothDbsWithNick("sinan") So(err, ShouldBeNil) So(sinan, ShouldNotBeNil) groupName := models.RandomGroupName() // cretae admin user account, err := models.CreateAccountInBothDbs() tests.ResultedWithNoErrorCheck(account, err) // fetch admin's session ses, err := modelhelper.FetchOrCreateSession(account.Nick, groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) groupChannel := models.CreateTypedGroupedChannelWithTest( account.Id, models.Channel_TYPE_GROUP, groupName, ) groupChannel.AddParticipant(account.Id) _, err = groupChannel.AddParticipant(devrim.Id) So(err, ShouldBeNil) _, err = groupChannel.AddParticipant(sinan.Id) So(err, ShouldBeNil) recipient, err := models.CreateAccountInBothDbs() tests.ResultedWithNoErrorCheck(recipient, err) recipient2, err := models.CreateAccountInBothDbs() tests.ResultedWithNoErrorCheck(recipient2, err) Convey("one can send initiate the collaboration channel with only him", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "this is a body for private message" pmr.GroupName = groupName pmr.Recipients = []string{} pmr.TypeConstant = models.Channel_TYPE_COLLABORATION cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) }) Convey("one can send initiate the collaboration channel with 2 participants", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "this is a body message for private message @devrim @sinan" pmr.GroupName = groupName pmr.Recipients = []string{"devrim", "sinan"} pmr.TypeConstant = models.Channel_TYPE_COLLABORATION cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) }) Convey("if group name is nil, should not fail to create collaboration channel", func() { ses, err := modelhelper.FetchOrCreateSession(sinan.Nick, "koding") So(err, ShouldBeNil) So(ses, ShouldNotBeNil) groupChannel, err := rest.CreateChannelByGroupNameAndType( sinan.Id, "koding", models.Channel_TYPE_GROUP, ses.ClientId, ) tests.ResultedWithNoErrorCheck(groupChannel, err) groupChannel.AddParticipant(devrim.Id) // ignore error groupChannel.AddParticipant(account.Id) // ignore error groupChannel.AddParticipant(sinan.Id) // ignore error pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "this is a body for private message @devrim @sinan" pmr.GroupName = "" pmr.Recipients = []string{"devrim", "sinan"} pmr.TypeConstant = models.Channel_TYPE_COLLABORATION cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) }) // we give token as parameter to SendPrivateChannelRequest // handler sets the accountId and groupname if not defined in handler's function // So it should not return any error unless account is defined Convey("if sender is not defined should not fail to create collaboration channel", func() { pmr := models.ChannelRequest{} pmr.AccountId = 0 pmr.Body = "this is a body for private message" pmr.GroupName = "" pmr.Recipients = []string{} pmr.TypeConstant = models.Channel_TYPE_COLLABORATION cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) }) Convey("one can send private message to multiple person", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "this is a body for private message @sinan" pmr.GroupName = groupName pmr.Recipients = []string{"sinan"} pmr.TypeConstant = models.Channel_TYPE_COLLABORATION cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) }) Convey("response should have created channel", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "this is a body for private message @devrim @sinan" pmr.GroupName = groupName pmr.Recipients = []string{"devrim", "sinan"} pmr.TypeConstant = models.Channel_TYPE_COLLABORATION cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) So(cmc.Channel.TypeConstant, ShouldEqual, models.Channel_TYPE_COLLABORATION) So(cmc.Channel.Id, ShouldBeGreaterThan, 0) So(cmc.Channel.GroupName, ShouldEqual, groupName) So(cmc.Channel.PrivacyConstant, ShouldEqual, models.Channel_PRIVACY_PRIVATE) }) Convey("send response should have participant status data", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "this is a body for private message @devrim @sinan" pmr.GroupName = groupName pmr.Recipients = []string{"devrim", "sinan"} pmr.TypeConstant = models.Channel_TYPE_COLLABORATION cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) So(cmc.IsParticipant, ShouldBeTrue) }) Convey("send response should have participant count", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "this is a body for @sinan private message @devrim" pmr.GroupName = groupName pmr.Recipients = []string{"devrim", "sinan"} pmr.TypeConstant = models.Channel_TYPE_COLLABORATION cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) So(cmc.ParticipantCount, ShouldEqual, 3) }) Convey("send response should have participant preview", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "this is @sinan a body for @devrim private message" pmr.GroupName = groupName pmr.Recipients = []string{"sinan", "devrim"} pmr.TypeConstant = models.Channel_TYPE_COLLABORATION cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) So(len(cmc.ParticipantsPreview), ShouldEqual, 3) }) Convey("send response should have last Message", func() { body := "hi @devrim this is a body for private message also for @sinan" pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = body pmr.GroupName = groupName pmr.Recipients = []string{"sinan", "devrim"} pmr.TypeConstant = models.Channel_TYPE_COLLABORATION cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) So(cmc.LastMessage.Message.Body, ShouldEqual, body) }) Convey("channel messages should be listed by all recipients", func() { // // use a different group name // // in order not to interfere with another request // groupName := "testgroup" + strconv.FormatInt(rand.Int63(), 10) body := "hi @devrim this is a body for private message also for @sinan" pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = body pmr.GroupName = groupName pmr.Recipients = []string{"sinan", "devrim"} pmr.TypeConstant = models.Channel_TYPE_COLLABORATION cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) query := &request.Query{ AccountId: account.Id, GroupName: groupName, Type: models.Channel_TYPE_COLLABORATION, } pm, err := rest.GetPrivateChannels(query, ses.ClientId) So(err, ShouldBeNil) So(pm, ShouldNotBeNil) So(len(pm), ShouldNotEqual, 0) So(pm[0], ShouldNotBeNil) So(pm[0].Channel.TypeConstant, ShouldEqual, models.Channel_TYPE_COLLABORATION) So(pm[0].Channel.Id, ShouldEqual, cmc.Channel.Id) So(pm[0].Channel.GroupName, ShouldEqual, cmc.Channel.GroupName) So(pm[0].LastMessage.Message.Body, ShouldEqual, cmc.LastMessage.Message.Body) So(pm[0].Channel.PrivacyConstant, ShouldEqual, models.Channel_PRIVACY_PRIVATE) So(len(pm[0].ParticipantsPreview), ShouldEqual, 3) So(pm[0].IsParticipant, ShouldBeTrue) }) Convey("user should be able to search collaboration channels via purpose field", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "search collaboration channel" pmr.GroupName = groupName pmr.Recipients = []string{"sinan", "devrim"} pmr.Purpose = "test me up" pmr.TypeConstant = models.Channel_TYPE_COLLABORATION cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) query := request.Query{ AccountId: account.Id, GroupName: groupName, Type: models.Channel_TYPE_COLLABORATION, } _, err = rest.SearchPrivateChannels(&query, ses.ClientId) So(err, ShouldNotBeNil) query.Name = "test" pm, err := rest.SearchPrivateChannels(&query, ses.ClientId) So(err, ShouldBeNil) So(pm, ShouldNotBeNil) So(len(pm), ShouldNotEqual, 0) So(pm[0], ShouldNotBeNil) So(pm[0].Channel.TypeConstant, ShouldEqual, models.Channel_TYPE_COLLABORATION) So(pm[0].Channel.Id, ShouldEqual, cmc.Channel.Id) So(pm[0].Channel.GroupName, ShouldEqual, cmc.Channel.GroupName) So(pm[0].LastMessage.Message.Body, ShouldEqual, cmc.LastMessage.Message.Body) So(pm[0].Channel.PrivacyConstant, ShouldEqual, models.Channel_PRIVACY_PRIVATE) So(pm[0].IsParticipant, ShouldBeTrue) }) Convey("user join activity should be listed by recipients", func() { groupName := models.RandomGroupName() // cretae admin user account, err := models.CreateAccountInBothDbs() tests.ResultedWithNoErrorCheck(account, err) // fetch admin's session ses, err := modelhelper.FetchOrCreateSession(account.Nick, groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) groupChannel := models.CreateTypedGroupedChannelWithTest( account.Id, models.Channel_TYPE_GROUP, groupName, ) So(groupChannel, ShouldNotBeNil) _, err = groupChannel.AddParticipant(account.Id) So(err, ShouldBeNil) _, err = groupChannel.AddParticipant(devrim.Id) So(err, ShouldBeNil) _, err = groupChannel.AddParticipant(sinan.Id) So(err, ShouldBeNil) _, err = groupChannel.AddParticipant(recipient.Id) So(err, ShouldBeNil) pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "test collaboration channel participants" pmr.GroupName = groupName pmr.Recipients = []string{"sinan", "devrim"} pmr.TypeConstant = models.Channel_TYPE_COLLABORATION cc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cc, ShouldNotBeNil) history, err := rest.GetHistory( cc.Channel.Id, &request.Query{ AccountId: account.Id, }, ses.ClientId, ) So(err, ShouldBeNil) So(history, ShouldNotBeNil) So(len(history.MessageList), ShouldEqual, 2) // add participant _, err = rest.AddChannelParticipant(cc.Channel.Id, ses.ClientId, recipient.Id) So(err, ShouldBeNil) history, err = rest.GetHistory( cc.Channel.Id, &request.Query{ AccountId: account.Id, }, ses.ClientId, ) So(err, ShouldBeNil) So(history, ShouldNotBeNil) So(len(history.MessageList), ShouldEqual, 3) So(history.MessageList[0].Message, ShouldNotBeNil) So(history.MessageList[0].Message.TypeConstant, ShouldEqual, models.ChannelMessage_TYPE_SYSTEM) So(history.MessageList[0].Message.Payload, ShouldNotBeNil) addedBy, ok := history.MessageList[0].Message.Payload["addedBy"] So(ok, ShouldBeTrue) So(*addedBy, ShouldEqual, account.Nick) systemType, ok := history.MessageList[0].Message.Payload["systemType"] So(ok, ShouldBeTrue) So(*systemType, ShouldEqual, models.ChannelRequestMessage_TYPE_JOIN) // try to add same participant _, err = rest.AddChannelParticipant(cc.Channel.Id, ses.ClientId, recipient.Id) So(err, ShouldBeNil) history, err = rest.GetHistory( cc.Channel.Id, &request.Query{ AccountId: account.Id, }, ses.ClientId, ) So(err, ShouldBeNil) So(history, ShouldNotBeNil) So(len(history.MessageList), ShouldEqual, 3) }) Convey("user should not be able to edit join messages", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "test collaboration channel participants again" pmr.GroupName = groupName pmr.Recipients = []string{"devrim"} pmr.TypeConstant = models.Channel_TYPE_COLLABORATION cc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cc, ShouldNotBeNil) _, err = rest.AddChannelParticipant(cc.Channel.Id, ses.ClientId, recipient.Id) So(err, ShouldBeNil) ses, err := modelhelper.FetchOrCreateSession(account.Nick, groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) history, err := rest.GetHistory( cc.Channel.Id, &request.Query{ AccountId: account.Id, }, ses.ClientId, ) So(err, ShouldBeNil) So(history, ShouldNotBeNil) So(len(history.MessageList), ShouldEqual, 3) joinMessage := history.MessageList[0].Message So(joinMessage, ShouldNotBeNil) _, err = rest.UpdatePost(joinMessage, ses.ClientId) So(err, ShouldNotBeNil) }) Convey("first chat message should include initial participants", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "test initial participation message" pmr.GroupName = groupName pmr.Recipients = []string{"sinan", "devrim"} pmr.TypeConstant = models.Channel_TYPE_COLLABORATION cc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cc, ShouldNotBeNil) ses, err := modelhelper.FetchOrCreateSession(account.Nick, groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) history, err := rest.GetHistory( cc.Channel.Id, &request.Query{ AccountId: account.Id, }, ses.ClientId, ) So(err, ShouldBeNil) So(history, ShouldNotBeNil) So(len(history.MessageList), ShouldEqual, 2) joinMessage := history.MessageList[1].Message So(joinMessage.TypeConstant, ShouldEqual, models.ChannelMessage_TYPE_SYSTEM) So(joinMessage.Payload, ShouldNotBeNil) initialParticipants, ok := joinMessage.Payload["initialParticipants"] So(ok, ShouldBeTrue) systemType, ok := history.MessageList[1].Message.Payload["systemType"] So(ok, ShouldBeTrue) So(*systemType, ShouldEqual, models.ChannelRequestMessage_TYPE_INIT) participants := make([]string, 0) err = json.Unmarshal([]byte(*initialParticipants), &participants) So(err, ShouldBeNil) So(len(participants), ShouldEqual, 2) So(participants, ShouldContain, "devrim") // So(*addedBy, ShouldEqual, account.OldId) }) }) }) }
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 TestChannelCreation(t *testing.T) { tests.WithRunner(t, func(r *runner.Runner) { Convey("while testing channel", t, func() { Convey("First Create Users", func() { account, err := models.CreateAccountInBothDbs() So(err, ShouldBeNil) groupName := models.RandomGroupName() ses, err := modelhelper.FetchOrCreateSession(account.Nick, groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) groupChannel := models.CreateTypedGroupedChannelWithTest( account.Id, models.Channel_TYPE_GROUP, groupName, ) So(err, ShouldBeNil) So(groupChannel, ShouldNotBeNil) nonOwnerAccount := models.NewAccount() nonOwnerAccount.OldId = AccountOldId2.Hex() nonOwnerAccount, err = rest.CreateAccount(nonOwnerAccount) So(err, ShouldBeNil) So(nonOwnerAccount, ShouldNotBeNil) noses, err := modelhelper.FetchOrCreateSession( nonOwnerAccount.Nick, groupName, ) So(err, ShouldBeNil) So(noses, ShouldNotBeNil) Convey("we should be able to create it", func() { channel1, err := rest.CreateChannelByGroupNameAndType( account.Id, groupName, models.Channel_TYPE_PRIVATE_MESSAGE, ses.ClientId, ) So(err, ShouldBeNil) So(channel1, ShouldNotBeNil) _, err = rest.AddChannelParticipant(channel1.Id, ses.ClientId, account.Id) So(err, ShouldBeNil) Convey("owner should be able to update it", func() { updatedPurpose := "another purpose from the paradise" channel1.Purpose = updatedPurpose channel2, err := rest.UpdateChannel(channel1, ses.ClientId) So(err, ShouldBeNil) So(channel2, ShouldNotBeNil) So(channel1.Purpose, ShouldEqual, channel1.Purpose) Convey("owner should be able to update payload", func() { if channel1.Payload == nil { channel1.Payload = gorm.Hstore{} } value := "value" channel1.Payload = gorm.Hstore{ "key": &value, } channel2, err := rest.UpdateChannel(channel1, ses.ClientId) So(err, ShouldBeNil) So(channel2, ShouldNotBeNil) So(channel1.Payload, ShouldNotBeNil) So(*channel1.Payload["key"], ShouldEqual, value) }) }) Convey("participant should be able to update only purpose, not name or payload", func() { _, err = rest.AddChannelParticipant(channel1.Id, ses.ClientId, nonOwnerAccount.Id) So(err, ShouldBeNil) updatedPurpose := "ChannelPurposeUpdated" updatedName := "ChannelNameUpdated" channel1.Name = updatedName channel1.Purpose = updatedPurpose channel2, err := rest.UpdateChannel(channel1, noses.ClientId) So(err, ShouldBeNil) So(channel2, ShouldNotBeNil) So(channel2.Name, ShouldNotBeNil) // participant cannot update channel name // can update only purpose of the channel So(channel2.Name, ShouldNotEqual, updatedName) So(channel2.Purpose, ShouldEqual, updatedPurpose) }) Convey("owner should be get channel by name", func() { channel2, err := rest.FetchChannelByName( account.Id, channel1.Name, channel1.GroupName, channel1.TypeConstant, ses.ClientId, ) So(err, ShouldBeNil) So(channel2, ShouldNotBeNil) So(channel1.Id, ShouldEqual, channel2.Id) So(channel1.Name, ShouldEqual, channel2.Name) So(channel1.GroupName, ShouldEqual, channel2.GroupName) So(channel1.GroupName, ShouldEqual, channel2.GroupName) }) Convey("unread count should be set", func() { channelContainer, err := rest.FetchChannelContainerByName(account.Id, channel1.Name, channel1.GroupName, channel1.TypeConstant, ses.ClientId) So(err, ShouldBeNil) So(channelContainer, ShouldNotBeNil) So(channelContainer.UnreadCount, ShouldEqual, 0) post, err := rest.CreatePost(channel1.Id, ses.ClientId) So(err, ShouldBeNil) So(post, ShouldNotBeNil) channelContainer, err = rest.FetchChannelContainerByName(account.Id, channel1.Name, channel1.GroupName, channel1.TypeConstant, ses.ClientId) So(err, ShouldBeNil) So(channelContainer, ShouldNotBeNil) So(channelContainer.UnreadCount, ShouldEqual, 1) }) Convey("non-owner should not be able to update it", func() { updatedPurpose := "another purpose from the paradise" channel1.Purpose = updatedPurpose channel1.CreatorId = nonOwnerAccount.Id channel2, err := rest.UpdateChannel(channel1, noses.ClientId) So(err, ShouldNotBeNil) So(channel2, ShouldBeNil) }) Convey("non-owner should not be able to get channel by name", func() { _, err := rest.FetchChannelByName( nonOwnerAccount.Id, channel1.Name, channel1.GroupName, channel1.TypeConstant, noses.ClientId, ) So(err, ShouldNotBeNil) }) }) Convey("normal user shouldnt be able to add new participants to pinned activity channel", func() { channel1, err := rest.CreateChannelByGroupNameAndType( account.Id, groupName, models.Channel_TYPE_PINNED_ACTIVITY, ses.ClientId, ) So(err, ShouldBeNil) So(channel1, ShouldNotBeNil) channelParticipant, err := rest.AddChannelParticipant(channel1.Id, noses.ClientId, nonOwnerAccount.Id) // there should be an err So(err, ShouldNotBeNil) // channel should be nil So(channelParticipant, ShouldBeNil) }) Convey("owner should be able list participants", func() { channel1, err := rest.CreateChannelByGroupNameAndType( account.Id, groupName, models.Channel_TYPE_DEFAULT, ses.ClientId, ) So(err, ShouldBeNil) So(channel1, ShouldNotBeNil) // add first participant channelParticipant1, err := rest.AddChannelParticipant(channel1.Id, ses.ClientId, nonOwnerAccount.Id) // there should be an err So(err, ShouldBeNil) // channel should be nil So(channelParticipant1, ShouldNotBeNil) nonOwnerAccount2 := models.NewAccount() nonOwnerAccount2.OldId = AccountOldId3.Hex() nonOwnerAccount2, err = rest.CreateAccount(nonOwnerAccount2) So(err, ShouldBeNil) So(nonOwnerAccount2, ShouldNotBeNil) channelParticipant2, err := rest.AddChannelParticipant(channel1.Id, ses.ClientId, nonOwnerAccount2.Id) // there should be an err So(err, ShouldBeNil) // channel should be nil So(channelParticipant2, ShouldNotBeNil) participants, err := rest.ListChannelParticipants(channel1.Id, ses.ClientId) // there should be an err So(err, ShouldBeNil) So(participants, ShouldNotBeNil) // owner // nonOwner1 // nonOwner2 So(len(participants), ShouldEqual, 3) }) Convey("normal user should be able to list participants", func() { channel1, err := rest.CreateChannelByGroupNameAndType( account.Id, groupName, models.Channel_TYPE_DEFAULT, ses.ClientId, ) So(err, ShouldBeNil) So(channel1, ShouldNotBeNil) // add first participant channelParticipant1, err := rest.AddChannelParticipant(channel1.Id, ses.ClientId, nonOwnerAccount.Id) // there should be an err So(err, ShouldBeNil) // channel should be nil So(channelParticipant1, ShouldNotBeNil) nonOwnerAccount2 := models.NewAccount() nonOwnerAccount2.OldId = AccountOldId3.Hex() nonOwnerAccount2, err = rest.CreateAccount(nonOwnerAccount2) So(err, ShouldBeNil) So(nonOwnerAccount2, ShouldNotBeNil) nonOwnerSes2, err := modelhelper.FetchOrCreateSession(nonOwnerAccount2.Nick, groupName) So(err, ShouldBeNil) So(nonOwnerSes2, ShouldNotBeNil) channelParticipant2, err := rest.AddChannelParticipant(channel1.Id, ses.ClientId, nonOwnerAccount2.Id) // there should be an err So(err, ShouldBeNil) // channel should be nil So(channelParticipant2, ShouldNotBeNil) participants, err := rest.ListChannelParticipants(channel1.Id, nonOwnerSes2.ClientId) // there should be an err So(err, ShouldBeNil) So(participants, ShouldNotBeNil) // owner // nonOwner1 // nonOwner2 So(len(participants), ShouldEqual, 3) }) }) }) }) }
func TestTeam(t *testing.T) { r := runner.New("test") if err := r.Init(); err != nil { t.Fatalf("couldnt start bongo %s", err.Error()) } defer r.Close() appConfig := config.MustRead(r.Conf.Path) // init mongo connection modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() handler := NewController(r.Log, appConfig) Convey("given a group", t, func() { // create admin admin, err := models.CreateAccountInBothDbsWithNick("sinan") So(err, ShouldBeNil) So(admin, ShouldNotBeNil) acc1, err := models.CreateAccountInBothDbs() So(err, ShouldBeNil) So(acc1, ShouldNotBeNil) // create another account acc2, err := models.CreateAccountInBothDbs() So(err, ShouldBeNil) So(acc2, ShouldNotBeNil) groupName := models.RandomGroupName() groupChannel := models.CreateTypedGroupedChannelWithTest(admin.Id, models.Channel_TYPE_GROUP, groupName) So(groupChannel, ShouldNotBeNil) defaultChannel1 := models.CreateTypedGroupedChannelWithTest(admin.Id, models.Channel_TYPE_TOPIC, groupName) So(defaultChannel1, ShouldNotBeNil) defaultChannel2 := models.CreateTypedGroupedChannelWithTest(admin.Id, models.Channel_TYPE_TOPIC, groupName) So(defaultChannel2, ShouldNotBeNil) group := &mongomodels.Group{ Id: bson.NewObjectId(), Body: groupName, Title: groupName, Slug: groupName, Privacy: "private", Visibility: "hidden", SocialApiChannelId: strconv.FormatInt(groupChannel.Id, 10), DefaultChannels: []string{ strconv.FormatInt(defaultChannel1.Id, 10), strconv.FormatInt(defaultChannel2.Id, 10), }, } err = modelhelper.CreateGroup(group) So(err, ShouldBeNil) Convey("should success if channel is not in db", func() { cp := &models.ChannelParticipant{ AccountId: acc1.Id, ChannelId: math.MaxInt64, } err := handler.HandleParticipant(cp) So(err, ShouldBeNil) }) Convey("should success if channel is not a group channel", func() { cp := &models.ChannelParticipant{ AccountId: acc1.Id, ChannelId: defaultChannel1.Id, } err := handler.HandleParticipant(cp) So(err, ShouldBeNil) }) Convey("should success if group is not in mongo", func() { groupName := models.RandomGroupName() groupChan := models.CreateTypedGroupedChannelWithTest(admin.Id, models.Channel_TYPE_GROUP, groupName) So(groupChan, ShouldNotBeNil) cp := &models.ChannelParticipant{ AccountId: acc1.Id, ChannelId: groupChan.Id, } err := handler.HandleParticipant(cp) So(err, ShouldBeNil) }) Convey("new participant should be in default channels", func() { cp := &models.ChannelParticipant{ AccountId: acc1.Id, ChannelId: groupChannel.Id, StatusConstant: models.ChannelParticipant_STATUS_ACTIVE, } err := handler.HandleParticipant(cp) So(err, ShouldBeNil) isParticipant, err := defaultChannel1.IsParticipant(acc1.Id) So(err, ShouldBeNil) So(isParticipant, ShouldBeTrue) isParticipant, err = defaultChannel2.IsParticipant(acc1.Id) So(err, ShouldBeNil) So(isParticipant, ShouldBeTrue) Convey("after leaving group channel", func() { cp := &models.ChannelParticipant{ AccountId: acc1.Id, ChannelId: groupChannel.Id, StatusConstant: models.ChannelParticipant_STATUS_LEFT, } err := handler.HandleParticipant(cp) So(err, ShouldBeNil) Convey("should be removed from default channels", func() { isParticipant, err := defaultChannel1.IsParticipant(acc1.Id) So(err, ShouldBeNil) So(isParticipant, ShouldBeFalse) isParticipant, err = defaultChannel2.IsParticipant(acc1.Id) So(err, ShouldBeNil) So(isParticipant, ShouldBeFalse) }) Convey("should be removed from all channels", func() { ids, err := cp.FetchAllParticipatedChannelIdsInGroup(cp.AccountId, groupChannel.GroupName) So(err, ShouldBeNil) So(len(ids), ShouldEqual, 0) }) }) Convey("after being blocked", func() { cp := &models.ChannelParticipant{ AccountId: acc1.Id, ChannelId: groupChannel.Id, StatusConstant: models.ChannelParticipant_STATUS_BLOCKED, } err := handler.HandleParticipant(cp) So(err, ShouldBeNil) Convey("should be removed from default channels", func() { isParticipant, err := defaultChannel1.IsParticipant(acc1.Id) So(err, ShouldBeNil) So(isParticipant, ShouldBeFalse) isParticipant, err = defaultChannel2.IsParticipant(acc1.Id) So(err, ShouldBeNil) So(isParticipant, ShouldBeFalse) }) }) }) Convey("should success if default channels is not available anymore", func() { // delete the channel So(defaultChannel1.Delete(), ShouldBeNil) cp := &models.ChannelParticipant{ AccountId: acc1.Id, ChannelId: groupChannel.Id, StatusConstant: models.ChannelParticipant_STATUS_ACTIVE, } err := handler.HandleParticipant(cp) So(err, ShouldBeNil) }) }) }
func TestGroupChannel(t *testing.T) { tests.WithRunner(t, func(r *runner.Runner) { Convey("while testing pinned activity channel", t, func() { groupName := models.RandomGroupName() account, err := models.CreateAccountInBothDbs() So(err, ShouldBeNil) ses, err := modelhelper.FetchOrCreateSession(account.Nick, groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) models.CreateTypedGroupedChannelWithTest( account.Id, models.Channel_TYPE_GROUP, groupName, ) Convey("channel should be there", func() { channel1, err := rest.CreateChannelByGroupNameAndType( account.Id, groupName, models.Channel_TYPE_GROUP, ses.ClientId, ) So(err, ShouldBeNil) So(channel1, ShouldNotBeNil) channel2, err := rest.CreateChannelByGroupNameAndType( account.Id, groupName, models.Channel_TYPE_GROUP, ses.ClientId, ) So(err, ShouldBeNil) So(channel2, ShouldNotBeNil) }) Convey("group channel should be shown before announcement", func() { account, err := models.CreateAccountInBothDbs() So(err, ShouldBeNil) ses, err := modelhelper.FetchOrCreateSession(account.Nick, groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) _, err = rest.CreateChannelByGroupNameAndType( account.Id, groupName, models.Channel_TYPE_GROUP, ses.ClientId, ) So(err, ShouldBeNil) _, err = rest.CreateChannelByGroupNameAndType( account.Id, groupName, models.Channel_TYPE_ANNOUNCEMENT, ses.ClientId, ) So(err, ShouldBeNil) channels, err := rest.FetchChannelsByQuery(account.Id, &request.Query{ GroupName: groupName, Type: models.Channel_TYPE_GROUP, }, ses.ClientId, ) So(err, ShouldBeNil) So(len(channels), ShouldEqual, 2) So(channels[0].TypeConstant, ShouldEqual, models.Channel_TYPE_GROUP) So(channels[1].TypeConstant, ShouldEqual, models.Channel_TYPE_ANNOUNCEMENT) }) Convey("owner should be able to update it", func() { channel1, err := rest.CreateChannelByGroupNameAndType( account.Id, groupName, models.Channel_TYPE_GROUP, ses.ClientId, ) So(err, ShouldBeNil) So(channel1, ShouldNotBeNil) // fetching channel returns creator id _, err = rest.UpdateChannel(channel1, ses.ClientId) So(err, ShouldBeNil) }) Convey("owner should only be able to update name and purpose of the channel", nil) Convey("normal user should be able to update it if and only if user is creator or participant", func() { channel1, err := rest.CreateChannelByGroupNameAndType( account.Id, groupName, models.Channel_TYPE_GROUP, ses.ClientId, ) So(err, ShouldBeNil) So(channel1, ShouldNotBeNil) anotherAccount := models.NewAccount() anotherAccount.OldId = bson.NewObjectId().Hex() anotherAccount, err = rest.CreateAccount(anotherAccount) So(err, ShouldBeNil) So(account, ShouldNotBeNil) ses, err := modelhelper.FetchOrCreateSession(anotherAccount.Nick, groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) _, err = rest.UpdateChannel(channel1, ses.ClientId) So(err, ShouldBeNil) }) Convey("owner cant delete it", func() { channel1, err := rest.CreateChannelByGroupNameAndType( account.Id, groupName, models.Channel_TYPE_GROUP, ses.ClientId, ) So(err, ShouldBeNil) So(channel1, ShouldNotBeNil) err = rest.DeleteChannel(account.Id, channel1.Id) So(err, ShouldNotBeNil) }) Convey("normal user cant delete it", func() { channel1, err := rest.CreateChannelByGroupNameAndType( account.Id, groupName, models.Channel_TYPE_GROUP, ses.ClientId, ) So(err, ShouldBeNil) So(channel1, ShouldNotBeNil) err = rest.DeleteChannel(rand.Int63(), channel1.Id) So(err, ShouldNotBeNil) }) Convey("member can post status update", nil) Convey("non-member can not post status update", nil) }) }) }
func TestAccountUpdated(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, err := models.CreateAccountInBothDbs() So(acc, ShouldNotBeNil) So(err, ShouldBeNil) Convey("it should save the document to algolia if not created before", func() { err := handler.AccountUpdated(acc) So(err, ShouldBeNil) Convey("it should have email in it", func() { // make sure account is there So(doBasicTestForAccount(handler, acc.OldId), ShouldBeNil) // update user's email selector := bson.M{"username": acc.Nick} newEmail := models.RandomGroupName() + "@bar.com" updateQuery := bson.M{"email": newEmail} err = modelhelper.UpdateUser(selector, updateQuery) So(err, ShouldBeNil) err = handler.AccountUpdated(acc) So(err, ShouldBeNil) err = makeSureWithSearch( handler, IndexAccounts, newEmail, map[string]interface{}{"restrictSearchableAttributes": "email"}, func(record map[string]interface{}, err error) bool { if err != nil { return false } if record == nil { return false } hits, ok := record["nbHits"] if hits == nil || !ok { return false } if hits.(float64) <= 0 { return false } return true }) So(err, ShouldBeNil) }) }) Convey("updating the account again should success", func() { err := handler.AccountCreated(acc) So(err, ShouldBeNil) So(doBasicTestForAccount(handler, acc.OldId), ShouldBeNil) }) Convey("it should delete the document when user account is deleted", func() { // first ensure account object is created err = handler.AccountCreated(acc) So(err, ShouldBeNil) So(doBasicTestForAccount(handler, acc.OldId), ShouldBeNil) newNick := "guest-" + models.RandomName() + "-rm" err = models.UpdateUsernameInBothDbs(acc.Nick, newNick) So(err, ShouldBeNil) acc.Nick = newNick err = handler.AccountUpdated(acc) So(err, ShouldBeNil) So(doBasicTestForAccountDeletion(handler, acc.OldId), ShouldBeNil) Convey("deleting the account again should success", func() { err = handler.AccountUpdated(acc) So(err, ShouldBeNil) So(doBasicTestForAccountDeletion(handler, acc.OldId), ShouldBeNil) }) }) Convey("it should not return any error when deleted user does not exist on Algolia", func() { newNick := "guest-" + models.RandomName() + "-rm" err = models.UpdateUsernameInBothDbs(acc.Nick, newNick) So(err, ShouldBeNil) acc.Nick = newNick err = handler.AccountUpdated(acc) So(err, ShouldBeNil) So(doBasicTestForAccountDeletion(handler, acc.OldId), ShouldBeNil) }) }) }
func TestAccountSaved(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, err := models.CreateAccountInBothDbs() So(err, ShouldBeNil) So(acc, ShouldNotBeNil) Convey("it should save the document to algolia", func() { err := handler.AccountCreated(acc) So(err, ShouldBeNil) Convey("it should have email in it", func() { // make sure account is there So(doBasicTestForAccount(handler, acc.OldId), ShouldBeNil) user, err := modelhelper.GetUser(acc.Nick) So(err, ShouldBeNil) err = makeSureWithSearch( handler, IndexAccounts, user.Email, map[string]interface{}{"restrictSearchableAttributes": "email"}, func(record map[string]interface{}, err error) bool { if err != nil { return false } if record == nil { return false } hits, ok := record["nbHits"] if hits == nil || !ok { return false } if hits.(float64) <= 0 { return false } return true }, ) So(err, ShouldBeNil) }) }) Convey("saving same account to algolia should success", func() { err := handler.AccountCreated(acc) So(err, ShouldBeNil) So(doBasicTestForAccount(handler, acc.OldId), ShouldBeNil) }) }) }
func TestCollaborationOperationsUnshareVM(t *testing.T) { r := runner.New("collaboration-UnshareVM-tests") err := r.Init() if err != nil { panic(err) } defer r.Close() appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() // init with defaults mongoCache := cache.NewMongoCacheWithTTL(modelhelper.Mongo.Session) defer mongoCache.StopGC() handler := New(r.Log, mongoCache, appConfig, r.Kite) Convey("while testing UnshareVM", t, func() { Convey("should be able to create the channel and workspace first", func() { Convey("should be able to UnshareVM", func() { creator, err := socialapimodels.CreateAccountInBothDbs() // init account So(err, ShouldBeNil) participant1, err := socialapimodels.CreateAccountInBothDbs() So(err, ShouldBeNil) participant2, err := socialapimodels.CreateAccountInBothDbs() So(err, ShouldBeNil) m1, m1ws1 := prepareSingleWorkspace(creator, participant1, participant2) channelId, err := strconv.ParseInt(m1ws1.ChannelId, 10, 64) So(err, ShouldBeNil) req1 := &models.Ping{ AccountId: creator.Id, FileId: fmt.Sprintf("%d", rand.Int63()), ChannelId: channelId, } toBeRemovedUsers, err := handler.findToBeRemovedUsers(req1) So(err, ShouldBeNil) So(toBeRemovedUsers, ShouldNotBeNil) err = handler.UnshareVM(req1, toBeRemovedUsers) So(err, ShouldBeNil) err = handler.EndPrivateMessage(req1) So(err, ShouldBeNil) Convey("remove users should not be in the machine", func() { mm1, err := modelhelper.GetMachineByUid(m1.Uid) So(err, ShouldBeNil) So(mm1, ShouldNotBeNil) So(len(mm1.Users), ShouldEqual, 1) ownerUser, err := modelhelper.GetUserByAccountId(creator.OldId) So(err, ShouldBeNil) So(mm1.Users[0].Id.Hex(), ShouldEqual, ownerUser.ObjectId.Hex()) }) }) Convey("if participant and owner shares multiple workspaces", func() { creator, err := socialapimodels.CreateAccountInBothDbs() // init account So(err, ShouldBeNil) participant1, err := socialapimodels.CreateAccountInBothDbs() So(err, ShouldBeNil) participant2, err := socialapimodels.CreateAccountInBothDbs() So(err, ShouldBeNil) participant3, err := socialapimodels.CreateAccountInBothDbs() So(err, ShouldBeNil) _, _, m2, m2ws1, m2ws2 := prepareWorkspace(creator, participant1, participant2, participant3) Convey("remove from first workspace", func() { channelId, err := strconv.ParseInt(m2ws1.ChannelId, 10, 64) So(err, ShouldBeNil) req := &models.Ping{ AccountId: creator.Id, FileId: fmt.Sprintf("%d", rand.Int63()), ChannelId: channelId, } toBeRemovedUsers, err := handler.findToBeRemovedUsers(req) So(err, ShouldBeNil) So(toBeRemovedUsers, ShouldNotBeNil) err = handler.UnshareVM(req, toBeRemovedUsers) So(err, ShouldBeNil) err = handler.EndPrivateMessage(req) So(err, ShouldBeNil) Convey("participants should still be in the second machine", func() { mm2, err := modelhelper.GetMachineByUid(m2.Uid) So(err, ShouldBeNil) So(mm2, ShouldNotBeNil) So(len(mm2.Users), ShouldEqual, 3) // participant1 is not in the second WS, so it should be removed from the machine ownerUser, err := modelhelper.GetUserByAccountId(creator.OldId) So(err, ShouldBeNil) So(mm2.Users[0].Id.Hex(), ShouldEqual, ownerUser.ObjectId.Hex()) participant2User, err := modelhelper.GetUserByAccountId(participant2.OldId) So(err, ShouldBeNil) So(mm2.Users[1].Id.Hex(), ShouldEqual, participant2User.ObjectId.Hex()) participant3User, err := modelhelper.GetUserByAccountId(participant3.OldId) So(err, ShouldBeNil) So(mm2.Users[2].Id.Hex(), ShouldEqual, participant3User.ObjectId.Hex()) Convey("after removing from second WS", func() { // remove from second WS too channelId, err := strconv.ParseInt(m2ws2.ChannelId, 10, 64) So(err, ShouldBeNil) req := &models.Ping{ AccountId: creator.Id, FileId: fmt.Sprintf("%d", rand.Int63()), ChannelId: channelId, } toBeRemovedUsers, err := handler.findToBeRemovedUsers(req) So(err, ShouldBeNil) So(toBeRemovedUsers, ShouldNotBeNil) err = handler.UnshareVM(req, toBeRemovedUsers) So(err, ShouldBeNil) err = handler.EndPrivateMessage(req) So(err, ShouldBeNil) Convey("owner and permanent should still stay", func() { mm2, err := modelhelper.GetMachineByUid(m2.Uid) So(err, ShouldBeNil) So(mm2, ShouldNotBeNil) So(len(mm2.Users), ShouldEqual, 2) ownerUser, err := modelhelper.GetUserByAccountId(creator.OldId) So(err, ShouldBeNil) So(mm2.Users[0].Id.Hex(), ShouldEqual, ownerUser.ObjectId.Hex()) participant1User, err := modelhelper.GetUserByAccountId(participant3.OldId) So(err, ShouldBeNil) So(mm2.Users[1].Id.Hex(), ShouldEqual, participant1User.ObjectId.Hex()) }) }) }) }) }) }) }) }
func TestChannelUpdatedCalculateUnreadItemCount(t *testing.T) { r := runner.New("test") if err := r.Init(); err != nil { t.Fatalf("couldnt start bongo %s", err.Error()) } defer r.Close() appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() groupName := models.RandomGroupName() Convey("while testing get account", t, func() { Convey("if cookie is not set, should return nil", func() { a := getAccount(&http.Request{}, models.Channel_KODING_NAME) So(a, ShouldNotBeNil) So(a.Id, ShouldBeZeroValue) }) Convey("if cookie value is not set, should return nil", func() { req, _ := http.NewRequest("GET", "/", nil) expire := time.Now().AddDate(0, 0, 1) cookie := http.Cookie{ Name: "clientId", Value: "", Path: "/", Domain: "localhost", Expires: expire, } req.AddCookie(&cookie) a := getAccount(req, models.Channel_KODING_NAME) So(a, ShouldNotBeNil) So(a.Id, ShouldBeZeroValue) }) Convey("if session doesnt have username, should return nil", func() { ses, err := modelhelper.CreateSessionForAccount("", groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) req, _ := http.NewRequest("GET", "/", nil) expire := time.Now().AddDate(0, 0, 1) cookie := http.Cookie{ Name: "clientId", Value: ses.ClientId, Path: "/", Domain: "localhost", Expires: expire, } req.AddCookie(&cookie) a := getAccount(req, models.Channel_KODING_NAME) So(a, ShouldNotBeNil) So(a.Id, ShouldBeZeroValue) }) Convey("if session is valid, should return account", func() { acc, err := models.CreateAccountInBothDbs() So(err, ShouldBeNil) So(acc, ShouldNotBeNil) ses, err := modelhelper.CreateSessionForAccount(acc.Nick, groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) req, _ := http.NewRequest("GET", "/", nil) expire := time.Now().AddDate(0, 0, 1) cookie := http.Cookie{ Name: "clientId", Value: ses.ClientId, Path: "/", Domain: "localhost", Expires: expire, } req.AddCookie(&cookie) res := getAccount(req, models.Channel_KODING_NAME) So(res, ShouldNotBeNil) So(acc.Id, ShouldEqual, res.Id) }) }) Convey("while making sure account", t, func() { Convey("if account is not in postgres", func() { nick := models.RandomName() oldAcc := &kodingmodels.Account{ Id: bson.NewObjectId(), Profile: kodingmodels.AccountProfile{ Nickname: nick, }, } err := modelhelper.CreateAccount(oldAcc) So(err, ShouldBeNil) oldUser := &kodingmodels.User{ ObjectId: bson.NewObjectId(), Password: nick, Salt: nick, Name: nick, Email: nick + "@koding.com", EmailFrequency: &kodingmodels.EmailFrequency{}, } err = modelhelper.CreateUser(oldUser) So(err, ShouldBeNil) groupName := models.RandomGroupName() _, err = makeSureAccount(groupName, nick) So(err, ShouldBeNil) Convey("should create it in postgres", func() { a := models.NewAccount() err = a.ByNick(nick) So(err, ShouldBeNil) So(a.OldId, ShouldEqual, oldAcc.Id.Hex()) Convey("should set socialAPI id in mongo", func() { oldAccFromDB, err := modelhelper.GetAccount(nick) So(err, ShouldBeNil) So(oldAccFromDB.SocialApiId, ShouldEqual, strconv.FormatInt(a.Id, 10)) }) }) }) Convey("if account is in postgres", func() { acc, err := models.CreateAccountInBothDbs() So(err, ShouldBeNil) So(acc, ShouldNotBeNil) groupName := models.RandomGroupName() _, err = makeSureAccount(groupName, acc.Nick) So(err, ShouldBeNil) Convey("should be in postgres", func() { a := models.NewAccount() err = a.ByNick(acc.Nick) So(err, ShouldBeNil) So(a.OldId, ShouldEqual, acc.OldId) Convey("should have socialAPI set", func() { oldAccFromDB, err := modelhelper.GetAccount(acc.Nick) So(err, ShouldBeNil) So(oldAccFromDB.SocialApiId, ShouldEqual, strconv.FormatInt(a.Id, 10)) }) }) }) }) Convey("while making sure group membership", t, func() { Convey("if account is not not a member", func() { account := models.CreateAccountWithTest() requester := models.CreateAccountWithTest() groupChannel := models.CreateTypedPublicChannelWithTest(account.Id, models.Channel_TYPE_GROUP) err := makeSureMembership(groupChannel, requester.Id) So(err, ShouldBeNil) Convey("should add as participant", func() { status, err := groupChannel.IsParticipant(requester.Id) So(err, ShouldBeNil) So(status, ShouldBeTrue) Convey("if account is a member", func() { err := makeSureMembership(groupChannel, requester.Id) So(err, ShouldBeNil) Convey("should be a participant", func() { status, err := groupChannel.IsParticipant(requester.Id) So(err, ShouldBeNil) So(status, ShouldBeTrue) }) }) }) }) }) }
func TestPersist(t *testing.T) { r := runner.New("test") if err := r.Init(); err != nil { t.Fatalf("couldnt start bongo %s", err.Error()) } defer r.Close() // init mongo connection appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() Convey("while testing Persist", t, func() { Convey("testing post message while all is well", func() { acc, err := socialapimodels.CreateAccountInBothDbs() So(err, ShouldBeNil) c := socialapimodels.CreateChannelWithTest(acc.Id) //cm := socialapimodels.CreateMessage(c.Id, acc.Id) mongoUser, err := modelhelper.GetUser(acc.Nick) So(err, ShouldBeNil) m := &Mail{ From: mongoUser.Email, OriginalRecipient: fmt.Sprintf("*****@*****.**", c.Id), MailboxHash: fmt.Sprintf("channelid.%d", c.Id), TextBody: "Its an example of text message", } err = m.Persist() So(err, ShouldBeNil) }) Convey("reply should have messageid", func() { acc, err := socialapimodels.CreateAccountInBothDbs() So(err, ShouldBeNil) c := socialapimodels.CreateChannelWithTest(acc.Id) socialapimodels.AddParticipantsWithTest(c.Id, acc.Id) mongoUser, err := modelhelper.GetUser(acc.Nick) So(err, ShouldBeNil) m := &Mail{ From: mongoUser.Email, OriginalRecipient: fmt.Sprintf("*****@*****.**", c.Id), MailboxHash: fmt.Sprintf("channelid.%d", c.Id), TextBody: "Its an example of text message", } err = m.Persist() So(err, ShouldEqual, bongo.RecordNotFound) }) Convey("testing reply message if all is well", func() { acc, err := socialapimodels.CreateAccountInBothDbs() So(err, ShouldBeNil) c := socialapimodels.CreateChannelWithTest(acc.Id) socialapimodels.AddParticipantsWithTest(c.Id, acc.Id) cm := socialapimodels.CreateMessage(c.Id, acc.Id, socialapimodels.ChannelMessage_TYPE_POST) So(cm, ShouldNotBeNil) mongoUser, err := modelhelper.GetUser(acc.Nick) So(err, ShouldBeNil) m := &Mail{ From: mongoUser.Email, OriginalRecipient: fmt.Sprintf("*****@*****.**", c.Id), MailboxHash: fmt.Sprintf("messageid.%d", cm.Id), TextBody: "Its an example of text message", StrippedTextReply: "This one is reply message", } err = m.Persist() So(err, ShouldBeNil) }) Convey("testing reply message, record not found if user is not a participant", func() { acc, err := socialapimodels.CreateAccountInBothDbs() So(err, ShouldBeNil) c := socialapimodels.CreateChannelWithTest(acc.Id) cm := socialapimodels.CreateMessage(c.Id, acc.Id, socialapimodels.ChannelMessage_TYPE_POST) So(cm, ShouldNotBeNil) mongoUser, err := modelhelper.GetUser(acc.Nick) So(err, ShouldBeNil) m := &Mail{ From: mongoUser.Email, OriginalRecipient: fmt.Sprintf("*****@*****.**", c.Id), MailboxHash: fmt.Sprintf("messageid.%d", cm.Id), TextBody: "Its an example of text message", StrippedTextReply: "This one is reply message", } err = m.persistPost(acc.Id) So(err, ShouldNotBeNil) }) }) }
func TestPrivateMesssages(t *testing.T) { tests.WithRunner(t, func(r *runner.Runner) { Convey("while testing private channels", t, func() { devrim, err := models.CreateAccountInBothDbsWithNick("devrim") So(err, ShouldBeNil) So(devrim, ShouldNotBeNil) sinan, err := models.CreateAccountInBothDbsWithNick("sinan") So(err, ShouldBeNil) So(sinan, ShouldNotBeNil) groupName := models.RandomGroupName() // cretae admin user account, err := models.CreateAccountInBothDbs() tests.ResultedWithNoErrorCheck(account, err) models.CreateTypedGroupedChannelWithTest( account.Id, models.Channel_TYPE_GROUP, groupName, ) // fetch admin's session ses, err := modelhelper.FetchOrCreateSession(account.Nick, groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) groupChannel, err := rest.CreateChannelByGroupNameAndType( account.Id, groupName, models.Channel_TYPE_GROUP, ses.ClientId, ) tests.ResultedWithNoErrorCheck(groupChannel, err) recipient := models.NewAccount() recipient.OldId = AccountOldId2.Hex() recipient, err = rest.CreateAccount(recipient) So(err, ShouldBeNil) So(recipient, ShouldNotBeNil) recipient2 := models.NewAccount() recipient2.OldId = AccountOldId3.Hex() recipient2, err = rest.CreateAccount(recipient2) So(err, ShouldBeNil) So(recipient2, ShouldNotBeNil) Convey("participants should be a member of parent group", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "this is a body message for private message @devrim @sinan" pmr.GroupName = groupName pmr.Recipients = []string{"devrim", "sinan"} _, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldNotBeNil) }) _, err = groupChannel.AddParticipant(sinan.Id) So(err, ShouldBeNil) _, err = groupChannel.AddParticipant(devrim.Id) So(err, ShouldBeNil) Convey("one can send private message to one person", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "this is a body message for private message @devrim @sinan" pmr.GroupName = groupName pmr.Recipients = []string{"devrim", "sinan"} cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) }) Convey("0 recipient should not fail", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "this is a body for private message" pmr.GroupName = groupName pmr.Recipients = []string{} cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) }) Convey("if sender is not defined but token is added, then shouldn't fail to create PM", func() { pmr := models.ChannelRequest{} pmr.AccountId = 0 pmr.Body = "this is a body for private message" pmr.GroupName = "" pmr.Recipients = []string{} cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) }) Convey("one can send private message to multiple person", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "this is a body for private message @sinan" pmr.GroupName = groupName pmr.Recipients = []string{"sinan"} cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) }) Convey("private message response should have created channel", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "this is a body for private message @devrim @sinan" pmr.GroupName = groupName pmr.Recipients = []string{"devrim", "sinan"} cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) So(cmc.Channel.TypeConstant, ShouldEqual, models.Channel_TYPE_PRIVATE_MESSAGE) So(cmc.Channel.Id, ShouldBeGreaterThan, 0) So(cmc.Channel.GroupName, ShouldEqual, groupName) So(cmc.Channel.PrivacyConstant, ShouldEqual, models.Channel_PRIVACY_PRIVATE) }) Convey("private message response should have participant status data", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "this is a body for private message @devrim @sinan" pmr.GroupName = groupName pmr.Recipients = []string{"devrim", "sinan"} cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) So(cmc.IsParticipant, ShouldBeTrue) }) Convey("private message response should have participant count", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "this is a body for @sinan private message @devrim" pmr.GroupName = groupName pmr.Recipients = []string{"devrim", "sinan"} cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) So(cmc.ParticipantCount, ShouldEqual, 3) }) Convey("private message response should have participant preview", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "this is @sinan a body for @devrim private message" pmr.GroupName = groupName pmr.Recipients = []string{"sinan", "devrim"} cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) So(len(cmc.ParticipantsPreview), ShouldEqual, 3) }) Convey("private message response should have last Message", func() { body := "hi @devrim this is a body for private message also for @sinan" pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = body pmr.GroupName = groupName pmr.Recipients = []string{"sinan", "devrim"} cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) So(cmc.LastMessage.Message.Body, ShouldEqual, body) }) Convey("private message should be listed by all recipients", func() { body := "hi @devrim this is a body for private message also for @sinan" pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = body pmr.GroupName = groupName pmr.Recipients = []string{"sinan", "devrim"} cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cmc, ShouldNotBeNil) pm, err := rest.GetPrivateChannels(&request.Query{AccountId: account.Id, GroupName: groupName}, ses.ClientId) So(err, ShouldBeNil) So(pm, ShouldNotBeNil) So(len(pm), ShouldNotEqual, 0) So(pm[0], ShouldNotBeNil) So(pm[0].Channel.TypeConstant, ShouldEqual, models.Channel_TYPE_PRIVATE_MESSAGE) So(pm[0].Channel.Id, ShouldEqual, cmc.Channel.Id) So(pm[0].Channel.GroupName, ShouldEqual, cmc.Channel.GroupName) So(pm[0].LastMessage.Message.Body, ShouldEqual, cmc.LastMessage.Message.Body) So(pm[0].Channel.PrivacyConstant, ShouldEqual, models.Channel_PRIVACY_PRIVATE) So(len(pm[0].ParticipantsPreview), ShouldEqual, 3) So(pm[0].IsParticipant, ShouldBeTrue) }) Convey("user should be able to search private messages via purpose field", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "search private messages" pmr.GroupName = groupName pmr.Recipients = []string{"sinan", "devrim"} pmr.Purpose = "test me up" cmc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) query := request.Query{AccountId: account.Id, GroupName: groupName} _, err = rest.SearchPrivateChannels(&query, ses.ClientId) So(err, ShouldNotBeNil) query.Name = "test" pm, err := rest.SearchPrivateChannels(&query, ses.ClientId) So(err, ShouldBeNil) So(pm, ShouldNotBeNil) So(len(pm), ShouldNotEqual, 0) So(pm[0], ShouldNotBeNil) So(pm[0].Channel.TypeConstant, ShouldEqual, models.Channel_TYPE_PRIVATE_MESSAGE) So(pm[0].Channel.Id, ShouldEqual, cmc.Channel.Id) So(pm[0].Channel.GroupName, ShouldEqual, cmc.Channel.GroupName) So(pm[0].LastMessage.Message.Body, ShouldEqual, cmc.LastMessage.Message.Body) So(pm[0].Channel.PrivacyConstant, ShouldEqual, models.Channel_PRIVACY_PRIVATE) So(pm[0].IsParticipant, ShouldBeTrue) }) payload := gorm.Hstore{} bar := "bar" payload["foo"] = &bar Convey("given payload should be populated", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "this is a body message for private message @devrim @sinan" pmr.GroupName = groupName pmr.Recipients = []string{"devrim", "sinan"} pmr.Payload = payload pcr, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(pcr, ShouldNotBeNil) So(pcr.Channel, ShouldNotBeNil) So(pcr.Channel.Payload, ShouldNotBeNil) So(len(pcr.Channel.Payload), ShouldBeGreaterThan, 0) So(*pcr.Channel.Payload["foo"], ShouldEqual, bar) }) Convey("user join activity should be listed by recipients", func() { groupName := models.RandomGroupName() // cretae admin user account, err := models.CreateAccountInBothDbs() tests.ResultedWithNoErrorCheck(account, err) // fetch admin's session ses, err := modelhelper.FetchOrCreateSession(account.Nick, groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) groupChannel := models.CreateTypedGroupedChannelWithTest( account.Id, models.Channel_TYPE_GROUP, groupName, ) So(groupChannel, ShouldNotBeNil) _, err = groupChannel.AddParticipant(account.Id) So(err, ShouldBeNil) _, err = groupChannel.AddParticipant(devrim.Id) So(err, ShouldBeNil) _, err = groupChannel.AddParticipant(sinan.Id) So(err, ShouldBeNil) _, err = groupChannel.AddParticipant(recipient.Id) So(err, ShouldBeNil) pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "test private message participants" pmr.GroupName = groupName pmr.Recipients = []string{"sinan", "devrim"} if pmr.Payload == nil { pmr.Payload = gorm.Hstore{} } pic := "pictureSomethingLikeThat" pmr.Payload["link_embed"] = &pic cc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cc, ShouldNotBeNil) history, err := rest.GetHistory( cc.Channel.Id, &request.Query{ AccountId: account.Id, }, ses.ClientId, ) So(err, ShouldBeNil) So(history, ShouldNotBeNil) So(len(history.MessageList), ShouldEqual, 2) // add participant _, err = rest.AddChannelParticipant(cc.Channel.Id, ses.ClientId, recipient.Id) So(err, ShouldBeNil) history, err = rest.GetHistory( cc.Channel.Id, &request.Query{ AccountId: account.Id, }, ses.ClientId, ) So(err, ShouldBeNil) So(history, ShouldNotBeNil) So(len(history.MessageList), ShouldEqual, 3) So(history.MessageList[0].Message, ShouldNotBeNil) So(history.MessageList[0].Message.TypeConstant, ShouldEqual, models.ChannelMessage_TYPE_SYSTEM) So(history.MessageList[0].Message.Payload, ShouldNotBeNil) addedBy, ok := history.MessageList[0].Message.Payload["addedBy"] So(ok, ShouldBeTrue) So(*addedBy, ShouldEqual, account.Nick) systemType, ok := history.MessageList[0].Message.Payload["systemType"] So(ok, ShouldBeTrue) So(*systemType, ShouldEqual, models.ChannelRequestMessage_TYPE_JOIN) // we set link_embed into the payloadof the system message above. // But system message payload data will not have payload defined // by client side or else // As a result, system message will not be able to have link_embed payload data _, k := history.MessageList[0].Message.Payload["link_embed"] So(k, ShouldBeFalse) // try to add same participant _, err = rest.AddChannelParticipant(cc.Channel.Id, ses.ClientId, recipient.Id) So(err, ShouldBeNil) history, err = rest.GetHistory( cc.Channel.Id, &request.Query{ AccountId: account.Id, }, ses.ClientId, ) So(err, ShouldBeNil) So(history, ShouldNotBeNil) So(len(history.MessageList), ShouldEqual, 3) }) Convey("user should not be able to edit join messages", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "test private message participants again" pmr.GroupName = groupName pmr.Recipients = []string{"devrim"} cc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cc, ShouldNotBeNil) _, err = rest.AddChannelParticipant(cc.Channel.Id, ses.ClientId, recipient.Id) So(err, ShouldBeNil) ses, err := modelhelper.FetchOrCreateSession(account.Nick, groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) history, err := rest.GetHistory( cc.Channel.Id, &request.Query{ AccountId: account.Id, }, ses.ClientId, ) So(err, ShouldBeNil) So(history, ShouldNotBeNil) So(len(history.MessageList), ShouldEqual, 3) joinMessage := history.MessageList[0].Message So(joinMessage, ShouldNotBeNil) _, err = rest.UpdatePost(joinMessage, ses.ClientId) So(err, ShouldNotBeNil) }) Convey("first chat message should include initial participants", func() { pmr := models.ChannelRequest{} pmr.AccountId = account.Id pmr.Body = "test initial participation message" pmr.GroupName = groupName pmr.Recipients = []string{"sinan", "devrim"} cc, err := rest.SendPrivateChannelRequest(pmr, ses.ClientId) So(err, ShouldBeNil) So(cc, ShouldNotBeNil) ses, err := modelhelper.FetchOrCreateSession(account.Nick, groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) history, err := rest.GetHistory( cc.Channel.Id, &request.Query{ AccountId: account.Id, }, ses.ClientId, ) So(err, ShouldBeNil) So(history, ShouldNotBeNil) So(len(history.MessageList), ShouldEqual, 2) joinMessage := history.MessageList[1].Message So(joinMessage.TypeConstant, ShouldEqual, models.ChannelMessage_TYPE_SYSTEM) So(joinMessage.Payload, ShouldNotBeNil) initialParticipants, ok := joinMessage.Payload["initialParticipants"] So(ok, ShouldBeTrue) systemType, ok := history.MessageList[1].Message.Payload["systemType"] So(ok, ShouldBeTrue) So(*systemType, ShouldEqual, models.ChannelRequestMessage_TYPE_INIT) participants := make([]string, 0) err = json.Unmarshal([]byte(*initialParticipants), &participants) So(err, ShouldBeNil) So(len(participants), ShouldEqual, 2) So(participants, ShouldContain, "devrim") // So(*addedBy, ShouldEqual, account.OldId) }) Convey("targetted account should be able to list private message channel of himself", nil) }) }) }