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