<commit_before>package main

import (
	"math/rand"
	"socialapi/models"
	"socialapi/request"
	"socialapi/rest"
	"strconv"
	"testing"
	"time"

	. "github.com/smartystreets/goconvey/convey"
)

func TestChannelMessage(t *testing.T) {
	Convey("While testing channel messages given a channel", t, func() {
		rand.Seed(time.Now().UnixNano())
		groupName := "testgroup" + strconv.FormatInt(rand.Int63(), 10)

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

		nonOwnerAccount := models.NewAccount()
		nonOwnerAccount.OldId = AccountOldId2.Hex()
		nonOwnerAccount, err = rest.CreateAccount(nonOwnerAccount)
		So(err, ShouldBeNil)
		So(nonOwnerAccount, ShouldNotBeNil)

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

		Convey("message should be able added to the group channel", func() {
			post, err := rest.CreatePost(groupChannel.Id, account.Id)
			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)
				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() {
			topicChannel, err := rest.CreateChannelByGroupNameAndType(account.Id, "koding", models.Channel_TYPE_TOPIC)
			So(err, ShouldBeNil)
			So(topicChannel, ShouldNotBeNil)

			post, err := rest.CreatePost(topicChannel.Id, account.Id)
			So(err, ShouldBeNil)
			So(post, ShouldNotBeNil)
			So(post.InitialChannelId, ShouldNotEqual, topicChannel.Id)

			publicChannel, err := rest.GetChannel(post.InitialChannelId)
			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, account.Id)
			So(err, ShouldBeNil)
			So(post, ShouldNotBeNil)
			err = rest.DeletePost(post.Id, account.Id, groupChannel.GroupName)
			So(err, ShouldBeNil)
			post2, err := rest.GetPost(post.Id, account.Id, groupChannel.GroupName)
			So(err, ShouldNotBeNil)
			So(post2, ShouldBeNil)
		})

		// handled by social worker
		Convey("message can be deleted by an admin", nil)
		Convey("message can not be edited by non-owner", nil)

		Convey("owner can like message", func() {
			post, err := rest.CreatePost(groupChannel.Id, account.Id)
			So(err, ShouldBeNil)
			So(post, ShouldNotBeNil)

			_, err = rest.AddInteraction("like", post.Id, post.AccountId)
			So(err, ShouldBeNil)

			cmc, err := rest.GetPostWithRelatedData(
				post.Id,
				&request.Query{
					AccountId: post.AccountId,
					GroupName: groupName,
				},
			)

			So(err, ShouldBeNil)
			So(cmc, ShouldNotBeNil)

			// it is liked by author
			So(cmc.Interactions["like"].IsInteracted, ShouldBeTrue)

			// actor length should be 1
			So(cmc.Interactions["like"].ActorsCount, ShouldEqual, 1)
<commit_msg>socialapi/tests: test for IP of Client is added to tests<commit_after>package main

import (
	"fmt"
	"math/rand"
	"net/http"
	"socialapi/models"
	"socialapi/request"
	"socialapi/rest"
	"strconv"
	"testing"
	"time"

	. "github.com/smartystreets/goconvey/convey"
)

func TestChannelMessage(t *testing.T) {
	Convey("While testing channel messages given a channel", t, func() {
		rand.Seed(time.Now().UnixNano())
		groupName := "testgroup" + strconv.FormatInt(rand.Int63(), 10)

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

		nonOwnerAccount := models.NewAccount()
		nonOwnerAccount.OldId = AccountOldId2.Hex()
		nonOwnerAccount, err = rest.CreateAccount(nonOwnerAccount)
		So(err, ShouldBeNil)
		So(nonOwnerAccount, ShouldNotBeNil)

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

		Convey("message should be able added to the group channel", func() {
			post, err := rest.CreatePost(groupChannel.Id, account.Id)
			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)
				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() {
			topicChannel, err := rest.CreateChannelByGroupNameAndType(account.Id, "koding", models.Channel_TYPE_TOPIC)
			So(err, ShouldBeNil)
			So(topicChannel, ShouldNotBeNil)

			post, err := rest.CreatePost(topicChannel.Id, account.Id)
			So(err, ShouldBeNil)
			So(post, ShouldNotBeNil)
			So(post.InitialChannelId, ShouldNotEqual, topicChannel.Id)

			publicChannel, err := rest.GetChannel(post.InitialChannelId)
			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, account.Id)
			So(err, ShouldBeNil)
			So(post, ShouldNotBeNil)
			err = rest.DeletePost(post.Id, account.Id, groupChannel.GroupName)
			So(err, ShouldBeNil)
			post2, err := rest.GetPost(post.Id, account.Id, groupChannel.GroupName)
			So(err, ShouldNotBeNil)
			So(post2, ShouldBeNil)
		})

		Convey("message should have IP of the Client", func() {
			h := http.Header{}
			h.Add("X-Forwarded-For", "172.16.31.10")
			post, err := rest.CreatePostWithHeader(groupChannel.Id, account.Id, h)
			So(err, ShouldBeNil)
			So(post, ShouldNotBeNil)
			So(post.Payload, ShouldNotBeNil)
			fmt.Println(post.Payload)
		})

		// handled by social worker
		Convey("message can be deleted by an admin", nil)
		Convey("message can not be edited by non-owner", nil)

		Convey("owner can like message", func() {
			post, err := rest.CreatePost(groupChannel.Id, account.Id)
			So(err, ShouldBeNil)
			So(post, ShouldNotBeNil)

			_, err = rest.AddInteraction("like", post.Id, post.AccountId)
			So(err, ShouldBeNil)

			cmc, err := rest.GetPostWithRelatedData(
				post.Id,
				&request.Query{
					AccountId: post.AccountId,
					GroupName: groupName,
				},
			)

			So(err, ShouldBeNil)
			So(cmc, ShouldNotBeNil)

			// it is liked by author
			So(cmc.Interactions["like"].IsInteracted, ShouldBeTrue)

			// actor length should be 1
			So(cmc.Interactions["like"].ActorsCount, ShouldEqual, 1)

{'fraction_non_alphanumeric': 0.12269435715261648, 'fraction_numerical': 0.003962289930318349, 'mean_word_length': 4.277577505407354, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1553204', 'n_tokens_mistral': 2802, 'n_tokens_neox': 2427, 'n_words': 573}