/
remote_cluster_test.go
152 lines (126 loc) · 5.11 KB
/
remote_cluster_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost-server/v5/model"
)
func TestAddRemoteCluster(t *testing.T) {
t.Run("adding remote cluster with duplicate site url and remote team id", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
remoteCluster := &model.RemoteCluster{
RemoteTeamId: model.NewId(),
DisplayName: "test",
SiteURL: "http://localhost:8065",
Token: "test",
RemoteToken: "test",
Topics: "",
CreatorId: th.BasicUser.Id,
}
_, err := th.App.AddRemoteCluster(remoteCluster)
require.Nil(t, err, "Adding a remote cluster should not error")
remoteCluster.RemoteId = model.NewId()
_, err = th.App.AddRemoteCluster(remoteCluster)
require.NotNil(t, err, "Adding a duplicate remote cluster should error")
assert.Contains(t, err.Error(), "Remote cluster has already been added.")
})
t.Run("adding remote cluster with duplicate site url or remote team id is allowed", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
remoteCluster := &model.RemoteCluster{
RemoteTeamId: model.NewId(),
DisplayName: "test",
SiteURL: "http://localhost:8065",
Token: "test",
RemoteToken: "test",
Topics: "",
CreatorId: th.BasicUser.Id,
}
existingRemoteCluster, err := th.App.AddRemoteCluster(remoteCluster)
require.Nil(t, err, "Adding a remote cluster should not error")
// Same site url but different remote team id
remoteCluster.RemoteId = model.NewId()
remoteCluster.RemoteTeamId = model.NewId()
remoteCluster.SiteURL = existingRemoteCluster.SiteURL
_, err = th.App.AddRemoteCluster(remoteCluster)
assert.Nil(t, err, "Adding a remote cluster should not error")
// Same remote team id but different site url
remoteCluster.RemoteId = model.NewId()
remoteCluster.RemoteTeamId = existingRemoteCluster.RemoteTeamId
remoteCluster.SiteURL = existingRemoteCluster.SiteURL + "/new"
_, err = th.App.AddRemoteCluster(remoteCluster)
assert.Nil(t, err, "Adding a remote cluster should not error")
})
}
func TestUpdateRemoteCluster(t *testing.T) {
t.Run("update remote cluster with an already existing site url and team id", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
remoteCluster := &model.RemoteCluster{
RemoteTeamId: model.NewId(),
DisplayName: "test",
SiteURL: "http://localhost:8065",
Token: "test",
RemoteToken: "test",
Topics: "",
CreatorId: th.BasicUser.Id,
}
otherRemoteCluster := &model.RemoteCluster{
RemoteTeamId: model.NewId(),
DisplayName: "test",
SiteURL: "http://localhost:8066",
Token: "test",
RemoteToken: "test",
Topics: "",
CreatorId: th.BasicUser.Id,
}
_, err := th.App.AddRemoteCluster(remoteCluster)
require.Nil(t, err, "Adding a remote cluster should not error")
savedRemoteClustered, err := th.App.AddRemoteCluster(otherRemoteCluster)
require.Nil(t, err, "Adding a remote cluster should not error")
savedRemoteClustered.SiteURL = remoteCluster.SiteURL
savedRemoteClustered.RemoteTeamId = remoteCluster.RemoteTeamId
_, err = th.App.UpdateRemoteCluster(savedRemoteClustered)
require.NotNil(t, err, "Updating remote cluster with duplicate site url should error")
assert.Contains(t, err.Error(), "Remote cluster with the same url already exists.")
})
t.Run("update remote cluster with an already existing site url or team id, is allowed", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
remoteCluster := &model.RemoteCluster{
RemoteTeamId: model.NewId(),
DisplayName: "test",
SiteURL: "http://localhost:8065",
Token: "test",
RemoteToken: "test",
Topics: "",
CreatorId: th.BasicUser.Id,
}
otherRemoteCluster := &model.RemoteCluster{
RemoteTeamId: model.NewId(),
DisplayName: "test",
SiteURL: "http://localhost:8066",
Token: "test",
RemoteToken: "test",
Topics: "",
CreatorId: th.BasicUser.Id,
}
existingRemoteCluster, err := th.App.AddRemoteCluster(remoteCluster)
require.Nil(t, err, "Adding a remote cluster should not error")
anotherExistingRemoteClustered, err := th.App.AddRemoteCluster(otherRemoteCluster)
require.Nil(t, err, "Adding a remote cluster should not error")
// Same site url but different remote team id
anotherExistingRemoteClustered.SiteURL = existingRemoteCluster.SiteURL
anotherExistingRemoteClustered.RemoteTeamId = model.NewId()
_, err = th.App.UpdateRemoteCluster(anotherExistingRemoteClustered)
assert.Nil(t, err, "Updating remote cluster should not error")
// Same remote team id but different site url
anotherExistingRemoteClustered.SiteURL = existingRemoteCluster.SiteURL + "/new"
anotherExistingRemoteClustered.RemoteTeamId = existingRemoteCluster.RemoteTeamId
_, err = th.App.UpdateRemoteCluster(anotherExistingRemoteClustered)
assert.Nil(t, err, "Updating remote cluster should not error")
})
}