2023-03-16 15:57:44 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2023-03-21 13:34:48 +01:00
|
|
|
const (
|
|
|
|
mockAccountID = "accountID"
|
|
|
|
mockUserID = "userID"
|
|
|
|
mockTokenID = "tokenID"
|
|
|
|
mockToken = "SoMeHaShEdToKeN"
|
|
|
|
)
|
|
|
|
|
2023-03-16 15:57:44 +01:00
|
|
|
func TestUser_AddPATToUser(t *testing.T) {
|
|
|
|
store := newStore(t)
|
2023-03-21 13:34:48 +01:00
|
|
|
account := newAccountWithId(mockAccountID, mockUserID, "")
|
|
|
|
|
2023-03-20 11:44:12 +01:00
|
|
|
err := store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when saving account: %s", err)
|
|
|
|
}
|
2023-03-16 15:57:44 +01:00
|
|
|
|
|
|
|
am := DefaultAccountManager{
|
2023-03-21 13:34:48 +01:00
|
|
|
Store: store,
|
2023-03-16 15:57:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pat := PersonalAccessToken{
|
2023-03-21 13:34:48 +01:00
|
|
|
ID: mockTokenID,
|
|
|
|
HashedToken: mockToken,
|
2023-03-16 15:57:44 +01:00
|
|
|
}
|
|
|
|
|
2023-03-21 13:34:48 +01:00
|
|
|
err = am.AddPATToUser(mockAccountID, mockUserID, &pat)
|
2023-03-20 11:44:12 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when adding PAT to user: %s", err)
|
|
|
|
}
|
2023-03-16 15:57:44 +01:00
|
|
|
|
|
|
|
fileStore := am.Store.(*FileStore)
|
2023-03-29 15:21:53 +02:00
|
|
|
tokenID := fileStore.HashedPAT2TokenID[mockToken]
|
2023-03-16 15:57:44 +01:00
|
|
|
|
2023-03-20 11:44:12 +01:00
|
|
|
if tokenID == "" {
|
2023-03-16 15:57:44 +01:00
|
|
|
t.Fatal("GetTokenIDByHashedToken failed after adding PAT")
|
|
|
|
}
|
|
|
|
|
2023-03-21 13:34:48 +01:00
|
|
|
assert.Equal(t, mockTokenID, tokenID)
|
2023-03-16 15:57:44 +01:00
|
|
|
|
2023-03-20 11:44:12 +01:00
|
|
|
userID := fileStore.TokenID2UserID[tokenID]
|
|
|
|
if userID == "" {
|
2023-03-16 15:57:44 +01:00
|
|
|
t.Fatal("GetUserByTokenId failed after adding PAT")
|
|
|
|
}
|
2023-03-21 13:34:48 +01:00
|
|
|
assert.Equal(t, mockUserID, userID)
|
2023-03-16 15:57:44 +01:00
|
|
|
}
|
2023-03-20 16:14:55 +01:00
|
|
|
|
|
|
|
func TestUser_DeletePAT(t *testing.T) {
|
|
|
|
store := newStore(t)
|
2023-03-21 13:34:48 +01:00
|
|
|
account := newAccountWithId(mockAccountID, mockUserID, "")
|
|
|
|
account.Users[mockUserID] = &User{
|
|
|
|
Id: mockUserID,
|
2023-03-20 16:14:55 +01:00
|
|
|
PATs: map[string]*PersonalAccessToken{
|
2023-03-21 13:34:48 +01:00
|
|
|
mockTokenID: {
|
|
|
|
ID: mockTokenID,
|
|
|
|
HashedToken: mockToken,
|
2023-03-20 16:14:55 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when saving account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
am := DefaultAccountManager{
|
2023-03-21 13:34:48 +01:00
|
|
|
Store: store,
|
2023-03-20 16:14:55 +01:00
|
|
|
}
|
|
|
|
|
2023-03-21 13:34:48 +01:00
|
|
|
err = am.DeletePAT(mockAccountID, mockUserID, mockTokenID)
|
2023-03-20 16:14:55 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when adding PAT to user: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-03-21 13:34:48 +01:00
|
|
|
assert.Nil(t, store.Accounts[mockAccountID].Users[mockUserID].PATs[mockTokenID])
|
|
|
|
assert.Empty(t, store.HashedPAT2TokenID[mockToken])
|
|
|
|
assert.Empty(t, store.TokenID2UserID[mockTokenID])
|
2023-03-20 16:14:55 +01:00
|
|
|
}
|