mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
4.2 KiB
133 lines
4.2 KiB
8 years ago
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
||
2 years ago
|
// SPDX-License-Identifier: MIT
|
||
8 years ago
|
|
||
2 years ago
|
package auth_test
|
||
8 years ago
|
|
||
|
import (
|
||
|
"testing"
|
||
|
|
||
2 years ago
|
auth_model "code.gitea.io/gitea/models/auth"
|
||
1 year ago
|
"code.gitea.io/gitea/models/db"
|
||
3 years ago
|
"code.gitea.io/gitea/models/unittest"
|
||
3 years ago
|
|
||
8 years ago
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func TestNewAccessToken(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
token := &auth_model.AccessToken{
|
||
8 years ago
|
UID: 3,
|
||
|
Name: "Token C",
|
||
|
}
|
||
1 year ago
|
assert.NoError(t, auth_model.NewAccessToken(db.DefaultContext, token))
|
||
3 years ago
|
unittest.AssertExistsAndLoadBean(t, token)
|
||
8 years ago
|
|
||
2 years ago
|
invalidToken := &auth_model.AccessToken{
|
||
8 years ago
|
ID: token.ID, // duplicate
|
||
|
UID: 2,
|
||
|
Name: "Token F",
|
||
|
}
|
||
1 year ago
|
assert.Error(t, auth_model.NewAccessToken(db.DefaultContext, invalidToken))
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
func TestAccessTokenByNameExists(t *testing.T) {
|
||
|
name := "Token Gitea"
|
||
|
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
token := &auth_model.AccessToken{
|
||
5 years ago
|
UID: 3,
|
||
|
Name: name,
|
||
|
}
|
||
|
|
||
|
// Check to make sure it doesn't exists already
|
||
1 year ago
|
exist, err := auth_model.AccessTokenByNameExists(db.DefaultContext, token)
|
||
5 years ago
|
assert.NoError(t, err)
|
||
|
assert.False(t, exist)
|
||
|
|
||
|
// Save it to the database
|
||
1 year ago
|
assert.NoError(t, auth_model.NewAccessToken(db.DefaultContext, token))
|
||
3 years ago
|
unittest.AssertExistsAndLoadBean(t, token)
|
||
5 years ago
|
|
||
|
// This token must be found by name in the DB now
|
||
1 year ago
|
exist, err = auth_model.AccessTokenByNameExists(db.DefaultContext, token)
|
||
5 years ago
|
assert.NoError(t, err)
|
||
|
assert.True(t, exist)
|
||
|
|
||
2 years ago
|
user4Token := &auth_model.AccessToken{
|
||
5 years ago
|
UID: 4,
|
||
|
Name: name,
|
||
|
}
|
||
|
|
||
|
// Name matches but different user ID, this shouldn't exists in the
|
||
|
// database
|
||
1 year ago
|
exist, err = auth_model.AccessTokenByNameExists(db.DefaultContext, user4Token)
|
||
5 years ago
|
assert.NoError(t, err)
|
||
|
assert.False(t, exist)
|
||
|
}
|
||
|
|
||
8 years ago
|
func TestGetAccessTokenBySHA(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
1 year ago
|
token, err := auth_model.GetAccessTokenBySHA(db.DefaultContext, "d2c6c1ba3890b309189a8e618c72a162e4efbf36")
|
||
8 years ago
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, int64(1), token.UID)
|
||
|
assert.Equal(t, "Token A", token.Name)
|
||
6 years ago
|
assert.Equal(t, "2b3668e11cb82d3af8c6e4524fc7841297668f5008d1626f0ad3417e9fa39af84c268248b78c481daa7e5dc437784003494f", token.TokenHash)
|
||
|
assert.Equal(t, "e4efbf36", token.TokenLastEight)
|
||
8 years ago
|
|
||
1 year ago
|
_, err = auth_model.GetAccessTokenBySHA(db.DefaultContext, "notahash")
|
||
8 years ago
|
assert.Error(t, err)
|
||
2 years ago
|
assert.True(t, auth_model.IsErrAccessTokenNotExist(err))
|
||
8 years ago
|
|
||
1 year ago
|
_, err = auth_model.GetAccessTokenBySHA(db.DefaultContext, "")
|
||
8 years ago
|
assert.Error(t, err)
|
||
2 years ago
|
assert.True(t, auth_model.IsErrAccessTokenEmpty(err))
|
||
8 years ago
|
}
|
||
|
|
||
|
func TestListAccessTokens(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
1 year ago
|
tokens, err := auth_model.ListAccessTokens(db.DefaultContext, auth_model.ListAccessTokensOptions{UserID: 1})
|
||
8 years ago
|
assert.NoError(t, err)
|
||
7 years ago
|
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")
|
||
|
}
|
||
8 years ago
|
|
||
1 year ago
|
tokens, err = auth_model.ListAccessTokens(db.DefaultContext, auth_model.ListAccessTokensOptions{UserID: 2})
|
||
8 years ago
|
assert.NoError(t, err)
|
||
7 years ago
|
if assert.Len(t, tokens, 1) {
|
||
|
assert.Equal(t, int64(2), tokens[0].UID)
|
||
|
assert.Equal(t, "Token A", tokens[0].Name)
|
||
|
}
|
||
8 years ago
|
|
||
1 year ago
|
tokens, err = auth_model.ListAccessTokens(db.DefaultContext, auth_model.ListAccessTokensOptions{UserID: 100})
|
||
8 years ago
|
assert.NoError(t, err)
|
||
|
assert.Empty(t, tokens)
|
||
|
}
|
||
|
|
||
|
func TestUpdateAccessToken(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
1 year ago
|
token, err := auth_model.GetAccessTokenBySHA(db.DefaultContext, "4c6f36e6cf498e2a448662f915d932c09c5a146c")
|
||
8 years ago
|
assert.NoError(t, err)
|
||
|
token.Name = "Token Z"
|
||
|
|
||
1 year ago
|
assert.NoError(t, auth_model.UpdateAccessToken(db.DefaultContext, token))
|
||
3 years ago
|
unittest.AssertExistsAndLoadBean(t, token)
|
||
8 years ago
|
}
|
||
|
|
||
|
func TestDeleteAccessTokenByID(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
8 years ago
|
|
||
1 year ago
|
token, err := auth_model.GetAccessTokenBySHA(db.DefaultContext, "4c6f36e6cf498e2a448662f915d932c09c5a146c")
|
||
8 years ago
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, int64(1), token.UID)
|
||
|
|
||
1 year ago
|
assert.NoError(t, auth_model.DeleteAccessTokenByID(db.DefaultContext, token.ID, 1))
|
||
3 years ago
|
unittest.AssertNotExistsBean(t, token)
|
||
8 years ago
|
|
||
1 year ago
|
err = auth_model.DeleteAccessTokenByID(db.DefaultContext, 100, 100)
|
||
8 years ago
|
assert.Error(t, err)
|
||
2 years ago
|
assert.True(t, auth_model.IsErrAccessTokenNotExist(err))
|
||
8 years ago
|
}
|