github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+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(result.Err)
    54  	} else {
    55  		user := result.Data.(*model.User)
    56  		require.Equal(t, email, user.Email)
    57  	}
    58  }
    59  
    60  func TestResetPassword(t *testing.T) {
    61  	th := api4.Setup().InitBasic()
    62  	defer th.TearDown()
    63  
    64  	CheckCommand(t, "user", "password", th.BasicUser.Email, "password2")
    65  
    66  	th.Client.Logout()
    67  	th.BasicUser.Password = "password2"
    68  	th.LoginBasic()
    69  }
    70  
    71  func TestMakeUserActiveAndInactive(t *testing.T) {
    72  	th := api4.Setup().InitBasic()
    73  	defer th.TearDown()
    74  
    75  	// first inactivate the user
    76  	CheckCommand(t, "user", "deactivate", th.BasicUser.Email)
    77  
    78  	// activate the inactive user
    79  	CheckCommand(t, "user", "activate", th.BasicUser.Email)
    80  }
    81  
    82  func TestChangeUserEmail(t *testing.T) {
    83  	th := api4.Setup().InitBasic()
    84  	defer th.TearDown()
    85  
    86  	newEmail := model.NewId() + "@mattermost-test.com"
    87  
    88  	CheckCommand(t, "user", "email", th.BasicUser.Username, newEmail)
    89  	if result := <-th.App.Srv.Store.User().GetByEmail(th.BasicUser.Email); result.Err == nil {
    90  		t.Fatal("should've updated to the new email")
    91  	}
    92  	if result := <-th.App.Srv.Store.User().GetByEmail(newEmail); result.Err != nil {
    93  		t.Fatal(result.Err)
    94  	} else {
    95  		user := result.Data.(*model.User)
    96  		if user.Email != newEmail {
    97  			t.Fatal("should've updated to the new email")
    98  		}
    99  	}
   100  
   101  	// should fail because using an invalid email
   102  	require.Error(t, RunCommand(t, "user", "email", th.BasicUser.Username, "wrong$email.com"))
   103  
   104  	// should fail because missing one parameter
   105  	require.Error(t, RunCommand(t, "user", "email", th.BasicUser.Username))
   106  
   107  	// should fail because missing both parameters
   108  	require.Error(t, RunCommand(t, "user", "email"))
   109  
   110  	// should fail because have more than 2  parameters
   111  	require.Error(t, RunCommand(t, "user", "email", th.BasicUser.Username, "new@email.com", "extra!"))
   112  
   113  	// should fail because user not found
   114  	require.Error(t, RunCommand(t, "user", "email", "invalidUser", newEmail))
   115  
   116  	// should fail because email already in use
   117  	require.Error(t, RunCommand(t, "user", "email", th.BasicUser.Username, th.BasicUser2.Email))
   118  
   119  }