github.com/demisto/mattermost-server@v4.9.0-rc3+incompatible/cmd/commands/user_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package commands
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/mattermost/mattermost-server/api"
    10  	"github.com/mattermost/mattermost-server/cmd"
    11  	"github.com/mattermost/mattermost-server/model"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestCreateUserWithTeam(t *testing.T) {
    16  	th := api.Setup().InitSystemAdmin()
    17  	defer th.TearDown()
    18  
    19  	id := model.NewId()
    20  	email := "success+" + id + "@simulator.amazonses.com"
    21  	username := "name" + id
    22  
    23  	cmd.CheckCommand(t, "user", "create", "--email", email, "--password", "mypassword1", "--username", username)
    24  
    25  	cmd.CheckCommand(t, "team", "add", th.SystemAdminTeam.Id, email)
    26  
    27  	profiles := th.SystemAdminClient.Must(th.SystemAdminClient.GetProfilesInTeam(th.SystemAdminTeam.Id, 0, 1000, "")).Data.(map[string]*model.User)
    28  
    29  	found := false
    30  
    31  	for _, user := range profiles {
    32  		if user.Email == email {
    33  			found = true
    34  		}
    35  
    36  	}
    37  
    38  	if !found {
    39  		t.Fatal("Failed to create User")
    40  	}
    41  }
    42  
    43  func TestCreateUserWithoutTeam(t *testing.T) {
    44  	th := api.Setup()
    45  	defer th.TearDown()
    46  
    47  	id := model.NewId()
    48  	email := "success+" + id + "@simulator.amazonses.com"
    49  	username := "name" + id
    50  
    51  	cmd.CheckCommand(t, "user", "create", "--email", email, "--password", "mypassword1", "--username", username)
    52  
    53  	if result := <-th.App.Srv.Store.User().GetByEmail(email); result.Err != nil {
    54  		t.Fatal()
    55  	} else {
    56  		user := result.Data.(*model.User)
    57  		if user.Email != email {
    58  			t.Fatal()
    59  		}
    60  	}
    61  }
    62  
    63  func TestResetPassword(t *testing.T) {
    64  	th := api.Setup().InitBasic()
    65  	defer th.TearDown()
    66  
    67  	cmd.CheckCommand(t, "user", "password", th.BasicUser.Email, "password2")
    68  
    69  	th.BasicClient.Logout()
    70  	th.BasicUser.Password = "password2"
    71  	th.LoginBasic()
    72  }
    73  
    74  func TestMakeUserActiveAndInactive(t *testing.T) {
    75  	th := api.Setup().InitBasic()
    76  	defer th.TearDown()
    77  
    78  	// first inactivate the user
    79  	cmd.CheckCommand(t, "user", "deactivate", th.BasicUser.Email)
    80  
    81  	// activate the inactive user
    82  	cmd.CheckCommand(t, "user", "activate", th.BasicUser.Email)
    83  }
    84  
    85  func TestChangeUserEmail(t *testing.T) {
    86  	th := api.Setup().InitBasic()
    87  	defer th.TearDown()
    88  
    89  	newEmail := model.NewId() + "@mattermost-test.com"
    90  
    91  	cmd.CheckCommand(t, "user", "email", th.BasicUser.Username, newEmail)
    92  	if result := <-th.App.Srv.Store.User().GetByEmail(th.BasicUser.Email); result.Err == nil {
    93  		t.Fatal("should've updated to the new email")
    94  	}
    95  	if result := <-th.App.Srv.Store.User().GetByEmail(newEmail); result.Err != nil {
    96  		t.Fatal()
    97  	} else {
    98  		user := result.Data.(*model.User)
    99  		if user.Email != newEmail {
   100  			t.Fatal("should've updated to the new email")
   101  		}
   102  	}
   103  
   104  	// should fail because using an invalid email
   105  	require.Error(t, cmd.RunCommand(t, "user", "email", th.BasicUser.Username, "wrong$email.com"))
   106  
   107  	// should fail because user not found
   108  	require.Error(t, cmd.RunCommand(t, "user", "email", "invalidUser", newEmail))
   109  
   110  }