github.com/cryptomisa/mattermost-server@v5.11.1+incompatible/cmd/mattermost/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/model"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestCreateUserWithTeam(t *testing.T) {
    14  	th := Setup().InitBasic()
    15  	defer th.TearDown()
    16  
    17  	id := model.NewId()
    18  	email := "success+" + id + "@simulator.amazonses.com"
    19  	username := "name" + id
    20  
    21  	th.CheckCommand(t, "user", "create", "--email", email, "--password", "mypassword1", "--username", username)
    22  
    23  	th.CheckCommand(t, "team", "add", th.BasicTeam.Id, email)
    24  
    25  	profiles := th.SystemAdminClient.Must(th.SystemAdminClient.GetUsersInTeam(th.BasicTeam.Id, 0, 1000, "")).([]*model.User)
    26  
    27  	found := false
    28  
    29  	for _, user := range profiles {
    30  		if user.Email == email {
    31  			found = true
    32  		}
    33  
    34  	}
    35  
    36  	if !found {
    37  		t.Fatal("Failed to create User")
    38  	}
    39  }
    40  
    41  func TestCreateUserWithoutTeam(t *testing.T) {
    42  	th := Setup()
    43  	defer th.TearDown()
    44  
    45  	id := model.NewId()
    46  	email := "success+" + id + "@simulator.amazonses.com"
    47  	username := "name" + id
    48  
    49  	th.CheckCommand(t, "user", "create", "--email", email, "--password", "mypassword1", "--username", username)
    50  
    51  	if result := <-th.App.Srv.Store.User().GetByEmail(email); result.Err != nil {
    52  		t.Fatal(result.Err)
    53  	} else {
    54  		user := result.Data.(*model.User)
    55  		require.Equal(t, email, user.Email)
    56  	}
    57  }
    58  
    59  func TestResetPassword(t *testing.T) {
    60  	th := Setup().InitBasic()
    61  	defer th.TearDown()
    62  
    63  	th.CheckCommand(t, "user", "password", th.BasicUser.Email, "password2")
    64  
    65  	th.Client.Logout()
    66  	th.BasicUser.Password = "password2"
    67  	th.LoginBasic()
    68  }
    69  
    70  func TestMakeUserActiveAndInactive(t *testing.T) {
    71  	th := Setup().InitBasic()
    72  	defer th.TearDown()
    73  
    74  	// first inactivate the user
    75  	th.CheckCommand(t, "user", "deactivate", th.BasicUser.Email)
    76  
    77  	// activate the inactive user
    78  	th.CheckCommand(t, "user", "activate", th.BasicUser.Email)
    79  }
    80  
    81  func TestChangeUserEmail(t *testing.T) {
    82  	th := Setup().InitBasic()
    83  	defer th.TearDown()
    84  
    85  	newEmail := model.NewId() + "@mattermost-test.com"
    86  
    87  	th.CheckCommand(t, "user", "email", th.BasicUser.Username, newEmail)
    88  	if result := <-th.App.Srv.Store.User().GetByEmail(th.BasicUser.Email); result.Err == nil {
    89  		t.Fatal("should've updated to the new email")
    90  	}
    91  	if result := <-th.App.Srv.Store.User().GetByEmail(newEmail); result.Err != nil {
    92  		t.Fatal(result.Err)
    93  	} else {
    94  		user := result.Data.(*model.User)
    95  		if user.Email != newEmail {
    96  			t.Fatal("should've updated to the new email")
    97  		}
    98  	}
    99  
   100  	// should fail because using an invalid email
   101  	require.Error(t, th.RunCommand(t, "user", "email", th.BasicUser.Username, "wrong$email.com"))
   102  
   103  	// should fail because missing one parameter
   104  	require.Error(t, th.RunCommand(t, "user", "email", th.BasicUser.Username))
   105  
   106  	// should fail because missing both parameters
   107  	require.Error(t, th.RunCommand(t, "user", "email"))
   108  
   109  	// should fail because have more than 2  parameters
   110  	require.Error(t, th.RunCommand(t, "user", "email", th.BasicUser.Username, "new@email.com", "extra!"))
   111  
   112  	// should fail because user not found
   113  	require.Error(t, th.RunCommand(t, "user", "email", "invalidUser", newEmail))
   114  
   115  	// should fail because email already in use
   116  	require.Error(t, th.RunCommand(t, "user", "email", th.BasicUser.Username, th.BasicUser2.Email))
   117  
   118  }