2016-12-29 04:03:40 +03:00
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2016-12-29 04:03:40 +03:00
|
|
|
|
2022-08-25 05:31:57 +03:00
|
|
|
package auth_test
|
2016-12-29 04:03:40 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-08-25 05:31:57 +03:00
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
2023-09-15 09:13:19 +03:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-12 17:36:47 +03:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-17 15:34:35 +03:00
|
|
|
|
2016-12-29 04:03:40 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 22:42:06 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-12-29 04:03:40 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewAccessToken(t *testing.T) {
|
2024-07-30 22:42:06 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-25 05:31:57 +03:00
|
|
|
token := &auth_model.AccessToken{
|
2016-12-29 04:03:40 +03:00
|
|
|
UID: 3,
|
|
|
|
Name: "Token C",
|
|
|
|
}
|
2024-07-30 22:42:06 +03:00
|
|
|
require.NoError(t, auth_model.NewAccessToken(db.DefaultContext, token))
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, token)
|
2016-12-29 04:03:40 +03:00
|
|
|
|
2022-08-25 05:31:57 +03:00
|
|
|
invalidToken := &auth_model.AccessToken{
|
2016-12-29 04:03:40 +03:00
|
|
|
ID: token.ID, // duplicate
|
|
|
|
UID: 2,
|
|
|
|
Name: "Token F",
|
|
|
|
}
|
2024-07-30 22:42:06 +03:00
|
|
|
require.Error(t, auth_model.NewAccessToken(db.DefaultContext, invalidToken))
|
2016-12-29 04:03:40 +03:00
|
|
|
}
|
|
|
|
|
2020-04-13 22:02:48 +03:00
|
|
|
func TestAccessTokenByNameExists(t *testing.T) {
|
|
|
|
name := "Token Gitea"
|
|
|
|
|
2024-07-30 22:42:06 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-25 05:31:57 +03:00
|
|
|
token := &auth_model.AccessToken{
|
2020-04-13 22:02:48 +03:00
|
|
|
UID: 3,
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to make sure it doesn't exists already
|
2023-09-15 09:13:19 +03:00
|
|
|
exist, err := auth_model.AccessTokenByNameExists(db.DefaultContext, token)
|
2024-07-30 22:42:06 +03:00
|
|
|
require.NoError(t, err)
|
2020-04-13 22:02:48 +03:00
|
|
|
assert.False(t, exist)
|
|
|
|
|
|
|
|
// Save it to the database
|
2024-07-30 22:42:06 +03:00
|
|
|
require.NoError(t, auth_model.NewAccessToken(db.DefaultContext, token))
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, token)
|
2020-04-13 22:02:48 +03:00
|
|
|
|
|
|
|
// This token must be found by name in the DB now
|
2023-09-15 09:13:19 +03:00
|
|
|
exist, err = auth_model.AccessTokenByNameExists(db.DefaultContext, token)
|
2024-07-30 22:42:06 +03:00
|
|
|
require.NoError(t, err)
|
2020-04-13 22:02:48 +03:00
|
|
|
assert.True(t, exist)
|
|
|
|
|
2022-08-25 05:31:57 +03:00
|
|
|
user4Token := &auth_model.AccessToken{
|
2020-04-13 22:02:48 +03:00
|
|
|
UID: 4,
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name matches but different user ID, this shouldn't exists in the
|
|
|
|
// database
|
2023-09-15 09:13:19 +03:00
|
|
|
exist, err = auth_model.AccessTokenByNameExists(db.DefaultContext, user4Token)
|
2024-07-30 22:42:06 +03:00
|
|
|
require.NoError(t, err)
|
2020-04-13 22:02:48 +03:00
|
|
|
assert.False(t, exist)
|
|
|
|
}
|
|
|
|
|
2016-12-29 04:03:40 +03:00
|
|
|
func TestGetAccessTokenBySHA(t *testing.T) {
|
2024-07-30 22:42:06 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2023-09-15 09:13:19 +03:00
|
|
|
token, err := auth_model.GetAccessTokenBySHA(db.DefaultContext, "d2c6c1ba3890b309189a8e618c72a162e4efbf36")
|
2024-07-30 22:42:06 +03:00
|
|
|
require.NoError(t, err)
|
2016-12-29 04:03:40 +03:00
|
|
|
assert.Equal(t, int64(1), token.UID)
|
|
|
|
assert.Equal(t, "Token A", token.Name)
|
2019-05-04 18:45:34 +03:00
|
|
|
assert.Equal(t, "2b3668e11cb82d3af8c6e4524fc7841297668f5008d1626f0ad3417e9fa39af84c268248b78c481daa7e5dc437784003494f", token.TokenHash)
|
|
|
|
assert.Equal(t, "e4efbf36", token.TokenLastEight)
|
2016-12-29 04:03:40 +03:00
|
|
|
|
2023-09-15 09:13:19 +03:00
|
|
|
_, err = auth_model.GetAccessTokenBySHA(db.DefaultContext, "notahash")
|
2024-07-30 22:42:06 +03:00
|
|
|
require.Error(t, err)
|
2022-08-25 05:31:57 +03:00
|
|
|
assert.True(t, auth_model.IsErrAccessTokenNotExist(err))
|
2016-12-29 04:03:40 +03:00
|
|
|
|
2023-09-15 09:13:19 +03:00
|
|
|
_, err = auth_model.GetAccessTokenBySHA(db.DefaultContext, "")
|
2024-07-30 22:42:06 +03:00
|
|
|
require.Error(t, err)
|
2022-08-25 05:31:57 +03:00
|
|
|
assert.True(t, auth_model.IsErrAccessTokenEmpty(err))
|
2016-12-29 04:03:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestListAccessTokens(t *testing.T) {
|
2024-07-30 22:42:06 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2023-11-24 06:49:41 +03:00
|
|
|
tokens, err := db.Find[auth_model.AccessToken](db.DefaultContext, auth_model.ListAccessTokensOptions{UserID: 1})
|
2024-07-30 22:42:06 +03:00
|
|
|
require.NoError(t, err)
|
2017-08-28 12:17:45 +03:00
|
|
|
if assert.Len(t, tokens, 2) {
|
|
|
|
assert.Equal(t, int64(1), tokens[0].UID)
|
|
|
|
assert.Equal(t, int64(1), tokens[1].UID)
|
|
|
|
assert.Contains(t, []string{tokens[0].Name, tokens[1].Name}, "Token A")
|
|
|
|
assert.Contains(t, []string{tokens[0].Name, tokens[1].Name}, "Token B")
|
|
|
|
}
|
2016-12-29 04:03:40 +03:00
|
|
|
|
2023-11-24 06:49:41 +03:00
|
|
|
tokens, err = db.Find[auth_model.AccessToken](db.DefaultContext, auth_model.ListAccessTokensOptions{UserID: 2})
|
2024-07-30 22:42:06 +03:00
|
|
|
require.NoError(t, err)
|
2017-08-28 12:17:45 +03:00
|
|
|
if assert.Len(t, tokens, 1) {
|
|
|
|
assert.Equal(t, int64(2), tokens[0].UID)
|
|
|
|
assert.Equal(t, "Token A", tokens[0].Name)
|
|
|
|
}
|
2016-12-29 04:03:40 +03:00
|
|
|
|
2023-11-24 06:49:41 +03:00
|
|
|
tokens, err = db.Find[auth_model.AccessToken](db.DefaultContext, auth_model.ListAccessTokensOptions{UserID: 100})
|
2024-07-30 22:42:06 +03:00
|
|
|
require.NoError(t, err)
|
2016-12-29 04:03:40 +03:00
|
|
|
assert.Empty(t, tokens)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateAccessToken(t *testing.T) {
|
2024-07-30 22:42:06 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2023-09-15 09:13:19 +03:00
|
|
|
token, err := auth_model.GetAccessTokenBySHA(db.DefaultContext, "4c6f36e6cf498e2a448662f915d932c09c5a146c")
|
2024-07-30 22:42:06 +03:00
|
|
|
require.NoError(t, err)
|
2016-12-29 04:03:40 +03:00
|
|
|
token.Name = "Token Z"
|
|
|
|
|
2024-07-30 22:42:06 +03:00
|
|
|
require.NoError(t, auth_model.UpdateAccessToken(db.DefaultContext, token))
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, token)
|
2016-12-29 04:03:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteAccessTokenByID(t *testing.T) {
|
2024-07-30 22:42:06 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2016-12-29 04:03:40 +03:00
|
|
|
|
2023-09-15 09:13:19 +03:00
|
|
|
token, err := auth_model.GetAccessTokenBySHA(db.DefaultContext, "4c6f36e6cf498e2a448662f915d932c09c5a146c")
|
2024-07-30 22:42:06 +03:00
|
|
|
require.NoError(t, err)
|
2016-12-29 04:03:40 +03:00
|
|
|
assert.Equal(t, int64(1), token.UID)
|
|
|
|
|
2024-07-30 22:42:06 +03:00
|
|
|
require.NoError(t, auth_model.DeleteAccessTokenByID(db.DefaultContext, token.ID, 1))
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertNotExistsBean(t, token)
|
2016-12-29 04:03:40 +03:00
|
|
|
|
2023-09-15 09:13:19 +03:00
|
|
|
err = auth_model.DeleteAccessTokenByID(db.DefaultContext, 100, 100)
|
2024-07-30 22:42:06 +03:00
|
|
|
require.Error(t, err)
|
2022-08-25 05:31:57 +03:00
|
|
|
assert.True(t, auth_model.IsErrAccessTokenNotExist(err))
|
2016-12-29 04:03:40 +03:00
|
|
|
}
|