summaryrefslogtreecommitdiffstats
path: root/app/import_test.go
diff options
context:
space:
mode:
authorGeorge Goldberg <george@gberg.me>2017-02-28 23:55:21 +0000
committerenahum <nahumhbl@gmail.com>2017-02-28 20:55:21 -0300
commitf7682ad11a3f27925b46ffc37fa66c6d4b6feef5 (patch)
tree0bba7bcb428599e847a9abfd9b003d1e7aa29982 /app/import_test.go
parent48d124ec9aee139c00538b17c1cf978015641c8e (diff)
downloadchat-f7682ad11a3f27925b46ffc37fa66c6d4b6feef5.tar.gz
chat-f7682ad11a3f27925b46ffc37fa66c6d4b6feef5.tar.bz2
chat-f7682ad11a3f27925b46ffc37fa66c6d4b6feef5.zip
PLT-5367: Basic post bulk importing. (#5562)
Diffstat (limited to 'app/import_test.go')
-rw-r--r--app/import_test.go360
1 files changed, 358 insertions, 2 deletions
diff --git a/app/import_test.go b/app/import_test.go
index ddcc2d06a..165c94875 100644
--- a/app/import_test.go
+++ b/app/import_test.go
@@ -6,6 +6,7 @@ package app
import (
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/utils"
+ "runtime/debug"
"strings"
"testing"
)
@@ -454,6 +455,108 @@ func TestImportValidateUserChannelsImportData(t *testing.T) {
}
}
+func TestImportValidatePostImportData(t *testing.T) {
+
+ // Test with minimum required valid properties.
+ data := PostImportData{
+ Team: ptrStr("teamname"),
+ Channel: ptrStr("channelname"),
+ User: ptrStr("username"),
+ Message: ptrStr("message"),
+ CreateAt: ptrInt64(model.GetMillis()),
+ }
+ if err := validatePostImportData(&data); err != nil {
+ t.Fatal("Validation failed but should have been valid.")
+ }
+
+ // Test with missing required properties.
+ data = PostImportData{
+ Channel: ptrStr("channelname"),
+ User: ptrStr("username"),
+ Message: ptrStr("message"),
+ CreateAt: ptrInt64(model.GetMillis()),
+ }
+ if err := validatePostImportData(&data); err == nil {
+ t.Fatal("Should have failed due to missing required property.")
+ }
+
+ data = PostImportData{
+ Team: ptrStr("teamname"),
+ User: ptrStr("username"),
+ Message: ptrStr("message"),
+ CreateAt: ptrInt64(model.GetMillis()),
+ }
+ if err := validatePostImportData(&data); err == nil {
+ t.Fatal("Should have failed due to missing required property.")
+ }
+
+ data = PostImportData{
+ Team: ptrStr("teamname"),
+ Channel: ptrStr("channelname"),
+ Message: ptrStr("message"),
+ CreateAt: ptrInt64(model.GetMillis()),
+ }
+ if err := validatePostImportData(&data); err == nil {
+ t.Fatal("Should have failed due to missing required property.")
+ }
+
+ data = PostImportData{
+ Team: ptrStr("teamname"),
+ Channel: ptrStr("channelname"),
+ User: ptrStr("username"),
+ CreateAt: ptrInt64(model.GetMillis()),
+ }
+ if err := validatePostImportData(&data); err == nil {
+ t.Fatal("Should have failed due to missing required property.")
+ }
+
+ data = PostImportData{
+ Team: ptrStr("teamname"),
+ Channel: ptrStr("channelname"),
+ User: ptrStr("username"),
+ Message: ptrStr("message"),
+ }
+ if err := validatePostImportData(&data); err == nil {
+ t.Fatal("Should have failed due to missing required property.")
+ }
+
+ // Test with invalid message.
+ data = PostImportData{
+ Team: ptrStr("teamname"),
+ Channel: ptrStr("channelname"),
+ User: ptrStr("username"),
+ Message: ptrStr(strings.Repeat("1234567890", 500)),
+ CreateAt: ptrInt64(model.GetMillis()),
+ }
+ if err := validatePostImportData(&data); err == nil {
+ t.Fatal("Should have failed due to too long message.")
+ }
+
+ // Test with invalid CreateAt
+ data = PostImportData{
+ Team: ptrStr("teamname"),
+ Channel: ptrStr("channelname"),
+ User: ptrStr("username"),
+ Message: ptrStr("message"),
+ CreateAt: ptrInt64(0),
+ }
+ if err := validatePostImportData(&data); err == nil {
+ t.Fatal("Should have failed due to 0 create-at value.")
+ }
+
+ // Test with valid all optional parameters.
+ data = PostImportData{
+ Team: ptrStr("teamname"),
+ Channel: ptrStr("channelname"),
+ User: ptrStr("username"),
+ Message: ptrStr("message"),
+ CreateAt: ptrInt64(model.GetMillis()),
+ }
+ if err := validatePostImportData(&data); err != nil {
+ t.Fatal("Should have succeeded.")
+ }
+}
+
func TestImportImportTeam(t *testing.T) {
_ = Setup()
@@ -1333,6 +1436,252 @@ func TestImportImportUser(t *testing.T) {
}
}
+func AssertAllPostsCount(t *testing.T, initialCount int64, change int64, teamName string) {
+ if result := <-Srv.Store.Post().AnalyticsPostCount(teamName, false, false); result.Err != nil {
+ t.Fatal(result.Err)
+ } else {
+ if initialCount+change != result.Data.(int64) {
+ debug.PrintStack()
+ t.Fatalf("Did not find the expected number of posts.")
+ }
+ }
+}
+
+func TestImportImportPost(t *testing.T) {
+ _ = Setup()
+
+ // Create a Team.
+ teamName := model.NewId()
+ ImportTeam(&TeamImportData{
+ Name: &teamName,
+ DisplayName: ptrStr("Display Name"),
+ Type: ptrStr("O"),
+ }, false)
+ team, err := GetTeamByName(teamName)
+ if err != nil {
+ t.Fatalf("Failed to get team from database.")
+ }
+
+ // Create a Channel.
+ channelName := model.NewId()
+ ImportChannel(&ChannelImportData{
+ Team: &teamName,
+ Name: &channelName,
+ DisplayName: ptrStr("Display Name"),
+ Type: ptrStr("O"),
+ }, false)
+ channel, err := GetChannelByName(channelName, team.Id)
+ if err != nil {
+ t.Fatalf("Failed to get channel from database.")
+ }
+
+ // Create a user.
+ username := model.NewId()
+ ImportUser(&UserImportData{
+ Username: &username,
+ Email: ptrStr(model.NewId() + "@example.com"),
+ }, false)
+ user, err := GetUserByUsername(username)
+ if err != nil {
+ t.Fatalf("Failed to get user from database.")
+ }
+
+ // Count the number of posts in the testing team.
+ var initialPostCount int64
+ if result := <-Srv.Store.Post().AnalyticsPostCount(team.Id, false, false); result.Err != nil {
+ t.Fatal(result.Err)
+ } else {
+ initialPostCount = result.Data.(int64)
+ }
+
+ // Try adding an invalid post in dry run mode.
+ data := &PostImportData{
+ Team: &teamName,
+ Channel: &channelName,
+ User: &username,
+ }
+ if err := ImportPost(data, true); err == nil {
+ t.Fatalf("Expected error.")
+ }
+ AssertAllPostsCount(t, initialPostCount, 0, team.Id)
+
+ // Try adding a valid post in dry run mode.
+ data = &PostImportData{
+ Team: &teamName,
+ Channel: &channelName,
+ User: &username,
+ Message: ptrStr("Hello"),
+ CreateAt: ptrInt64(model.GetMillis()),
+ }
+ if err := ImportPost(data, true); err != nil {
+ t.Fatalf("Expected success.")
+ }
+ AssertAllPostsCount(t, initialPostCount, 0, team.Id)
+
+ // Try adding an invalid post in apply mode.
+ data = &PostImportData{
+ Team: &teamName,
+ Channel: &channelName,
+ User: &username,
+ CreateAt: ptrInt64(model.GetMillis()),
+ }
+ if err := ImportPost(data, false); err == nil {
+ t.Fatalf("Expected error.")
+ }
+ AssertAllPostsCount(t, initialPostCount, 0, team.Id)
+
+ // Try adding a valid post with invalid team in apply mode.
+ data = &PostImportData{
+ Team: ptrStr(model.NewId()),
+ Channel: &channelName,
+ User: &username,
+ Message: ptrStr("Message"),
+ CreateAt: ptrInt64(model.GetMillis()),
+ }
+ if err := ImportPost(data, false); err == nil {
+ t.Fatalf("Expected error.")
+ }
+ AssertAllPostsCount(t, initialPostCount, 0, team.Id)
+
+ // Try adding a valid post with invalid channel in apply mode.
+ data = &PostImportData{
+ Team: &teamName,
+ Channel: ptrStr(model.NewId()),
+ User: &username,
+ Message: ptrStr("Message"),
+ CreateAt: ptrInt64(model.GetMillis()),
+ }
+ if err := ImportPost(data, false); err == nil {
+ t.Fatalf("Expected error.")
+ }
+ AssertAllPostsCount(t, initialPostCount, 0, team.Id)
+
+ // Try adding a valid post with invalid user in apply mode.
+ data = &PostImportData{
+ Team: &teamName,
+ Channel: &channelName,
+ User: ptrStr(model.NewId()),
+ Message: ptrStr("Message"),
+ CreateAt: ptrInt64(model.GetMillis()),
+ }
+ if err := ImportPost(data, false); err == nil {
+ t.Fatalf("Expected error.")
+ }
+ AssertAllPostsCount(t, initialPostCount, 0, team.Id)
+
+ // Try adding a valid post in apply mode.
+ time := model.GetMillis()
+ data = &PostImportData{
+ Team: &teamName,
+ Channel: &channelName,
+ User: &username,
+ Message: ptrStr("Message"),
+ CreateAt: &time,
+ }
+ if err := ImportPost(data, false); err != nil {
+ t.Fatalf("Expected success.")
+ }
+ AssertAllPostsCount(t, initialPostCount, 1, team.Id)
+
+ // Check the post values.
+ if result := <-Srv.Store.Post().GetPostsCreatedAt(channel.Id, time); result.Err != nil {
+ t.Fatal(result.Err.Error())
+ } else {
+ posts := result.Data.([]*model.Post)
+ if len(posts) != 1 {
+ t.Fatal("Unexpected number of posts found.")
+ }
+ post := posts[0]
+ if post.Message != *data.Message || post.CreateAt != *data.CreateAt || post.UserId != user.Id {
+ t.Fatal("Post properties not as expected")
+ }
+ }
+
+ // Update the post.
+ data = &PostImportData{
+ Team: &teamName,
+ Channel: &channelName,
+ User: &username,
+ Message: ptrStr("Message"),
+ CreateAt: &time,
+ }
+ if err := ImportPost(data, false); err != nil {
+ t.Fatalf("Expected success.")
+ }
+ AssertAllPostsCount(t, initialPostCount, 1, team.Id)
+
+ // Check the post values.
+ if result := <-Srv.Store.Post().GetPostsCreatedAt(channel.Id, time); result.Err != nil {
+ t.Fatal(result.Err.Error())
+ } else {
+ posts := result.Data.([]*model.Post)
+ if len(posts) != 1 {
+ t.Fatal("Unexpected number of posts found.")
+ }
+ post := posts[0]
+ if post.Message != *data.Message || post.CreateAt != *data.CreateAt || post.UserId != user.Id {
+ t.Fatal("Post properties not as expected")
+ }
+ }
+
+ // Save the post with a different time.
+ newTime := time + 1
+ data = &PostImportData{
+ Team: &teamName,
+ Channel: &channelName,
+ User: &username,
+ Message: ptrStr("Message"),
+ CreateAt: &newTime,
+ }
+ if err := ImportPost(data, false); err != nil {
+ t.Fatalf("Expected success.")
+ }
+ AssertAllPostsCount(t, initialPostCount, 2, team.Id)
+
+ // Save the post with a different message.
+ data = &PostImportData{
+ Team: &teamName,
+ Channel: &channelName,
+ User: &username,
+ Message: ptrStr("Message 2"),
+ CreateAt: &time,
+ }
+ if err := ImportPost(data, false); err != nil {
+ t.Fatalf("Expected success.")
+ }
+ AssertAllPostsCount(t, initialPostCount, 3, team.Id)
+
+ // Test with hashtags
+ hashtagTime := time + 2
+ data = &PostImportData{
+ Team: &teamName,
+ Channel: &channelName,
+ User: &username,
+ Message: ptrStr("Message 2 #hashtagmashupcity"),
+ CreateAt: &hashtagTime,
+ }
+ if err := ImportPost(data, false); err != nil {
+ t.Fatalf("Expected success.")
+ }
+ AssertAllPostsCount(t, initialPostCount, 4, team.Id)
+
+ if result := <-Srv.Store.Post().GetPostsCreatedAt(channel.Id, hashtagTime); result.Err != nil {
+ t.Fatal(result.Err.Error())
+ } else {
+ posts := result.Data.([]*model.Post)
+ if len(posts) != 1 {
+ t.Fatal("Unexpected number of posts found.")
+ }
+ post := posts[0]
+ if post.Message != *data.Message || post.CreateAt != *data.CreateAt || post.UserId != user.Id {
+ t.Fatal("Post properties not as expected")
+ }
+ if post.Hashtags != "#hashtagmashupcity" {
+ t.Fatalf("Hashtags not as expected: %s", post.Hashtags)
+ }
+ }
+}
+
func TestImportImportLine(t *testing.T) {
_ = Setup()
@@ -1362,6 +1711,12 @@ func TestImportImportLine(t *testing.T) {
if err := ImportLine(line, false); err == nil {
t.Fatalf("Expected an error when importing a line with type uesr with a nil user.")
}
+
+ // Try import line with post type but nil post.
+ line.Type = "post"
+ if err := ImportLine(line, false); err == nil {
+ t.Fatalf("Expected an error when importing a line with type post with a nil post.")
+ }
}
func TestImportBulkImport(t *testing.T) {
@@ -1369,13 +1724,14 @@ func TestImportBulkImport(t *testing.T) {
teamName := model.NewId()
channelName := model.NewId()
+ username := model.NewId()
// Run bulk import with a valid 1 of everything.
data1 := `{"type": "version", "version": 1}
{"type": "team", "team": {"type": "O", "display_name": "lskmw2d7a5ao7ppwqh5ljchvr4", "name": "` + teamName + `"}}
{"type": "channel", "channel": {"type": "O", "display_name": "xr6m6udffngark2uekvr3hoeny", "team": "` + teamName + `", "name": "` + channelName + `"}}
-{"type": "user", "user": {"username": "kufjgnkxkrhhfgbrip6qxkfsaa", "email": "kufjgnkxkrhhfgbrip6qxkfsaa@example.com"}}
-{"type": "user", "user": {"username": "bwshaim6qnc2ne7oqkd5b2s2rq", "email": "bwshaim6qnc2ne7oqkd5b2s2rq@example.com", "teams": [{"name": "` + teamName + `", "channels": [{"name": "` + channelName + `"}]}]}}`
+{"type": "user", "user": {"username": "` + username + `", "email": "` + username + `@example.com", "teams": [{"name": "` + teamName + `", "channels": [{"name": "` + channelName + `"}]}]}}
+{"type": "post", "post": {"team": "` + teamName + `", "channel": "` + channelName + `", "user": "` + username + `", "message": "Hello World", "create_at": 123456789012}}`
if err, line := BulkImport(strings.NewReader(data1), false); err != nil || line != 0 {
t.Fatalf("BulkImport should have succeeded: %v, %v", err.Error(), line)