summaryrefslogtreecommitdiffstats
path: root/app/import_test.go
diff options
context:
space:
mode:
authorPradeep Murugesan <pradeepmurugesan@outlook.com>2018-07-20 10:49:49 +0200
committerSaturnino Abril <saturnino.abril@gmail.com>2018-07-20 16:49:49 +0800
commitee1d037ca6893ae57061a1eba25f624d7c163baa (patch)
treeaabb43244860e5a3b597c23e9b182ac22888effd /app/import_test.go
parent17c081736186816d0bc32e76432d086fc7df3809 (diff)
downloadchat-ee1d037ca6893ae57061a1eba25f624d7c163baa.tar.gz
chat-ee1d037ca6893ae57061a1eba25f624d7c163baa.tar.bz2
chat-ee1d037ca6893ae57061a1eba25f624d7c163baa.zip
Support attachments in post and replies - Bulk import (#9124)
* 9006 - process the attachments of the post * 9006 enabling the import of attachments in the reply post * 9006 assert if the post and files are linked * 9006 fixed the typo
Diffstat (limited to 'app/import_test.go')
-rw-r--r--app/import_test.go188
1 files changed, 187 insertions, 1 deletions
diff --git a/app/import_test.go b/app/import_test.go
index 8a88937f9..f99e100f1 100644
--- a/app/import_test.go
+++ b/app/import_test.go
@@ -3792,7 +3792,7 @@ func TestImportBulkImport(t *testing.T) {
{"type": "user", "user": {"username": "` + username + `", "email": "` + username + `@example.com", "teams": [{"name": "` + teamName + `", "channels": [{"name": "` + channelName + `"}]}]}}
{"type": "user", "user": {"username": "` + username2 + `", "email": "` + username2 + `@example.com", "teams": [{"name": "` + teamName + `", "channels": [{"name": "` + channelName + `"}]}]}}
{"type": "user", "user": {"username": "` + username3 + `", "email": "` + username3 + `@example.com", "teams": [{"name": "` + teamName + `", "channels": [{"name": "` + channelName + `"}]}]}}
-{"type": "post", "post": {"team": "` + teamName + `", "channel": "` + channelName + `", "user": "` + username + `", "message": "Hello World", "create_at": 123456789012}}
+{"type": "post", "post": {"team": "` + teamName + `", "channel": "` + channelName + `", "user": "` + username + `", "message": "Hello World", "create_at": 123456789012, "attachments":[{"path": "` + testImage + `"}]}}
{"type": "direct_channel", "direct_channel": {"members": ["` + username + `", "` + username2 + `"]}}
{"type": "direct_channel", "direct_channel": {"members": ["` + username + `", "` + username2 + `", "` + username3 + `"]}}
{"type": "direct_post", "direct_post": {"channel_members": ["` + username + `", "` + username2 + `"], "user": "` + username + `", "message": "Hello Direct Channel", "create_at": 123456789013}}
@@ -3911,3 +3911,189 @@ func TestImportImportEmoji(t *testing.T) {
err = th.App.ImportEmoji(&data, false)
assert.Nil(t, err, "Second run should have succeeded apply mode")
}
+
+func TestImportAttachment(t *testing.T) {
+ th := Setup()
+ defer th.TearDown()
+
+ testsDir, _ := utils.FindDir("tests")
+ testImage := filepath.Join(testsDir, "test.png")
+ invalidPath := "some-invalid-path"
+
+ userId := model.NewId()
+ data := AttachmentImportData{Path: &testImage}
+ _, err := th.App.ImportAttachment(&data, &model.Post{UserId: userId, ChannelId: "some-channel"}, "some-team", true)
+ assert.Nil(t, err, "sample run without errors")
+
+ attachments := GetAttachments(userId, th, t)
+ assert.Equal(t, len(attachments), 1)
+
+ data = AttachmentImportData{Path: &invalidPath}
+ _, err = th.App.ImportAttachment(&data, &model.Post{UserId: model.NewId(), ChannelId: "some-channel"}, "some-team", true)
+ assert.NotNil(t, err, "should have failed when opening the file")
+ assert.Equal(t, err.Id, "app.import.attachment.bad_file.error")
+}
+
+func TestImportPostAndRepliesWithAttachments(t *testing.T) {
+
+ th := Setup()
+ defer th.TearDown()
+
+ // Create a Team.
+ teamName := model.NewId()
+ th.App.ImportTeam(&TeamImportData{
+ Name: &teamName,
+ DisplayName: ptrStr("Display Name"),
+ Type: ptrStr("O"),
+ }, false)
+ team, err := th.App.GetTeamByName(teamName)
+ if err != nil {
+ t.Fatalf("Failed to get team from database.")
+ }
+
+ // Create a Channel.
+ channelName := model.NewId()
+ th.App.ImportChannel(&ChannelImportData{
+ Team: &teamName,
+ Name: &channelName,
+ DisplayName: ptrStr("Display Name"),
+ Type: ptrStr("O"),
+ }, false)
+ _, err = th.App.GetChannelByName(channelName, team.Id)
+ if err != nil {
+ t.Fatalf("Failed to get channel from database.")
+ }
+
+ // Create a user3.
+ username := model.NewId()
+ th.App.ImportUser(&UserImportData{
+ Username: &username,
+ Email: ptrStr(model.NewId() + "@example.com"),
+ }, false)
+ user3, err := th.App.GetUserByUsername(username)
+ if err != nil {
+ t.Fatalf("Failed to get user3 from database.")
+ }
+
+ username2 := model.NewId()
+ th.App.ImportUser(&UserImportData{
+ Username: &username2,
+ Email: ptrStr(model.NewId() + "@example.com"),
+ }, false)
+ user4, err := th.App.GetUserByUsername(username2)
+ if err != nil {
+ t.Fatalf("Failed to get user3 from database.")
+ }
+
+ // Post with attachments.
+ time := model.GetMillis()
+ attachmentsPostTime := time
+ attachmentsReplyTime := time + 1
+ testsDir, _ := utils.FindDir("tests")
+ testImage := filepath.Join(testsDir, "test.png")
+ testMarkDown := filepath.Join(testsDir, "test-attachments.md")
+ data := &PostImportData{
+ Team: &teamName,
+ Channel: &channelName,
+ User: &username,
+ Message: ptrStr("Message with reply"),
+ CreateAt: &attachmentsPostTime,
+ Attachments: &[]AttachmentImportData{{Path: &testImage}, {Path: &testMarkDown}},
+ Replies: &[]ReplyImportData{{
+ User: &user4.Username,
+ Message: ptrStr("Message reply"),
+ CreateAt: &attachmentsReplyTime,
+ Attachments: &[]AttachmentImportData{{Path: &testImage}},
+ }},
+ }
+
+ if err := th.App.ImportPost(data, false); err != nil {
+ t.Fatalf("Expected success.")
+ }
+
+ attachments := GetAttachments(user3.Id, th, t)
+ assert.Equal(t, len(attachments), 2)
+ assert.Contains(t, attachments[0].Path, team.Id)
+ assert.Contains(t, attachments[1].Path, team.Id)
+ AssertFileIdsInPost(attachments, th, t)
+
+ attachments = GetAttachments(user4.Id, th, t)
+ assert.Equal(t, len(attachments), 1)
+ assert.Contains(t, attachments[0].Path, team.Id)
+ AssertFileIdsInPost(attachments, th, t)
+
+ // Reply with Attachments in Direct Post
+
+ // Create direct post users.
+
+ username3 := model.NewId()
+ th.App.ImportUser(&UserImportData{
+ Username: &username3,
+ Email: ptrStr(model.NewId() + "@example.com"),
+ }, false)
+ user3, err = th.App.GetUserByUsername(username3)
+ if err != nil {
+ t.Fatalf("Failed to get user3 from database.")
+ }
+
+ username4 := model.NewId()
+ th.App.ImportUser(&UserImportData{
+ Username: &username4,
+ Email: ptrStr(model.NewId() + "@example.com"),
+ }, false)
+
+ user4, err = th.App.GetUserByUsername(username4)
+ if err != nil {
+ t.Fatalf("Failed to get user3 from database.")
+ }
+
+ directImportData := &DirectPostImportData{
+ ChannelMembers: &[]string{
+ user3.Username,
+ user4.Username,
+ },
+ User: &user3.Username,
+ Message: ptrStr("Message with Replies"),
+ CreateAt: ptrInt64(model.GetMillis()),
+ Replies: &[]ReplyImportData{{
+ User: &user4.Username,
+ Message: ptrStr("Message reply with attachment"),
+ CreateAt: ptrInt64(model.GetMillis()),
+ Attachments: &[]AttachmentImportData{{Path: &testImage}},
+ }},
+ }
+
+ if err := th.App.ImportDirectPost(directImportData, false); err != nil {
+ t.Fatalf("Expected success.")
+ }
+
+ attachments = GetAttachments(user4.Id, th, t)
+ assert.Equal(t, len(attachments), 1)
+ assert.Contains(t, attachments[0].Path, "noteam")
+ AssertFileIdsInPost(attachments, th, t)
+
+}
+
+func GetAttachments(userId string, th *TestHelper, t *testing.T) []*model.FileInfo {
+ if result := <-th.App.Srv.Store.FileInfo().GetForUser(userId); result.Err != nil {
+ t.Fatal(result.Err.Error())
+ } else {
+ return result.Data.([]*model.FileInfo)
+ }
+ return nil
+}
+
+func AssertFileIdsInPost(files []*model.FileInfo, th *TestHelper, t *testing.T) {
+ postId := files[0].PostId
+ assert.NotNil(t, postId)
+
+ if result := <-th.App.Srv.Store.Post().GetPostsByIds([]string{postId}); result.Err != nil {
+ t.Fatal(result.Err.Error())
+ } else {
+ posts := result.Data.([]*model.Post)
+ assert.Equal(t, len(posts), 1)
+ for _, file := range files {
+ assert.Contains(t, posts[0].FileIds, file.Id)
+ }
+ }
+}