switch PATs to map and add deletion

This commit is contained in:
Pascal Fischer
2023-03-20 16:14:55 +01:00
parent e1ef091d45
commit e30def175b
8 changed files with 162 additions and 26 deletions

View File

@ -51,7 +51,7 @@ func TestUser_AddPATToUser(t *testing.T) {
LastUsed: time.Time{},
}
err = am.AddPATToUser("account_id", "testuser", pat)
err = am.AddPATToUser("account_id", "testuser", &pat)
if err != nil {
t.Fatalf("Error when adding PAT to user: %s", err)
}
@ -71,3 +71,60 @@ func TestUser_AddPATToUser(t *testing.T) {
}
assert.Equal(t, "testuser", userID)
}
func TestUser_DeletePAT(t *testing.T) {
store := newStore(t)
account := newAccountWithId("account_id", "testuser", "")
account.Peers["testpeer"] = &Peer{
Key: "peerkey",
SetupKey: "peerkeysetupkey",
IP: net.IP{127, 0, 0, 1},
Meta: PeerSystemMeta{},
Name: "peer name",
Status: &PeerStatus{Connected: true, LastSeen: time.Now()},
}
account.Users["user1"] = &User{
Id: "user1",
Role: "admin",
AutoGroups: nil,
PATs: map[string]*PersonalAccessToken{
"tokenID": {
ID: "tokenID",
Description: "some Description",
HashedToken: "SoMeHaShEdToKeN",
ExpirationDate: time.Now().AddDate(0, 0, 7),
CreatedBy: "user1",
CreatedAt: time.Now(),
LastUsed: time.Now(),
},
},
}
err := store.SaveAccount(account)
if err != nil {
t.Fatalf("Error when saving account: %s", err)
}
am := DefaultAccountManager{
Store: store,
cacheMux: sync.Mutex{},
cacheLoading: nil,
peersUpdateManager: nil,
idpManager: nil,
cacheManager: nil,
ctx: nil,
eventStore: nil,
singleAccountMode: false,
singleAccountModeDomain: "",
dnsDomain: "",
peerLoginExpiry: nil,
}
err = am.DeletePAT("account_id", "user1", "tokenID")
if err != nil {
t.Fatalf("Error when adding PAT to user: %s", err)
}
assert.Nil(t, store.Accounts["account_id"].Users["user1"].PATs["tokenID"])
assert.Empty(t, store.HashedPAT2TokenID["SoMeHaShEdToKeN"])
assert.Empty(t, store.TokenID2UserID["tokenID"])
}