github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/storetest/user_access_token_store.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package storetest
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/mattermost/mattermost-server/v5/model"
    12  	"github.com/mattermost/mattermost-server/v5/store"
    13  )
    14  
    15  func TestUserAccessTokenStore(t *testing.T, ss store.Store) {
    16  	t.Run("UserAccessTokenSaveGetDelete", func(t *testing.T) { testUserAccessTokenSaveGetDelete(t, ss) })
    17  	t.Run("UserAccessTokenDisableEnable", func(t *testing.T) { testUserAccessTokenDisableEnable(t, ss) })
    18  	t.Run("UserAccessTokenSearch", func(t *testing.T) { testUserAccessTokenSearch(t, ss) })
    19  }
    20  
    21  func testUserAccessTokenSaveGetDelete(t *testing.T, ss store.Store) {
    22  	uat := &model.UserAccessToken{
    23  		Token:       model.NewId(),
    24  		UserId:      model.NewId(),
    25  		Description: "testtoken",
    26  	}
    27  
    28  	s1 := &model.Session{}
    29  	s1.UserId = uat.UserId
    30  	s1.Token = uat.Token
    31  
    32  	s1, err := ss.Session().Save(s1)
    33  	require.NoError(t, err)
    34  
    35  	_, nErr := ss.UserAccessToken().Save(uat)
    36  	require.NoError(t, nErr)
    37  
    38  	result, terr := ss.UserAccessToken().Get(uat.Id)
    39  	require.NoError(t, terr)
    40  	require.Equal(t, result.Token, uat.Token, "received incorrect token after save")
    41  
    42  	received, err2 := ss.UserAccessToken().GetByToken(uat.Token)
    43  	require.NoError(t, err2)
    44  	require.Equal(t, received.Token, uat.Token, "received incorrect token after save")
    45  
    46  	_, nErr = ss.UserAccessToken().GetByToken("notarealtoken")
    47  	require.Error(t, nErr, "should have failed on bad token")
    48  
    49  	received2, err2 := ss.UserAccessToken().GetByUser(uat.UserId, 0, 100)
    50  	require.NoError(t, err2)
    51  	require.Equal(t, 1, len(received2), "received incorrect number of tokens after save")
    52  
    53  	result2, err := ss.UserAccessToken().GetAll(0, 100)
    54  	require.NoError(t, err)
    55  	require.Equal(t, 1, len(result2), "received incorrect number of tokens after save")
    56  
    57  	nErr = ss.UserAccessToken().Delete(uat.Id)
    58  	require.NoError(t, nErr)
    59  
    60  	_, err = ss.Session().Get(s1.Token)
    61  	require.Error(t, err, "should error - session should be deleted")
    62  
    63  	_, nErr = ss.UserAccessToken().GetByToken(s1.Token)
    64  	require.Error(t, nErr, "should error - access token should be deleted")
    65  
    66  	s2 := &model.Session{}
    67  	s2.UserId = uat.UserId
    68  	s2.Token = uat.Token
    69  
    70  	s2, err = ss.Session().Save(s2)
    71  	require.NoError(t, err)
    72  
    73  	_, nErr = ss.UserAccessToken().Save(uat)
    74  	require.NoError(t, nErr)
    75  
    76  	nErr = ss.UserAccessToken().DeleteAllForUser(uat.UserId)
    77  	require.NoError(t, nErr)
    78  
    79  	_, err = ss.Session().Get(s2.Token)
    80  	require.Error(t, err, "should error - session should be deleted")
    81  
    82  	_, nErr = ss.UserAccessToken().GetByToken(s2.Token)
    83  	require.Error(t, nErr, "should error - access token should be deleted")
    84  }
    85  
    86  func testUserAccessTokenDisableEnable(t *testing.T, ss store.Store) {
    87  	uat := &model.UserAccessToken{
    88  		Token:       model.NewId(),
    89  		UserId:      model.NewId(),
    90  		Description: "testtoken",
    91  	}
    92  
    93  	s1 := &model.Session{}
    94  	s1.UserId = uat.UserId
    95  	s1.Token = uat.Token
    96  
    97  	s1, err := ss.Session().Save(s1)
    98  	require.NoError(t, err)
    99  
   100  	_, nErr := ss.UserAccessToken().Save(uat)
   101  	require.NoError(t, nErr)
   102  
   103  	nErr = ss.UserAccessToken().UpdateTokenDisable(uat.Id)
   104  	require.NoError(t, nErr)
   105  
   106  	_, err = ss.Session().Get(s1.Token)
   107  	require.Error(t, err, "should error - session should be deleted")
   108  
   109  	s2 := &model.Session{}
   110  	s2.UserId = uat.UserId
   111  	s2.Token = uat.Token
   112  
   113  	s2, err = ss.Session().Save(s2)
   114  	require.NoError(t, err)
   115  
   116  	nErr = ss.UserAccessToken().UpdateTokenEnable(uat.Id)
   117  	require.NoError(t, nErr)
   118  }
   119  
   120  func testUserAccessTokenSearch(t *testing.T, ss store.Store) {
   121  	u1 := model.User{}
   122  	u1.Email = MakeEmail()
   123  	u1.Username = model.NewId()
   124  
   125  	_, err := ss.User().Save(&u1)
   126  	require.NoError(t, err)
   127  
   128  	uat := &model.UserAccessToken{
   129  		Token:       model.NewId(),
   130  		UserId:      u1.Id,
   131  		Description: "testtoken",
   132  	}
   133  
   134  	s1 := &model.Session{}
   135  	s1.UserId = uat.UserId
   136  	s1.Token = uat.Token
   137  
   138  	s1, nErr := ss.Session().Save(s1)
   139  	require.NoError(t, nErr)
   140  
   141  	_, nErr = ss.UserAccessToken().Save(uat)
   142  	require.NoError(t, nErr)
   143  
   144  	received, nErr := ss.UserAccessToken().Search(uat.Id)
   145  	require.NoError(t, nErr)
   146  
   147  	require.Equal(t, 1, len(received), "received incorrect number of tokens after search")
   148  
   149  	received, nErr = ss.UserAccessToken().Search(uat.UserId)
   150  	require.NoError(t, nErr)
   151  	require.Equal(t, 1, len(received), "received incorrect number of tokens after search")
   152  
   153  	received, nErr = ss.UserAccessToken().Search(u1.Username)
   154  	require.NoError(t, nErr)
   155  	require.Equal(t, 1, len(received), "received incorrect number of tokens after search")
   156  }