mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* bots model, store and api (#9903)
* bots model, store and api
Fixes: MM-13100, MM-13101, MM-13103, MM-13105, MMM-13119
* uncomment tests incorrectly commented, and fix merge issues
* add etags support
* add missing licenses
* remove unused sqlbuilder.go (for now...)
* rejig permissions
* split out READ_BOTS into READ_BOTS and READ_OTHERS_BOTS, the latter
implicitly allowing the former
* make MANAGE_OTHERS_BOTS imply MANAGE_BOTS
* conform to general rest api pattern
* eliminate redundant http.StatusOK
* Update api4/bot.go
Co-Authored-By: lieut-data <jesse.hallam@gmail.com>
* s/model.UserFromBotModel/model.UserFromBot/g
* Update model/bot.go
Co-Authored-By: lieut-data <jesse.hallam@gmail.com>
* Update model/client4.go
Co-Authored-By: lieut-data <jesse.hallam@gmail.com>
* move sessionHasPermissionToManageBot to app/authorization.go
* use api.ApiSessionRequired for createBot
* introduce BOT_DESCRIPTION_MAX_RUNES constant
* MM-13512 Prevent getting a user by email based on privacy settings (#10021)
* MM-13512 Prevent getting a user by email based on privacy settings
* Add additional config settings to tests
* upgrade db to 5.7 (#10019)
* MM-13526 Add validation when setting a user's Locale field (#10022)
* Fix typos (#10024)
* Fixing first user being created with system admin privilages without being explicity specified. (#10014)
* Revert "Support for Embeded chat (#9129)" (#10017)
This reverts commit 3fcecd521a.
* s/DisableBot/UpdateBotActive
* add permissions on upgrade
* Update NOTICE.txt (#10054)
- add new dependency (text)
- handle switch to forked dependency (go-gomail -> go-mail)
- misc copyright owner updates
* avoid leaking bot knowledge without permission
* [GH-6798] added a new api endpoint to get the bulk reactions for posts (#10049)
* 6798 added a new api to get the bulk reactions for posts
* 6798 added the permsission check before getting the reactions
* GH-6798 added a new app function for the new endpoint
* 6798 added a store method to get reactions for multiple posts
* 6798 connected the app function with the new store function
* 6798 fixed the review comments
* MM-13559 Update model.post.is_valid.file_ids.app_error text per report (#10055)
Ticket: https://mattermost.atlassian.net/browse/MM-13559
Report: https://github.com/mattermost/mattermost-server/issues/10023
* Trigger Login Hooks with OAuth (#10061)
* make BotStore.GetAll deterministic even on duplicate CreateAt
* fix spurious TestMuteCommandSpecificChannel test failure
See
https://community-daily.mattermost.com/core/pl/px9p8s3dzbg1pf3ddrm5cr36uw
* fix race in TestExportUserChannels
* TestExportUserChannels: remove SaveMember call, as it is redundant and used to be silently failing anyway
* MM-13117: bot tokens (#10111)
* eliminate redundant Client/AdminClient declarations
* harden TestUpdateChannelScheme to API failures
* eliminate unnecessary config restoration
* minor cleanup
* make TestGenerateMfaSecret config dependency explicit
* TestCreateUserAccessToken for bots
* TestGetUserAccessToken* for bots
* leverage SessionHasPermissionToUserOrBot for user token APIs
* Test(Revoke|Disable|Enable)UserAccessToken
* make EnableUserAccessTokens explicit, so as to not rely on local config.json
* uncomment TestResetPassword, but still skip
* mark assert(Invalid)Token as helper
* fix whitespace issues
* fix mangled comments
* MM-13116: bot plugin api (#10113)
* MM-13117: expose bot API to plugins
This also changes the `CreatorId` column definition to allow for plugin
ids, as the default unless the plugin overrides is to use the plugin id
here. This branch hasn't hit master yet, so no migration needed.
* gofmt issues
* expunge use of BotList in plugin/client API
* introduce model.BotGetOptions
* use botUserId term for clarity
* MM-13129 Adding functionality to deal with orphaned bots (#10238)
* Add way to list orphaned bots.
* Add /assign route to modify ownership of bot accounts.
* Apply suggestions from code review
Co-Authored-By: crspeller <crspeller@gmail.com>
* MM-13120: add IsBot field to returned user objects (#10103)
* MM-13104: forbid bot login (#10251)
* MM-13104: disallow bot login
* fix shadowing
* MM-13136 Disable user bots when user is disabled. (#10293)
* Disable user bots when user is disabled.
* Grammer.
Co-Authored-By: crspeller <crspeller@gmail.com>
* Fixing bot branch for test changes.
* Don't use external dependancies in bot plugin tests.
* Rename bot CreatorId to OwnerId
* Adding ability to re-enable bots
* Fixing IsBot to not attempt to be saved to DB.
* Adding diagnostics and licencing counting for bot accounts.
* Modifying gorp to allow reading of '-' fields.
* Removing unnessisary nil values from UserCountOptions.
* Changing comment to GoDoc format
* Improving user count SQL
* Some improvments from feedback.
* Omit empty on User.IsBot
707 lines
19 KiB
Go
707 lines
19 KiB
Go
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
|
// See License.txt for license information.
|
|
|
|
package app
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"image"
|
|
"image/color"
|
|
"math/rand"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mattermost/mattermost-server/einterfaces"
|
|
"github.com/mattermost/mattermost-server/model"
|
|
oauthgitlab "github.com/mattermost/mattermost-server/model/gitlab"
|
|
)
|
|
|
|
func TestIsUsernameTaken(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
user := th.BasicUser
|
|
taken := th.App.IsUsernameTaken(user.Username)
|
|
|
|
if !taken {
|
|
t.Logf("the username '%v' should be taken", user.Username)
|
|
t.FailNow()
|
|
}
|
|
|
|
newUsername := "randomUsername"
|
|
taken = th.App.IsUsernameTaken(newUsername)
|
|
|
|
if taken {
|
|
t.Logf("the username '%v' should not be taken", newUsername)
|
|
t.FailNow()
|
|
}
|
|
}
|
|
|
|
func TestCheckUserDomain(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
user := th.BasicUser
|
|
|
|
cases := []struct {
|
|
domains string
|
|
matched bool
|
|
}{
|
|
{"simulator.amazonses.com", true},
|
|
{"gmail.com", false},
|
|
{"", true},
|
|
{"gmail.com simulator.amazonses.com", true},
|
|
}
|
|
for _, c := range cases {
|
|
matched := CheckUserDomain(user, c.domains)
|
|
if matched != c.matched {
|
|
if c.matched {
|
|
t.Logf("'%v' should have matched '%v'", user.Email, c.domains)
|
|
} else {
|
|
t.Logf("'%v' should not have matched '%v'", user.Email, c.domains)
|
|
}
|
|
t.FailNow()
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCreateOAuthUser(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
glUser := oauthgitlab.GitLabUser{Id: int64(r.Intn(1000)) + 1, Username: "o" + model.NewId(), Email: model.NewId() + "@simulator.amazonses.com", Name: "Joram Wilander"}
|
|
|
|
json := glUser.ToJson()
|
|
user, err := th.App.CreateOAuthUser(model.USER_AUTH_SERVICE_GITLAB, strings.NewReader(json), th.BasicTeam.Id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if user.Username != glUser.Username {
|
|
t.Fatal("usernames didn't match")
|
|
}
|
|
|
|
th.App.PermanentDeleteUser(user)
|
|
|
|
*th.App.Config().TeamSettings.EnableUserCreation = false
|
|
|
|
_, err = th.App.CreateOAuthUser(model.USER_AUTH_SERVICE_GITLAB, strings.NewReader(json), th.BasicTeam.Id)
|
|
if err == nil {
|
|
t.Fatal("should have failed - user creation disabled")
|
|
}
|
|
}
|
|
|
|
func TestCreateProfileImage(t *testing.T) {
|
|
b, err := CreateProfileImage("Corey Hulen", "eo1zkdr96pdj98pjmq8zy35wba", "nunito-bold.ttf")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
rdr := bytes.NewReader(b)
|
|
img, _, err2 := image.Decode(rdr)
|
|
if err2 != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
colorful := color.RGBA{116, 49, 196, 255}
|
|
|
|
if img.At(1, 1) != colorful {
|
|
t.Fatal("Failed to create correct color")
|
|
}
|
|
}
|
|
|
|
func TestSetDefaultProfileImage(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
err := th.App.SetDefaultProfileImage(&model.User{
|
|
Id: model.NewId(),
|
|
Username: "notvaliduser",
|
|
})
|
|
require.Error(t, err)
|
|
|
|
user := th.BasicUser
|
|
|
|
err = th.App.SetDefaultProfileImage(user)
|
|
require.Nil(t, err)
|
|
|
|
user = getUserFromDB(th.App, user.Id, t)
|
|
assert.Equal(t, int64(0), user.LastPictureUpdate)
|
|
}
|
|
|
|
func TestUpdateUserToRestrictedDomain(t *testing.T) {
|
|
th := Setup(t)
|
|
defer th.TearDown()
|
|
|
|
user := th.CreateUser()
|
|
defer th.App.PermanentDeleteUser(user)
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
*cfg.TeamSettings.RestrictCreationToDomains = "foo.com"
|
|
})
|
|
|
|
_, err := th.App.UpdateUser(user, false)
|
|
assert.True(t, err == nil)
|
|
|
|
user.Email = "asdf@ghjk.l"
|
|
_, err = th.App.UpdateUser(user, false)
|
|
assert.False(t, err == nil)
|
|
}
|
|
|
|
func TestUpdateUserActive(t *testing.T) {
|
|
th := Setup(t)
|
|
defer th.TearDown()
|
|
|
|
user := th.CreateUser()
|
|
|
|
EnableUserDeactivation := th.App.Config().TeamSettings.EnableUserDeactivation
|
|
defer func() {
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.EnableUserDeactivation = EnableUserDeactivation })
|
|
}()
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
*cfg.TeamSettings.EnableUserDeactivation = true
|
|
})
|
|
err := th.App.UpdateUserActive(user.Id, false)
|
|
assert.Nil(t, err)
|
|
}
|
|
|
|
func TestUpdateActiveBotsSideEffect(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
bot, err := th.App.CreateBot(&model.Bot{
|
|
Username: "username",
|
|
Description: "a bot",
|
|
OwnerId: th.BasicUser.Id,
|
|
})
|
|
require.Nil(t, err)
|
|
defer th.App.PermanentDeleteBot(bot.UserId)
|
|
|
|
// Automatic deactivation disabled
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
*cfg.ServiceSettings.DisableBotsWhenOwnerIsDeactivated = false
|
|
})
|
|
|
|
th.App.UpdateActive(th.BasicUser, false)
|
|
|
|
retbot1, err := th.App.GetBot(bot.UserId, true)
|
|
require.Nil(t, err)
|
|
require.Zero(t, retbot1.DeleteAt)
|
|
user1, err := th.App.GetUser(bot.UserId)
|
|
require.Nil(t, err)
|
|
require.Zero(t, user1.DeleteAt)
|
|
|
|
th.App.UpdateActive(th.BasicUser, true)
|
|
|
|
// Automatic deactivation enabled
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
*cfg.ServiceSettings.DisableBotsWhenOwnerIsDeactivated = true
|
|
})
|
|
|
|
th.App.UpdateActive(th.BasicUser, false)
|
|
|
|
retbot2, err := th.App.GetBot(bot.UserId, true)
|
|
require.Nil(t, err)
|
|
require.NotZero(t, retbot2.DeleteAt)
|
|
user2, err := th.App.GetUser(bot.UserId)
|
|
require.Nil(t, err)
|
|
require.NotZero(t, user2.DeleteAt)
|
|
|
|
th.App.UpdateActive(th.BasicUser, true)
|
|
}
|
|
|
|
func TestUpdateOAuthUserAttrs(t *testing.T) {
|
|
th := Setup(t)
|
|
defer th.TearDown()
|
|
|
|
id := model.NewId()
|
|
id2 := model.NewId()
|
|
gitlabProvider := einterfaces.GetOauthProvider("gitlab")
|
|
|
|
username := "user" + id
|
|
username2 := "user" + id2
|
|
|
|
email := "user" + id + "@nowhere.com"
|
|
email2 := "user" + id2 + "@nowhere.com"
|
|
|
|
var user, user2 *model.User
|
|
var gitlabUserObj oauthgitlab.GitLabUser
|
|
user, gitlabUserObj = createGitlabUser(t, th.App, username, email)
|
|
user2, _ = createGitlabUser(t, th.App, username2, email2)
|
|
|
|
t.Run("UpdateUsername", func(t *testing.T) {
|
|
t.Run("NoExistingUserWithSameUsername", func(t *testing.T) {
|
|
gitlabUserObj.Username = "updateduser" + model.NewId()
|
|
gitlabUser := getGitlabUserPayload(gitlabUserObj, t)
|
|
data := bytes.NewReader(gitlabUser)
|
|
|
|
user = getUserFromDB(th.App, user.Id, t)
|
|
th.App.UpdateOAuthUserAttrs(data, user, gitlabProvider, "gitlab")
|
|
user = getUserFromDB(th.App, user.Id, t)
|
|
|
|
if user.Username != gitlabUserObj.Username {
|
|
t.Fatal("user's username is not updated")
|
|
}
|
|
})
|
|
|
|
t.Run("ExistinguserWithSameUsername", func(t *testing.T) {
|
|
gitlabUserObj.Username = user2.Username
|
|
|
|
gitlabUser := getGitlabUserPayload(gitlabUserObj, t)
|
|
data := bytes.NewReader(gitlabUser)
|
|
|
|
user = getUserFromDB(th.App, user.Id, t)
|
|
th.App.UpdateOAuthUserAttrs(data, user, gitlabProvider, "gitlab")
|
|
user = getUserFromDB(th.App, user.Id, t)
|
|
|
|
if user.Username == gitlabUserObj.Username {
|
|
t.Fatal("user's username is updated though there already exists another user with the same username")
|
|
}
|
|
})
|
|
})
|
|
|
|
t.Run("UpdateEmail", func(t *testing.T) {
|
|
t.Run("NoExistingUserWithSameEmail", func(t *testing.T) {
|
|
gitlabUserObj.Email = "newuser" + model.NewId() + "@nowhere.com"
|
|
gitlabUser := getGitlabUserPayload(gitlabUserObj, t)
|
|
data := bytes.NewReader(gitlabUser)
|
|
|
|
user = getUserFromDB(th.App, user.Id, t)
|
|
th.App.UpdateOAuthUserAttrs(data, user, gitlabProvider, "gitlab")
|
|
user = getUserFromDB(th.App, user.Id, t)
|
|
|
|
if user.Email != gitlabUserObj.Email {
|
|
t.Fatal("user's email is not updated")
|
|
}
|
|
|
|
if !user.EmailVerified {
|
|
t.Fatal("user's email should have been verified")
|
|
}
|
|
})
|
|
|
|
t.Run("ExistingUserWithSameEmail", func(t *testing.T) {
|
|
gitlabUserObj.Email = user2.Email
|
|
|
|
gitlabUser := getGitlabUserPayload(gitlabUserObj, t)
|
|
data := bytes.NewReader(gitlabUser)
|
|
|
|
user = getUserFromDB(th.App, user.Id, t)
|
|
th.App.UpdateOAuthUserAttrs(data, user, gitlabProvider, "gitlab")
|
|
user = getUserFromDB(th.App, user.Id, t)
|
|
|
|
if user.Email == gitlabUserObj.Email {
|
|
t.Fatal("user's email is updated though there already exists another user with the same email")
|
|
}
|
|
})
|
|
})
|
|
|
|
t.Run("UpdateFirstName", func(t *testing.T) {
|
|
gitlabUserObj.Name = "Updated User"
|
|
gitlabUser := getGitlabUserPayload(gitlabUserObj, t)
|
|
data := bytes.NewReader(gitlabUser)
|
|
|
|
user = getUserFromDB(th.App, user.Id, t)
|
|
th.App.UpdateOAuthUserAttrs(data, user, gitlabProvider, "gitlab")
|
|
user = getUserFromDB(th.App, user.Id, t)
|
|
|
|
if user.FirstName != "Updated" {
|
|
t.Fatal("user's first name is not updated")
|
|
}
|
|
})
|
|
|
|
t.Run("UpdateLastName", func(t *testing.T) {
|
|
gitlabUserObj.Name = "Updated Lastname"
|
|
gitlabUser := getGitlabUserPayload(gitlabUserObj, t)
|
|
data := bytes.NewReader(gitlabUser)
|
|
|
|
user = getUserFromDB(th.App, user.Id, t)
|
|
th.App.UpdateOAuthUserAttrs(data, user, gitlabProvider, "gitlab")
|
|
user = getUserFromDB(th.App, user.Id, t)
|
|
|
|
if user.LastName != "Lastname" {
|
|
t.Fatal("user's last name is not updated")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestUpdateUserEmail(t *testing.T) {
|
|
th := Setup(t)
|
|
defer th.TearDown()
|
|
|
|
user := th.CreateUser()
|
|
|
|
t.Run("RequireVerification", func(t *testing.T) {
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
*cfg.EmailSettings.RequireEmailVerification = true
|
|
})
|
|
|
|
currentEmail := user.Email
|
|
newEmail := th.MakeEmail()
|
|
|
|
user.Email = newEmail
|
|
user2, err := th.App.UpdateUser(user, false)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, currentEmail, user2.Email)
|
|
assert.True(t, user2.EmailVerified)
|
|
|
|
token, err := th.App.CreateVerifyEmailToken(user2.Id, newEmail)
|
|
assert.Nil(t, err)
|
|
|
|
err = th.App.VerifyEmailFromToken(token.Token)
|
|
assert.Nil(t, err)
|
|
|
|
user2, err = th.App.GetUser(user2.Id)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, newEmail, user2.Email)
|
|
assert.True(t, user2.EmailVerified)
|
|
})
|
|
|
|
t.Run("RequireVerificationAlreadyUsedEmail", func(t *testing.T) {
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
*cfg.EmailSettings.RequireEmailVerification = true
|
|
})
|
|
|
|
user2 := th.CreateUser()
|
|
newEmail := user2.Email
|
|
|
|
user.Email = newEmail
|
|
user3, err := th.App.UpdateUser(user, false)
|
|
assert.NotNil(t, err)
|
|
assert.Nil(t, user3)
|
|
})
|
|
|
|
t.Run("NoVerification", func(t *testing.T) {
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
*cfg.EmailSettings.RequireEmailVerification = false
|
|
})
|
|
|
|
newEmail := th.MakeEmail()
|
|
|
|
user.Email = newEmail
|
|
user2, err := th.App.UpdateUser(user, false)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, newEmail, user2.Email)
|
|
})
|
|
}
|
|
|
|
func getUserFromDB(a *App, id string, t *testing.T) *model.User {
|
|
user, err := a.GetUser(id)
|
|
if err != nil {
|
|
t.Fatal("user is not found", err)
|
|
return nil
|
|
}
|
|
return user
|
|
}
|
|
|
|
func getGitlabUserPayload(gitlabUser oauthgitlab.GitLabUser, t *testing.T) []byte {
|
|
var payload []byte
|
|
var err error
|
|
if payload, err = json.Marshal(gitlabUser); err != nil {
|
|
t.Fatal("Serialization of gitlab user to json failed", err)
|
|
}
|
|
|
|
return payload
|
|
}
|
|
|
|
func createGitlabUser(t *testing.T, a *App, username string, email string) (*model.User, oauthgitlab.GitLabUser) {
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
gitlabUserObj := oauthgitlab.GitLabUser{Id: int64(r.Intn(1000)) + 1, Username: username, Login: "user1", Email: email, Name: "Test User"}
|
|
gitlabUser := getGitlabUserPayload(gitlabUserObj, t)
|
|
|
|
var user *model.User
|
|
var err *model.AppError
|
|
|
|
if user, err = a.CreateOAuthUser("gitlab", bytes.NewReader(gitlabUser), ""); err != nil {
|
|
t.Fatal("unable to create the user", err)
|
|
}
|
|
|
|
return user, gitlabUserObj
|
|
}
|
|
|
|
func TestGetUsersByStatus(t *testing.T) {
|
|
th := Setup(t)
|
|
defer th.TearDown()
|
|
|
|
team := th.CreateTeam()
|
|
channel, err := th.App.CreateChannel(&model.Channel{
|
|
DisplayName: "dn_" + model.NewId(),
|
|
Name: "name_" + model.NewId(),
|
|
Type: model.CHANNEL_OPEN,
|
|
TeamId: team.Id,
|
|
CreatorId: model.NewId(),
|
|
}, false)
|
|
if err != nil {
|
|
t.Fatalf("failed to create channel: %v", err)
|
|
}
|
|
|
|
createUserWithStatus := func(username string, status string) *model.User {
|
|
id := model.NewId()
|
|
|
|
user, err := th.App.CreateUser(&model.User{
|
|
Email: "success+" + id + "@simulator.amazonses.com",
|
|
Username: "un_" + username + "_" + id,
|
|
Nickname: "nn_" + id,
|
|
Password: "Password1",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("failed to create user: %v", err)
|
|
}
|
|
|
|
th.LinkUserToTeam(user, team)
|
|
th.AddUserToChannel(user, channel)
|
|
|
|
th.App.SaveAndBroadcastStatus(&model.Status{
|
|
UserId: user.Id,
|
|
Status: status,
|
|
Manual: true,
|
|
})
|
|
|
|
return user
|
|
}
|
|
|
|
// Creating these out of order in case that affects results
|
|
awayUser1 := createUserWithStatus("away1", model.STATUS_AWAY)
|
|
awayUser2 := createUserWithStatus("away2", model.STATUS_AWAY)
|
|
dndUser1 := createUserWithStatus("dnd1", model.STATUS_DND)
|
|
dndUser2 := createUserWithStatus("dnd2", model.STATUS_DND)
|
|
offlineUser1 := createUserWithStatus("offline1", model.STATUS_OFFLINE)
|
|
offlineUser2 := createUserWithStatus("offline2", model.STATUS_OFFLINE)
|
|
onlineUser1 := createUserWithStatus("online1", model.STATUS_ONLINE)
|
|
onlineUser2 := createUserWithStatus("online2", model.STATUS_ONLINE)
|
|
|
|
t.Run("sorting by status then alphabetical", func(t *testing.T) {
|
|
usersByStatus, err := th.App.GetUsersInChannelPageByStatus(channel.Id, 0, 8, true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
expectedUsersByStatus := []*model.User{
|
|
onlineUser1,
|
|
onlineUser2,
|
|
awayUser1,
|
|
awayUser2,
|
|
dndUser1,
|
|
dndUser2,
|
|
offlineUser1,
|
|
offlineUser2,
|
|
}
|
|
|
|
if len(usersByStatus) != len(expectedUsersByStatus) {
|
|
t.Fatalf("received only %v users, expected %v", len(usersByStatus), len(expectedUsersByStatus))
|
|
}
|
|
|
|
for i := range usersByStatus {
|
|
if usersByStatus[i].Id != expectedUsersByStatus[i].Id {
|
|
t.Fatalf("received user %v at index %v, expected %v", usersByStatus[i].Username, i, expectedUsersByStatus[i].Username)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("paging", func(t *testing.T) {
|
|
usersByStatus, err := th.App.GetUsersInChannelPageByStatus(channel.Id, 0, 3, true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if len(usersByStatus) != 3 {
|
|
t.Fatal("received too many users")
|
|
}
|
|
|
|
if usersByStatus[0].Id != onlineUser1.Id && usersByStatus[1].Id != onlineUser2.Id {
|
|
t.Fatal("expected to receive online users first")
|
|
}
|
|
|
|
if usersByStatus[2].Id != awayUser1.Id {
|
|
t.Fatal("expected to receive away users second")
|
|
}
|
|
|
|
usersByStatus, err = th.App.GetUsersInChannelPageByStatus(channel.Id, 1, 3, true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if usersByStatus[0].Id != awayUser2.Id {
|
|
t.Fatal("expected to receive away users second")
|
|
}
|
|
|
|
if usersByStatus[1].Id != dndUser1.Id && usersByStatus[2].Id != dndUser2.Id {
|
|
t.Fatal("expected to receive dnd users third")
|
|
}
|
|
|
|
usersByStatus, err = th.App.GetUsersInChannelPageByStatus(channel.Id, 1, 4, true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if len(usersByStatus) != 4 {
|
|
t.Fatal("received too many users")
|
|
}
|
|
|
|
if usersByStatus[0].Id != dndUser1.Id && usersByStatus[1].Id != dndUser2.Id {
|
|
t.Fatal("expected to receive dnd users third")
|
|
}
|
|
|
|
if usersByStatus[2].Id != offlineUser1.Id && usersByStatus[3].Id != offlineUser2.Id {
|
|
t.Fatal("expected to receive offline users last")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestCreateUserWithToken(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""}
|
|
|
|
t.Run("invalid token", func(t *testing.T) {
|
|
if _, err := th.App.CreateUserWithToken(&user, "123"); err == nil {
|
|
t.Fatal("Should fail on unexisting token")
|
|
}
|
|
})
|
|
|
|
t.Run("invalid token type", func(t *testing.T) {
|
|
token := model.NewToken(
|
|
TOKEN_TYPE_VERIFY_EMAIL,
|
|
model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id, "email": user.Email}),
|
|
)
|
|
<-th.App.Srv.Store.Token().Save(token)
|
|
defer th.App.DeleteToken(token)
|
|
if _, err := th.App.CreateUserWithToken(&user, token.Token); err == nil {
|
|
t.Fatal("Should fail on bad token type")
|
|
}
|
|
})
|
|
|
|
t.Run("expired token", func(t *testing.T) {
|
|
token := model.NewToken(
|
|
TOKEN_TYPE_TEAM_INVITATION,
|
|
model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id, "email": user.Email}),
|
|
)
|
|
token.CreateAt = model.GetMillis() - TEAM_INVITATION_EXPIRY_TIME - 1
|
|
<-th.App.Srv.Store.Token().Save(token)
|
|
defer th.App.DeleteToken(token)
|
|
if _, err := th.App.CreateUserWithToken(&user, token.Token); err == nil {
|
|
t.Fatal("Should fail on expired token")
|
|
}
|
|
})
|
|
|
|
t.Run("invalid team id", func(t *testing.T) {
|
|
token := model.NewToken(
|
|
TOKEN_TYPE_TEAM_INVITATION,
|
|
model.MapToJson(map[string]string{"teamId": model.NewId(), "email": user.Email}),
|
|
)
|
|
<-th.App.Srv.Store.Token().Save(token)
|
|
defer th.App.DeleteToken(token)
|
|
if _, err := th.App.CreateUserWithToken(&user, token.Token); err == nil {
|
|
t.Fatal("Should fail on bad team id")
|
|
}
|
|
})
|
|
|
|
t.Run("valid request", func(t *testing.T) {
|
|
invitationEmail := model.NewId() + "other-email@test.com"
|
|
token := model.NewToken(
|
|
TOKEN_TYPE_TEAM_INVITATION,
|
|
model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id, "email": invitationEmail}),
|
|
)
|
|
<-th.App.Srv.Store.Token().Save(token)
|
|
newUser, err := th.App.CreateUserWithToken(&user, token.Token)
|
|
if err != nil {
|
|
t.Log(err)
|
|
t.Fatal("Should add user to the team")
|
|
}
|
|
if newUser.Email != invitationEmail {
|
|
t.Fatal("The user email must be the invitation one")
|
|
}
|
|
if result := <-th.App.Srv.Store.Token().GetByToken(token.Token); result.Err == nil {
|
|
t.Fatal("The token must be deleted after be used")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestPermanentDeleteUser(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
b := []byte("testimage")
|
|
|
|
finfo, err := th.App.DoUploadFile(time.Now(), th.BasicTeam.Id, th.BasicChannel.Id, th.BasicUser.Id, "testfile.txt", b)
|
|
|
|
if err != nil {
|
|
t.Log(err)
|
|
t.Fatal("Unable to upload file")
|
|
}
|
|
|
|
err = th.App.PermanentDeleteUser(th.BasicUser)
|
|
if err != nil {
|
|
t.Log(err)
|
|
t.Fatal("Unable to delete user")
|
|
}
|
|
|
|
res, err := th.App.FileExists(finfo.Path)
|
|
|
|
if err != nil {
|
|
t.Log(err)
|
|
t.Fatal("Unable to check whether file exists")
|
|
}
|
|
|
|
if res {
|
|
t.Log(err)
|
|
t.Fatal("File was not deleted on FS")
|
|
}
|
|
|
|
finfo, err = th.App.GetFileInfo(finfo.Id)
|
|
|
|
if finfo != nil {
|
|
t.Log(err)
|
|
t.Fatal("Unable to find finfo")
|
|
}
|
|
|
|
if err == nil {
|
|
t.Log(err)
|
|
t.Fatal("GetFileInfo after DeleteUser is nil")
|
|
}
|
|
}
|
|
|
|
func TestPasswordRecovery(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
token, err := th.App.CreatePasswordRecoveryToken(th.BasicUser.Id, th.BasicUser.Email)
|
|
assert.Nil(t, err)
|
|
|
|
tokenData := struct {
|
|
UserId string
|
|
Email string
|
|
}{}
|
|
|
|
err2 := json.Unmarshal([]byte(token.Extra), &tokenData)
|
|
assert.Nil(t, err2)
|
|
assert.Equal(t, th.BasicUser.Id, tokenData.UserId)
|
|
assert.Equal(t, th.BasicUser.Email, tokenData.Email)
|
|
|
|
// Password token with same eMail as during creation
|
|
err = th.App.ResetPasswordFromToken(token.Token, "abcdefgh")
|
|
assert.Nil(t, err)
|
|
|
|
// Password token with modified eMail after creation
|
|
token, err = th.App.CreatePasswordRecoveryToken(th.BasicUser.Id, th.BasicUser.Email)
|
|
assert.Nil(t, err)
|
|
|
|
th.App.UpdateConfig(func(c *model.Config) {
|
|
*c.EmailSettings.RequireEmailVerification = false
|
|
})
|
|
|
|
th.BasicUser.Email = th.MakeEmail()
|
|
_, err = th.App.UpdateUser(th.BasicUser, false)
|
|
assert.Nil(t, err)
|
|
|
|
err = th.App.ResetPasswordFromToken(token.Token, "abcdefgh")
|
|
assert.NotNil(t, err)
|
|
}
|