Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Nightly Race / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
160 lines
5.3 KiB
Go
160 lines
5.3 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package model
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestMemberInviteUnmarshalJSON(t *testing.T) {
|
|
t.Run("raw email array", func(t *testing.T) {
|
|
var invite MemberInvite
|
|
err := json.Unmarshal([]byte(`["user1@example.com","user2@example.com"]`), &invite)
|
|
require.NoError(t, err)
|
|
require.Equal(t, []string{"user1@example.com", "user2@example.com"}, invite.Emails)
|
|
require.Empty(t, invite.ChannelIds)
|
|
require.Empty(t, invite.Profiles)
|
|
})
|
|
|
|
t.Run("object with emails and channels", func(t *testing.T) {
|
|
var invite MemberInvite
|
|
err := json.Unmarshal([]byte(`{"emails":["user1@example.com"],"channelIds":["junk"],"message":"hi"}`), &invite)
|
|
require.NoError(t, err)
|
|
require.Equal(t, []string{"user1@example.com"}, invite.Emails)
|
|
require.Equal(t, []string{"junk"}, invite.ChannelIds)
|
|
require.Equal(t, "hi", invite.Message)
|
|
require.Empty(t, invite.Profiles)
|
|
})
|
|
|
|
t.Run("object with profiles", func(t *testing.T) {
|
|
var invite MemberInvite
|
|
err := json.Unmarshal([]byte(`{"emails":["user1@example.com"],"profiles":[{"email":"user1@example.com","username":"user.one","first_name":"User","last_name":"One"}]}`), &invite)
|
|
require.NoError(t, err)
|
|
require.Equal(t, []string{"user1@example.com"}, invite.Emails)
|
|
require.Len(t, invite.Profiles, 1)
|
|
require.Equal(t, "user1@example.com", invite.Profiles[0].Email)
|
|
require.Equal(t, "user.one", invite.Profiles[0].Username)
|
|
require.Equal(t, "User", invite.Profiles[0].FirstName)
|
|
require.Equal(t, "One", invite.Profiles[0].LastName)
|
|
})
|
|
}
|
|
|
|
func TestMemberInviteIsValid(t *testing.T) {
|
|
validInvite := func() *MemberInvite {
|
|
return &MemberInvite{
|
|
Emails: []string{"user1@example.com"},
|
|
Profiles: []*MemberInviteProfile{{
|
|
Email: "user1@example.com",
|
|
Username: "user.one",
|
|
FirstName: "User",
|
|
LastName: "One",
|
|
}},
|
|
}
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
mutate func(*MemberInvite)
|
|
expectedErr string
|
|
}{
|
|
{
|
|
name: "valid without profiles",
|
|
mutate: func(invite *MemberInvite) { invite.Profiles = nil },
|
|
},
|
|
{name: "valid with profiles"},
|
|
{
|
|
name: "no emails",
|
|
mutate: func(invite *MemberInvite) { invite.Emails = nil },
|
|
expectedErr: "model.member.is_valid.emails.app_error",
|
|
},
|
|
{
|
|
name: "invalid channel",
|
|
mutate: func(invite *MemberInvite) { invite.ChannelIds = []string{"junk"} },
|
|
expectedErr: "model.member.is_valid.channel.app_error",
|
|
},
|
|
{
|
|
name: "case-insensitive profile email",
|
|
mutate: func(invite *MemberInvite) { invite.Emails[0] = "USER1@EXAMPLE.COM" },
|
|
},
|
|
{
|
|
name: "profile email not invited",
|
|
mutate: func(invite *MemberInvite) { invite.Profiles[0].Email = "other@example.com" },
|
|
expectedErr: "model.member.is_valid.profile_email.app_error",
|
|
},
|
|
{
|
|
name: "duplicate profile email",
|
|
mutate: func(invite *MemberInvite) {
|
|
invite.Profiles = append(invite.Profiles, &MemberInviteProfile{Email: "USER1@EXAMPLE.COM", Username: "user.two"})
|
|
},
|
|
expectedErr: "model.member.is_valid.profile_email_duplicate.app_error",
|
|
},
|
|
{
|
|
name: "nil profile",
|
|
mutate: func(invite *MemberInvite) { invite.Profiles = []*MemberInviteProfile{nil} },
|
|
expectedErr: "model.member.is_valid.profile_nil.app_error",
|
|
},
|
|
{
|
|
name: "duplicate usernames",
|
|
mutate: func(invite *MemberInvite) {
|
|
invite.Emails = append(invite.Emails, "user2@example.com")
|
|
invite.Profiles = append(invite.Profiles, &MemberInviteProfile{Email: "user2@example.com", Username: "USER.ONE"})
|
|
},
|
|
expectedErr: "model.member.is_valid.profile_username_duplicate.app_error",
|
|
},
|
|
{
|
|
name: "invalid username",
|
|
mutate: func(invite *MemberInvite) { invite.Profiles[0].Username = "inv@lid" },
|
|
expectedErr: "model.member.is_valid.profile_username.app_error",
|
|
},
|
|
{
|
|
name: "uppercase username",
|
|
mutate: func(invite *MemberInvite) { invite.Profiles[0].Username = "User.One" },
|
|
},
|
|
{
|
|
name: "first name too long",
|
|
mutate: func(invite *MemberInvite) {
|
|
invite.Profiles[0].FirstName = strings.Repeat("a", UserFirstNameMaxRunes+1)
|
|
},
|
|
expectedErr: "model.member.is_valid.profile_first_name.app_error",
|
|
},
|
|
{
|
|
name: "last name too long",
|
|
mutate: func(invite *MemberInvite) { invite.Profiles[0].LastName = strings.Repeat("a", UserLastNameMaxRunes+1) },
|
|
expectedErr: "model.member.is_valid.profile_last_name.app_error",
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
invite := validInvite()
|
|
if test.mutate != nil {
|
|
test.mutate(invite)
|
|
}
|
|
appErr := invite.IsValid()
|
|
if test.expectedErr == "" {
|
|
require.Nil(t, appErr)
|
|
} else {
|
|
require.NotNil(t, appErr)
|
|
require.Equal(t, test.expectedErr, appErr.Id)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMemberInviteAuditable(t *testing.T) {
|
|
invite := &MemberInvite{
|
|
Emails: []string{"user1@example.com"},
|
|
ChannelIds: []string{"channel1"},
|
|
Profiles: []*MemberInviteProfile{{Email: "user1@example.com", Username: "user.one"}},
|
|
}
|
|
auditable := invite.Auditable()
|
|
require.Equal(t, []string{"user1@example.com"}, auditable["emails"])
|
|
require.Equal(t, []string{"channel1"}, auditable["channel_ids"])
|
|
require.Equal(t, 1, auditable["profile_count"])
|
|
}
|