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

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

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

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

			Convey("it should list latest posts when there is no time interval in query", func() {
				cmc, err := rest.FetchAccountActivities(account.Id, ses.ClientId)
				So(err, ShouldBeNil)
				So(len(cmc), ShouldEqual, 1)
				So(cmc[0].Message.Body, ShouldEqual, post.Body)
			})
		})
	})
}
Esempio n. 2
0
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)
			})
		})
	})
}
Esempio n. 3
0
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)
			})
		})
	})
}
Esempio n. 4
0
func TestChannelHistory(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		Convey("While testing history of a channel", t, func() {
			Convey("We should be able to create it(channel) first", func() {
				groupName := models.RandomGroupName()

				account, err := models.CreateAccountInBothDbsWithNick("sinan")
				So(err, ShouldBeNil)
				So(account, ShouldNotBeNil)

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

				channel := models.CreateTypedGroupedChannelWithTest(
					account.Id,
					models.Channel_TYPE_GROUP,
					groupName,
				)
				_, err = channel.AddParticipant(account.Id)
				So(err, ShouldBeNil)

				Convey("While posting a new message to it", func() {
					var channelParticipant *models.ChannelParticipant
					var err error
					Convey("We should be able to create a participant first", func() {
						channelParticipant, err = rest.CreateChannelParticipant(channel.Id, ses.ClientId)
						So(err, ShouldBeNil)
						So(channelParticipant, ShouldNotBeNil)

						Convey("Create posts with created participant", func() {
							channel := channel
							for i := 0; i < 10; i++ {
								post, err := rest.CreatePost(channel.Id, ses.ClientId)
								So(err, ShouldBeNil)
								So(post, ShouldNotBeNil)
								So(post.Id, ShouldNotEqual, 0)
								So(post.Body, ShouldNotEqual, "")

							}
							Convey("We should be able to fetch the history", func() {
								history, err := rest.GetHistory(
									channel.Id,
									&request.Query{
										AccountId: account.Id,
									},
									ses.ClientId,
								)
								So(err, ShouldBeNil)
								So(history, ShouldNotBeNil)
								So(len(history.MessageList), ShouldEqual, 10)
							})

							Convey("We should be not able to fetch the history if the clientId is not set", func() {
								history, err := rest.GetHistory(
									channel.Id,
									&request.Query{
										AccountId: account.Id,
									},
									"",
								)
								So(err, ShouldNotBeNil)
								So(history, ShouldBeNil)
							})

							Convey("We should be not able to fetch the history if the clientId doesnt exist", func() {

								history, err := rest.GetHistory(
									channel.Id,
									&request.Query{
										AccountId: account.Id,
									},
									"foobarzaa",
								)

								So(err, ShouldNotBeNil)
								So(history, ShouldBeNil)
							})

							Convey("We should be able to get channel message count", func() {
								count, err := rest.CountHistory(channel.Id)
								So(err, ShouldBeNil)
								So(count, ShouldNotBeNil)
								So(count.TotalCount, ShouldEqual, 10)
							})
						})
						Convey("We should be able to check history according to request", func() {
							channel := channel
							for i := 0; i < 5; i++ {
								body := fmt.Sprintf("body%d", i)
								post, err := rest.CreatePostWithBodyAndAuth(channel.Id, body, ses.ClientId)
								// we need to wait while posting messages
								// if we dont use time sleep, Added_at field of the messages is
								// gonna be equal and this behavior is not expected situation
								// Then, tests will not be worked correctly
								time.Sleep(1000 * time.Millisecond)
								So(err, ShouldBeNil)
								So(post, ShouldNotBeNil)
								So(post.Id, ShouldNotEqual, 0)
								So(post.Body, ShouldNotEqual, "")
							}
							bodyMes := "postMid message"
							postMid, err := rest.CreatePostWithBodyAndAuth(channel.Id, bodyMes, ses.ClientId)
							So(postMid, ShouldNotBeNil)
							So(err, ShouldBeNil)
							for i := 5; i < 10; i++ {
								time.Sleep(1000 * time.Millisecond)
								body := fmt.Sprintf("body%d", i)
								post, err := rest.CreatePostWithBodyAndAuth(channel.Id, body, ses.ClientId)
								So(err, ShouldBeNil)
								So(post, ShouldNotBeNil)
								So(post.Id, ShouldNotEqual, 0)
								So(post.Body, ShouldNotEqual, "")
							}
							Convey("We should able to fetch the history with query request ", func() {
								history, err := rest.GetHistory(
									channel.Id,
									&request.Query{
										AccountId: account.Id,
									},
									ses.ClientId,
								)

								So(err, ShouldBeNil)
								So(history, ShouldNotBeNil)
								So(len(history.MessageList), ShouldEqual, 11)
							})

							Convey("We should able to fetch the history with query ADDED AT & ASC ", func() {
								history, err := rest.GetHistory(
									channel.Id,
									&request.Query{
										From:      postMid.CreatedAt,
										SortOrder: "ASC",
									},
									ses.ClientId,
								)

								var historyArr []string
								arr := []string{"postMid message", "body5", "body6", "body7", "body8", "body9"}
								for _, x := range history.MessageList {
									historyArr = append(historyArr, x.Message.Body)
								}
								So(err, ShouldBeNil)
								So(history, ShouldNotBeNil)
								So(len(history.MessageList), ShouldEqual, 6)
								So(arr, ShouldResemble, historyArr)
							})

							Convey("We should able to fetch the history with query ADDED AT & DESC ", func() {
								history, err := rest.GetHistory(
									channel.Id,
									&request.Query{
										From:      postMid.CreatedAt,
										SortOrder: "DESC",
									},
									ses.ClientId,
								)

								var historyArr []string
								arr := []string{"body4", "body3", "body2", "body1", "body0"}
								for _, x := range history.MessageList {
									historyArr = append(historyArr, x.Message.Body)
								}
								So(err, ShouldBeNil)
								So(history, ShouldNotBeNil)
								So(len(history.MessageList), ShouldEqual, 5)
								So(arr, ShouldResemble, historyArr)
							})
							Convey("We should able to fetch the with query ADDED AT & DESC ORDER& LIMIT ", func() {
								history, err := rest.GetHistory(
									channel.Id,
									&request.Query{
										From:      postMid.CreatedAt,
										SortOrder: "DESC",
										Limit:     3,
									},
									ses.ClientId,
								)

								var historyArr []string
								arr := []string{"body4", "body3", "body2"}
								for _, x := range history.MessageList {
									historyArr = append(historyArr, x.Message.Body)
								}
								So(err, ShouldBeNil)
								So(history, ShouldNotBeNil)
								So(len(history.MessageList), ShouldEqual, 3)
								So(arr, ShouldResemble, historyArr)
							})
							Convey("We should able to fetch the with query ADDED AT & ASC ORDER& LIMIT ", func() {
								history, err := rest.GetHistory(
									channel.Id,
									&request.Query{
										From:      postMid.CreatedAt,
										SortOrder: "ASC",
										Limit:     3,
									},
									ses.ClientId,
								)

								var historyArr []string
								arr := []string{"postMid message", "body5", "body6"}
								for _, x := range history.MessageList {
									historyArr = append(historyArr, x.Message.Body)
								}
								So(err, ShouldBeNil)
								So(history, ShouldNotBeNil)
								So(len(history.MessageList), ShouldEqual, 3)
								So(arr, ShouldResemble, historyArr)
							})
						})
					})
				})
			})
		})
	})
}
Esempio n. 5
0
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)
				})
			})
		})
	})
}
Esempio n. 6
0
func TestPinnedActivityChannel(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {

		SkipConvey("while  testing pinned activity channel", t, func() {
			groupName := models.RandomGroupName()

			account := models.NewAccount()
			account.OldId = AccountOldId.Hex()
			account, err := rest.CreateAccount(account)
			So(err, ShouldBeNil)
			So(account, ShouldNotBeNil)
			So(account.Id, ShouldNotEqual, 0)

			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)

			nonOwnerSes, err := modelhelper.FetchOrCreateSession(nonOwnerAccount.Nick, groupName)
			So(err, ShouldBeNil)
			So(nonOwnerSes, ShouldNotBeNil)

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

			post, err := rest.CreatePost(groupChannel.Id, ses.ClientId)
			So(err, ShouldBeNil)
			So(post, ShouldNotBeNil)

			Convey("requester should have one", func() {
				account := account
				channel, err := rest.FetchPinnedActivityChannel(account.Id, groupName)
				So(err, ShouldBeNil)
				So(channel, ShouldNotBeNil)
				So(channel.Id, ShouldNotEqual, 0)
				So(channel.TypeConstant, ShouldEqual, models.Channel_TYPE_PINNED_ACTIVITY)
				So(channel.CreatorId, ShouldEqual, account.Id)
			})

			Convey("owner should be able to update it", nil)

			Convey("non-owner should not be able to update it", nil)

			Convey("owner should not be able to add new participants into it", func() {
				channel, err := rest.FetchPinnedActivityChannel(account.Id, groupName)
				So(err, ShouldBeNil)
				So(channel, ShouldNotBeNil)
				channelParticipant, err := rest.AddChannelParticipant(channel.Id, ses.ClientId, nonOwnerAccount.Id)
				// there should be an err
				So(err, ShouldNotBeNil)
				// channel should be nil
				So(channelParticipant, ShouldBeNil)
			})

			Convey("normal user shouldnt be able to add new participants to it", func() {
				channel, err := rest.FetchPinnedActivityChannel(account.Id, groupName)
				So(err, ShouldBeNil)
				So(channel, ShouldNotBeNil)
				channelParticipant, err := rest.AddChannelParticipant(channel.Id, nonOwnerSes.ClientId, nonOwnerAccount.Id)
				// there should be an err
				So(err, ShouldNotBeNil)
				// channel should be nil
				So(channelParticipant, ShouldBeNil)
			})

			Convey("owner should  not be able to remove participant from it", func() {
				channel, err := rest.FetchPinnedActivityChannel(account.Id, groupName)
				So(err, ShouldBeNil)
				So(channel, ShouldNotBeNil)
				channelParticipant, err := rest.DeleteChannelParticipant(channel.Id, ses.ClientId, nonOwnerAccount.Id)
				// there should be an err
				So(err, ShouldNotBeNil)
				// channel should be nil
				So(channelParticipant, ShouldBeNil)
			})

			Convey("normal user shouldnt be able to remove participants from it", func() {
				channel, err := rest.FetchPinnedActivityChannel(account.Id, groupName)
				So(err, ShouldBeNil)
				So(channel, ShouldNotBeNil)
				channelParticipant, err := rest.DeleteChannelParticipant(channel.Id, nonOwnerSes.ClientId, nonOwnerAccount.Id)
				// there should be an err
				So(err, ShouldNotBeNil)
				// channel should be nil
				So(channelParticipant, ShouldBeNil)
			})

			Convey("owner should be able to add new message into it", func() {
				_, err := rest.AddPinnedMessage(account.Id, post.Id, "koding")
				// there should be an err
				So(err, ShouldBeNil)

				_, err = rest.RemovePinnedMessage(account.Id, post.Id, "koding")
				So(err, ShouldBeNil)

			})

			Convey("owner should be able to list messages", func() {
				groupName := "testgroup" + strconv.FormatInt(rand.Int63(), 10)

				pinnedChannel, err := rest.FetchPinnedActivityChannel(account.Id, groupName)
				So(err, ShouldBeNil)
				So(pinnedChannel, ShouldNotBeNil)

				groupChannel, err := rest.CreateChannelByGroupNameAndType(
					account.Id,
					groupName,
					models.Channel_TYPE_DEFAULT,
					ses.ClientId,
				)
				So(err, ShouldBeNil)
				So(groupChannel, ShouldNotBeNil)

				post1, err := rest.CreatePostWithBody(groupChannel.Id, nonOwnerAccount.Id, "create a message #1times")
				So(err, ShouldBeNil)
				So(post1, ShouldNotBeNil)

				_, err = rest.AddPinnedMessage(nonOwnerAccount.Id, post1.Id, groupName)
				// there should be an err
				So(err, ShouldBeNil)

				post2, err := rest.CreatePostWithBody(groupChannel.Id, nonOwnerAccount.Id, "create a message #1another")
				So(err, ShouldBeNil)
				So(post2, ShouldNotBeNil)

				_, err = rest.AddPinnedMessage(nonOwnerAccount.Id, post2.Id, groupName)
				// there should be an err
				So(err, ShouldBeNil)

				//time.Sleep(time.Second * 5)

				history, err := rest.FetchPinnedMessages(account.Id, groupName)
				// there should be an err
				So(err, ShouldBeNil)
				// channel should be nil
				So(history, ShouldNotBeNil)

				// message count should be 2
				So(len(history.MessageList), ShouldEqual, 2)

				// unread count should be 0
				So(history.UnreadCount, ShouldEqual, 0)

				// old id should be the same one
				So(history.MessageList[0].AccountOldId, ShouldContainSubstring, nonOwnerAccount.OldId)

				// replies count should be 0
				So(len(history.MessageList[0].Replies), ShouldEqual, 0)
			})

			Convey("Messages shouldnt be added as pinned twice ", func() {
				// use account id as message id
				_, err := rest.AddPinnedMessage(account.Id, post.Id, "koding")
				// there should be an err
				So(err, ShouldBeNil)
				// use account id as message id, pin message is idempotent, if it is
				// in the channel, wont give error
				_, err = rest.AddPinnedMessage(account.Id, post.Id, "koding")
				// there should not be an err
				So(err, ShouldBeNil)
			})

			Convey("Non-exist message should not be added as pinned ", nil)

		})
	})
}
Esempio n. 7
0
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")
			})
		})
	})
}