Exemplo n.º 1
0
func generateAndAddMembersToGroup(groupSlug string, count int) {
	// generate members
	for i := 0; i < count; i++ {
		account := models.CreateAccountInBothDbsWithCheck()
		p := models.NewPresenceDaily()
		p.AccountId = account.GetId()
		p.GroupName = groupSlug
		So(p.Create(), ShouldBeNil)
	}
}
Exemplo n.º 2
0
func TestAddRemoveChannelParticipant(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		controller := &Controller{}
		Convey("While testing add/remove channel participants", t, func() {
			account := models.CreateAccountInBothDbsWithCheck()
			groupName := models.RandomGroupName()
			_ = 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)

			pe := &models.ParticipantEvent{}

			Convey("When user follows/unfollows a topic, they must be notified", func() {
				topicChannel, err := rest.CreateChannelByGroupNameAndType(
					account.Id,
					groupName,
					models.Channel_TYPE_TOPIC,
					ses.ClientId,
				)
				tests.ResultedWithNoErrorCheck(topicChannel, err)

				pe.Id = topicChannel.Id
				cp := &models.ChannelParticipant{}
				cp.AccountId = account.Id

				pe.Participants = []*models.ChannelParticipant{cp}

				participants, err := controller.fetchNotifiedParticipantIds(topicChannel, pe, AddedToChannelEventName)
				So(err, ShouldBeNil)
				So(len(participants), ShouldEqual, 1)
				So(participants[0], ShouldEqual, account.Id)

				participants, err = controller.fetchNotifiedParticipantIds(topicChannel, pe, RemovedFromChannelEventName)
				So(err, ShouldBeNil)
				So(len(participants), ShouldEqual, 1)
				So(participants[0], ShouldEqual, account.Id)
			})

			Convey("When user joins a topic, only participant user must be notified", func() {
				privateChannel, err := rest.CreateChannelByGroupNameAndType(
					account.Id,
					groupName,
					models.Channel_TYPE_PRIVATE_MESSAGE,
					ses.ClientId,
				)

				tests.ResultedWithNoErrorCheck(privateChannel, err)

				pe.Id = privateChannel.Id
				cp := &models.ChannelParticipant{}
				cp.AccountId = account.Id

				pe.Participants = []*models.ChannelParticipant{cp}

				participants, err := controller.fetchNotifiedParticipantIds(privateChannel, pe, AddedToChannelEventName)
				So(err, ShouldBeNil)
				So(len(participants), ShouldEqual, 1)
				So(participants[0], ShouldEqual, account.Id)
			})

			Convey("When user leaves a topic, all participants musts be notified", func() {
				privateChannel, err := rest.CreateChannelByGroupNameAndType(
					account.Id,
					groupName,
					models.Channel_TYPE_PRIVATE_MESSAGE,
					ses.ClientId,
				)
				tests.ResultedWithNoErrorCheck(privateChannel, err)

				pe.Id = privateChannel.Id
				cp := &models.ChannelParticipant{}
				cp.AccountId = account.Id

				pe.Participants = []*models.ChannelParticipant{cp}

				participants, err := controller.fetchNotifiedParticipantIds(privateChannel, pe, RemovedFromChannelEventName)
				tests.ResultedWithNoErrorCheck(participants, err)

				So(len(participants), ShouldEqual, 2)
				So(participants[0], ShouldEqual, account.Id)
			})
		})
	})

}
Exemplo n.º 3
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")
			})
		})
	})
}