github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/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/v5/model"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestCreateUserWithTeam(t *testing.T) {
    14  	th := Setup(t).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  	require.True(t, found, "Failed to create User")
    37  }
    38  
    39  func TestCreateUserWithoutTeam(t *testing.T) {
    40  	th := Setup(t)
    41  	defer th.TearDown()
    42  
    43  	id := model.NewId()
    44  	email := "success+" + id + "@simulator.amazonses.com"
    45  	username := "name" + id
    46  
    47  	th.CheckCommand(t, "user", "create", "--email", email, "--password", "mypassword1", "--username", username)
    48  
    49  	user, err := th.App.Srv().Store.User().GetByEmail(email)
    50  	require.Nil(t, err)
    51  
    52  	require.Equal(t, email, user.Email)
    53  }
    54  
    55  func TestResetPassword(t *testing.T) {
    56  	th := Setup(t).InitBasic()
    57  	defer th.TearDown()
    58  
    59  	th.CheckCommand(t, "user", "password", th.BasicUser.Email, "password2")
    60  
    61  	th.Client.Logout()
    62  	th.BasicUser.Password = "password2"
    63  	th.LoginBasic()
    64  }
    65  
    66  func TestMakeUserActiveAndInactive(t *testing.T) {
    67  	th := Setup(t).InitBasic()
    68  	defer th.TearDown()
    69  
    70  	// first inactivate the user
    71  	th.CheckCommand(t, "user", "deactivate", th.BasicUser.Email)
    72  
    73  	// activate the inactive user
    74  	th.CheckCommand(t, "user", "activate", th.BasicUser.Email)
    75  }
    76  
    77  func TestChangeUserEmail(t *testing.T) {
    78  	th := Setup(t).InitBasic()
    79  	defer th.TearDown()
    80  
    81  	newEmail := model.NewId() + "@mattermost-test.com"
    82  
    83  	th.CheckCommand(t, "user", "email", th.BasicUser.Username, newEmail)
    84  	_, err := th.App.Srv().Store.User().GetByEmail(th.BasicUser.Email)
    85  	require.NotNil(t, err, "should've updated to the new email")
    86  
    87  	user, err := th.App.Srv().Store.User().GetByEmail(newEmail)
    88  	require.Nil(t, err)
    89  
    90  	require.Equal(t, user.Email, newEmail, "should've updated to the new email")
    91  
    92  	// should fail because using an invalid email
    93  	require.Error(t, th.RunCommand(t, "user", "email", th.BasicUser.Username, "wrong$email.com"))
    94  
    95  	// should fail because missing one parameter
    96  	require.Error(t, th.RunCommand(t, "user", "email", th.BasicUser.Username))
    97  
    98  	// should fail because missing both parameters
    99  	require.Error(t, th.RunCommand(t, "user", "email"))
   100  
   101  	// should fail because have more than 2  parameters
   102  	require.Error(t, th.RunCommand(t, "user", "email", th.BasicUser.Username, "new@email.com", "extra!"))
   103  
   104  	// should fail because user not found
   105  	require.Error(t, th.RunCommand(t, "user", "email", "invalidUser", newEmail))
   106  
   107  	// should fail because email already in use
   108  	require.Error(t, th.RunCommand(t, "user", "email", th.BasicUser.Username, th.BasicUser2.Email))
   109  
   110  }
   111  
   112  func TestDeleteUserBotUser(t *testing.T) {
   113  	th := Setup(t).InitBasic()
   114  	defer th.TearDown()
   115  
   116  	th.CheckCommand(t, "user", "delete", th.BasicUser.Username, "--confirm")
   117  	_, err := th.App.Srv().Store.User().Get(th.BasicUser.Id)
   118  	require.Error(t, err)
   119  
   120  	// Make a bot
   121  	bot := &model.Bot{
   122  		Username:    "bottodelete",
   123  		Description: "Delete me!",
   124  		OwnerId:     model.NewId(),
   125  	}
   126  	user, err := th.App.Srv().Store.User().Save(model.UserFromBot(bot))
   127  	require.Nil(t, err)
   128  	bot.UserId = user.Id
   129  	bot, nErr := th.App.Srv().Store.Bot().Save(bot)
   130  	require.Nil(t, nErr)
   131  
   132  	th.CheckCommand(t, "user", "delete", bot.Username, "--confirm")
   133  	_, err = th.App.Srv().Store.User().Get(user.Id)
   134  	require.Error(t, err)
   135  	_, nErr = th.App.Srv().Store.Bot().Get(user.Id, true)
   136  	require.Error(t, nErr)
   137  }
   138  
   139  func TestConvertUser(t *testing.T) {
   140  	th := Setup(t).InitBasic()
   141  	defer th.TearDown()
   142  
   143  	t.Run("Invalid command line input", func(t *testing.T) {
   144  		err := th.RunCommand(t, "user", "convert", th.BasicUser.Username)
   145  		require.NotNil(t, err)
   146  
   147  		err = th.RunCommand(t, "user", "convert", th.BasicUser.Username, "--user", "--bot")
   148  		require.NotNil(t, err)
   149  
   150  		err = th.RunCommand(t, "user", "convert", "--bot")
   151  		require.NotNil(t, err)
   152  	})
   153  
   154  	t.Run("Convert to bot from username", func(t *testing.T) {
   155  		th.CheckCommand(t, "user", "convert", th.BasicUser.Username, "anotherinvaliduser", "--bot")
   156  		_, err := th.App.Srv().Store.Bot().Get(th.BasicUser.Id, false)
   157  		require.Nil(t, err)
   158  	})
   159  
   160  	t.Run("Unable to convert to user with missing password", func(t *testing.T) {
   161  		err := th.RunCommand(t, "user", "convert", th.BasicUser.Username, "--user")
   162  		require.NotNil(t, err)
   163  	})
   164  
   165  	t.Run("Unable to convert to user with invalid email", func(t *testing.T) {
   166  		err := th.RunCommand(t, "user", "convert", th.BasicUser.Username, "--user",
   167  			"--password", "password",
   168  			"--email", "invalidEmail")
   169  		require.NotNil(t, err)
   170  	})
   171  
   172  	t.Run("Convert to user with minimum flags", func(t *testing.T) {
   173  		err := th.RunCommand(t, "user", "convert", th.BasicUser.Username, "--user",
   174  			"--password", "password")
   175  		require.Nil(t, err)
   176  		_, err = th.App.Srv().Store.Bot().Get(th.BasicUser.Id, false)
   177  		require.NotNil(t, err)
   178  	})
   179  
   180  	t.Run("Convert to bot from email", func(t *testing.T) {
   181  		th.CheckCommand(t, "user", "convert", th.BasicUser2.Email, "--bot")
   182  		_, err := th.App.Srv().Store.Bot().Get(th.BasicUser2.Id, false)
   183  		require.Nil(t, err)
   184  	})
   185  
   186  	t.Run("Convert to user with all flags", func(t *testing.T) {
   187  		err := th.RunCommand(t, "user", "convert", th.BasicUser2.Username, "--user",
   188  			"--password", "password",
   189  			"--username", "newusername",
   190  			"--email", "valid@email.com",
   191  			"--nickname", "newNickname",
   192  			"--firstname", "newFirstName",
   193  			"--lastname", "newLastName",
   194  			"--locale", "en_CA",
   195  			"--system_admin")
   196  		require.Nil(t, err)
   197  
   198  		_, err = th.App.Srv().Store.Bot().Get(th.BasicUser2.Id, false)
   199  		require.NotNil(t, err)
   200  
   201  		user, appErr := th.App.Srv().Store.User().Get(th.BasicUser2.Id)
   202  		require.Nil(t, appErr)
   203  		require.Equal(t, "newusername", user.Username)
   204  		require.Equal(t, "valid@email.com", user.Email)
   205  		require.Equal(t, "newNickname", user.Nickname)
   206  		require.Equal(t, "newFirstName", user.FirstName)
   207  		require.Equal(t, "newLastName", user.LastName)
   208  		require.Equal(t, "en_CA", user.Locale)
   209  		require.True(t, user.IsInRole("system_admin"))
   210  	})
   211  
   212  }