mirror of https://github.com/gogs/gogs.git
db: use `context` and go-mockgen for `AccessTokensStore` (#7013)
parent
4cc3000b07
commit
0a92ad27ef
|
@ -1,5 +1,7 @@
|
||||||
version = 1
|
version = 1
|
||||||
|
|
||||||
|
exclude_patterns = ["**/mocks.go"]
|
||||||
|
|
||||||
[[analyzers]]
|
[[analyzers]]
|
||||||
name = "docker"
|
name = "docker"
|
||||||
enabled = true
|
enabled = true
|
||||||
|
|
|
@ -38,17 +38,20 @@ jobs:
|
||||||
args: --timeout=30m
|
args: --timeout=30m
|
||||||
- name: Install Task
|
- name: Install Task
|
||||||
uses: arduino/setup-task@v1
|
uses: arduino/setup-task@v1
|
||||||
|
- name: Install goimports and go-mockgen
|
||||||
|
run: |
|
||||||
|
go install github.com/derision-test/go-mockgen/cmd/go-mockgen@latest
|
||||||
|
go install golang.org/x/tools/cmd/goimports@latest
|
||||||
- name: Check Go module tidiness and generated files
|
- name: Check Go module tidiness and generated files
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
go mod tidy
|
go mod tidy
|
||||||
task generate
|
task generate
|
||||||
task generate-schemadoc
|
|
||||||
STATUS=$(git status --porcelain)
|
STATUS=$(git status --porcelain)
|
||||||
if [ ! -z "$STATUS" ]; then
|
if [ ! -z "$STATUS" ]; then
|
||||||
echo "Unstaged files:"
|
echo "Unstaged files:"
|
||||||
echo $STATUS
|
echo $STATUS
|
||||||
echo "Run 'go mod tidy', 'task generate' or 'task generate-schemadoc' and commit them"
|
echo "Run 'go mod tidy' or 'task generate' commit them"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,8 @@ tasks:
|
||||||
|
|
||||||
generate:
|
generate:
|
||||||
desc: Run all go:generate commands
|
desc: Run all go:generate commands
|
||||||
deps: [generate-schemadoc]
|
cmds:
|
||||||
|
- go generate ./...
|
||||||
|
|
||||||
test:
|
test:
|
||||||
desc: Run all tests.
|
desc: Run all tests.
|
||||||
|
|
|
@ -24,6 +24,8 @@ Gogs has the following dependencies:
|
||||||
- [Go](https://golang.org/doc/install) (v1.16 or higher)
|
- [Go](https://golang.org/doc/install) (v1.16 or higher)
|
||||||
- [Less.js](http://lesscss.org/usage/#command-line-usage-installing)
|
- [Less.js](http://lesscss.org/usage/#command-line-usage-installing)
|
||||||
- [Task](https://github.com/go-task/task) (v3)
|
- [Task](https://github.com/go-task/task) (v3)
|
||||||
|
- [goimports](https://pkg.go.dev/golang.org/x/tools/cmd/goimports)
|
||||||
|
- [go-mockgen](https://github.com/derision-test/go-mockgen)
|
||||||
- Database upon your choice (pick one, we choose PostgreSQL in this document):
|
- Database upon your choice (pick one, we choose PostgreSQL in this document):
|
||||||
- [PostgreSQL](https://wiki.postgresql.org/wiki/Detailed_installation_guides) (v9.6 or higher)
|
- [PostgreSQL](https://wiki.postgresql.org/wiki/Detailed_installation_guides) (v9.6 or higher)
|
||||||
- [MySQL](https://dev.mysql.com/downloads/mysql/) with `ENGINE=InnoDB` (v5.7 or higher)
|
- [MySQL](https://dev.mysql.com/downloads/mysql/) with `ENGINE=InnoDB` (v5.7 or higher)
|
||||||
|
@ -39,6 +41,8 @@ Gogs has the following dependencies:
|
||||||
brew install go postgresql git npm go-task/tap/go-task
|
brew install go postgresql git npm go-task/tap/go-task
|
||||||
npm install -g less
|
npm install -g less
|
||||||
npm install -g less-plugin-clean-css
|
npm install -g less-plugin-clean-css
|
||||||
|
go install github.com/derision-test/go-mockgen/cmd/go-mockgen@latest
|
||||||
|
go install golang.org/x/tools/cmd/goimports@latest
|
||||||
```
|
```
|
||||||
|
|
||||||
1. Configure PostgreSQL to start automatically:
|
1. Configure PostgreSQL to start automatically:
|
||||||
|
@ -75,6 +79,8 @@ Gogs has the following dependencies:
|
||||||
sudo apt install -y make git-all postgresql postgresql-contrib golang-go nodejs
|
sudo apt install -y make git-all postgresql postgresql-contrib golang-go nodejs
|
||||||
npm install -g less
|
npm install -g less
|
||||||
go install github.com/go-task/task/v3/cmd/task@latest
|
go install github.com/go-task/task/v3/cmd/task@latest
|
||||||
|
go install github.com/derision-test/go-mockgen/cmd/go-mockgen@latest
|
||||||
|
go install golang.org/x/tools/cmd/goimports@latest
|
||||||
```
|
```
|
||||||
|
|
||||||
1. Configure startup services:
|
1. Configure startup services:
|
||||||
|
|
|
@ -130,14 +130,14 @@ func authenticatedUserID(c *macaron.Context, sess session.Store) (_ int64, isTok
|
||||||
|
|
||||||
// Let's see if token is valid.
|
// Let's see if token is valid.
|
||||||
if len(tokenSHA) > 0 {
|
if len(tokenSHA) > 0 {
|
||||||
t, err := db.AccessTokens.GetBySHA1(tokenSHA)
|
t, err := db.AccessTokens.GetBySHA1(c.Req.Context(), tokenSHA)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !db.IsErrAccessTokenNotExist(err) {
|
if !db.IsErrAccessTokenNotExist(err) {
|
||||||
log.Error("GetAccessTokenBySHA: %v", err)
|
log.Error("GetAccessTokenBySHA: %v", err)
|
||||||
}
|
}
|
||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
if err = db.AccessTokens.Save(t); err != nil {
|
if err = db.AccessTokens.Save(c.Req.Context(), t); err != nil {
|
||||||
log.Error("UpdateAccessToken: %v", err)
|
log.Error("UpdateAccessToken: %v", err)
|
||||||
}
|
}
|
||||||
return t.UserID, true
|
return t.UserID, true
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -19,22 +20,23 @@ import (
|
||||||
//
|
//
|
||||||
// NOTE: All methods are sorted in alphabetical order.
|
// NOTE: All methods are sorted in alphabetical order.
|
||||||
type AccessTokensStore interface {
|
type AccessTokensStore interface {
|
||||||
// Create creates a new access token and persist to database.
|
// Create creates a new access token and persist to database. It returns
|
||||||
// It returns ErrAccessTokenAlreadyExist when an access token
|
// ErrAccessTokenAlreadyExist when an access token with same name already exists
|
||||||
// with same name already exists for the user.
|
// for the user.
|
||||||
Create(userID int64, name string) (*AccessToken, error)
|
Create(ctx context.Context, userID int64, name string) (*AccessToken, error)
|
||||||
// DeleteByID deletes the access token by given ID.
|
// DeleteByID deletes the access token by given ID.
|
||||||
// 🚨 SECURITY: The "userID" is required to prevent attacker
|
//
|
||||||
// deletes arbitrary access token that belongs to another user.
|
// 🚨 SECURITY: The "userID" is required to prevent attacker deletes arbitrary
|
||||||
DeleteByID(userID, id int64) error
|
// access token that belongs to another user.
|
||||||
// GetBySHA1 returns the access token with given SHA1.
|
DeleteByID(ctx context.Context, userID, id int64) error
|
||||||
// It returns ErrAccessTokenNotExist when not found.
|
// GetBySHA1 returns the access token with given SHA1. It returns
|
||||||
GetBySHA1(sha1 string) (*AccessToken, error)
|
// ErrAccessTokenNotExist when not found.
|
||||||
|
GetBySHA1(ctx context.Context, sha1 string) (*AccessToken, error)
|
||||||
// List returns all access tokens belongs to given user.
|
// List returns all access tokens belongs to given user.
|
||||||
List(userID int64) ([]*AccessToken, error)
|
List(ctx context.Context, userID int64) ([]*AccessToken, error)
|
||||||
// Save persists all values of given access token.
|
// Save persists all values of given access token. The Updated field is set to
|
||||||
// The Updated field is set to current time automatically.
|
// current time automatically.
|
||||||
Save(t *AccessToken) error
|
Save(ctx context.Context, t *AccessToken) error
|
||||||
}
|
}
|
||||||
|
|
||||||
var AccessTokens AccessTokensStore
|
var AccessTokens AccessTokensStore
|
||||||
|
@ -42,17 +44,17 @@ var AccessTokens AccessTokensStore
|
||||||
// AccessToken is a personal access token.
|
// AccessToken is a personal access token.
|
||||||
type AccessToken struct {
|
type AccessToken struct {
|
||||||
ID int64
|
ID int64
|
||||||
UserID int64 `xorm:"uid INDEX" gorm:"COLUMN:uid;INDEX"`
|
UserID int64 `gorm:"column:uid;index"`
|
||||||
Name string
|
Name string
|
||||||
Sha1 string `xorm:"UNIQUE VARCHAR(40)" gorm:"TYPE:VARCHAR(40);UNIQUE"`
|
Sha1 string `gorm:"type:VARCHAR(40);unique"`
|
||||||
SHA256 string `gorm:"type:VARCHAR(64);unique;not null"`
|
SHA256 string `gorm:"type:VARCHAR(64);unique;not null"`
|
||||||
|
|
||||||
Created time.Time `xorm:"-" gorm:"-" json:"-"`
|
Created time.Time `gorm:"-" json:"-"`
|
||||||
CreatedUnix int64
|
CreatedUnix int64
|
||||||
Updated time.Time `xorm:"-" gorm:"-" json:"-"`
|
Updated time.Time `gorm:"-" json:"-"`
|
||||||
UpdatedUnix int64
|
UpdatedUnix int64
|
||||||
HasRecentActivity bool `xorm:"-" gorm:"-" json:"-"`
|
HasRecentActivity bool `gorm:"-" json:"-"`
|
||||||
HasUsed bool `xorm:"-" gorm:"-" json:"-"`
|
HasUsed bool `gorm:"-" json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// BeforeCreate implements the GORM create hook.
|
// BeforeCreate implements the GORM create hook.
|
||||||
|
@ -97,8 +99,8 @@ func (err ErrAccessTokenAlreadyExist) Error() string {
|
||||||
return fmt.Sprintf("access token already exists: %v", err.args)
|
return fmt.Sprintf("access token already exists: %v", err.args)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *accessTokens) Create(userID int64, name string) (*AccessToken, error) {
|
func (db *accessTokens) Create(ctx context.Context, userID int64, name string) (*AccessToken, error) {
|
||||||
err := db.Where("uid = ? AND name = ?", userID, name).First(new(AccessToken)).Error
|
err := db.WithContext(ctx).Where("uid = ? AND name = ?", userID, name).First(new(AccessToken)).Error
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return nil, ErrAccessTokenAlreadyExist{args: errutil.Args{"userID": userID, "name": name}}
|
return nil, ErrAccessTokenAlreadyExist{args: errutil.Args{"userID": userID, "name": name}}
|
||||||
} else if err != gorm.ErrRecordNotFound {
|
} else if err != gorm.ErrRecordNotFound {
|
||||||
|
@ -114,7 +116,7 @@ func (db *accessTokens) Create(userID int64, name string) (*AccessToken, error)
|
||||||
Sha1: sha256[:40], // To pass the column unique constraint, keep the length of SHA1.
|
Sha1: sha256[:40], // To pass the column unique constraint, keep the length of SHA1.
|
||||||
SHA256: sha256,
|
SHA256: sha256,
|
||||||
}
|
}
|
||||||
if err = db.DB.Create(accessToken).Error; err != nil {
|
if err = db.DB.WithContext(ctx).Create(accessToken).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,8 +125,8 @@ func (db *accessTokens) Create(userID int64, name string) (*AccessToken, error)
|
||||||
return accessToken, nil
|
return accessToken, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *accessTokens) DeleteByID(userID, id int64) error {
|
func (db *accessTokens) DeleteByID(ctx context.Context, userID, id int64) error {
|
||||||
return db.Where("id = ? AND uid = ?", id, userID).Delete(new(AccessToken)).Error
|
return db.WithContext(ctx).Where("id = ? AND uid = ?", id, userID).Delete(new(AccessToken)).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ errutil.NotFound = (*ErrAccessTokenNotExist)(nil)
|
var _ errutil.NotFound = (*ErrAccessTokenNotExist)(nil)
|
||||||
|
@ -146,10 +148,10 @@ func (ErrAccessTokenNotExist) NotFound() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *accessTokens) GetBySHA1(sha1 string) (*AccessToken, error) {
|
func (db *accessTokens) GetBySHA1(ctx context.Context, sha1 string) (*AccessToken, error) {
|
||||||
sha256 := cryptoutil.SHA256(sha1)
|
sha256 := cryptoutil.SHA256(sha1)
|
||||||
token := new(AccessToken)
|
token := new(AccessToken)
|
||||||
err := db.Where("sha256 = ?", sha256).First(token).Error
|
err := db.WithContext(ctx).Where("sha256 = ?", sha256).First(token).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == gorm.ErrRecordNotFound {
|
if err == gorm.ErrRecordNotFound {
|
||||||
return nil, ErrAccessTokenNotExist{args: errutil.Args{"sha": sha1}}
|
return nil, ErrAccessTokenNotExist{args: errutil.Args{"sha": sha1}}
|
||||||
|
@ -159,11 +161,11 @@ func (db *accessTokens) GetBySHA1(sha1 string) (*AccessToken, error) {
|
||||||
return token, nil
|
return token, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *accessTokens) List(userID int64) ([]*AccessToken, error) {
|
func (db *accessTokens) List(ctx context.Context, userID int64) ([]*AccessToken, error) {
|
||||||
var tokens []*AccessToken
|
var tokens []*AccessToken
|
||||||
return tokens, db.Where("uid = ?", userID).Order("id ASC").Find(&tokens).Error
|
return tokens, db.WithContext(ctx).Where("uid = ?", userID).Order("id ASC").Find(&tokens).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *accessTokens) Save(t *AccessToken) error {
|
func (db *accessTokens) Save(ctx context.Context, t *AccessToken) error {
|
||||||
return db.DB.Save(t).Error
|
return db.DB.WithContext(ctx).Save(t).Error
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,10 +5,12 @@
|
||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
"gogs.io/gogs/internal/errutil"
|
"gogs.io/gogs/internal/errutil"
|
||||||
|
@ -77,105 +79,87 @@ func TestAccessTokens(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func accessTokensCreate(t *testing.T, db *accessTokens) {
|
func accessTokensCreate(t *testing.T, db *accessTokens) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
// Create first access token with name "Test"
|
// Create first access token with name "Test"
|
||||||
token, err := db.Create(1, "Test")
|
token, err := db.Create(ctx, 1, "Test")
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.Equal(t, int64(1), token.UserID)
|
assert.Equal(t, int64(1), token.UserID)
|
||||||
assert.Equal(t, "Test", token.Name)
|
assert.Equal(t, "Test", token.Name)
|
||||||
assert.Equal(t, 40, len(token.Sha1), "sha1 length")
|
assert.Equal(t, 40, len(token.Sha1), "sha1 length")
|
||||||
|
|
||||||
// Get it back and check the Created field
|
// Get it back and check the Created field
|
||||||
token, err = db.GetBySHA1(token.Sha1)
|
token, err = db.GetBySHA1(ctx, token.Sha1)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, db.NowFunc().Format(time.RFC3339), token.Created.UTC().Format(time.RFC3339))
|
assert.Equal(t, db.NowFunc().Format(time.RFC3339), token.Created.UTC().Format(time.RFC3339))
|
||||||
|
|
||||||
// Try create second access token with same name should fail
|
// Try create second access token with same name should fail
|
||||||
_, err = db.Create(token.UserID, token.Name)
|
_, err = db.Create(ctx, token.UserID, token.Name)
|
||||||
expErr := ErrAccessTokenAlreadyExist{args: errutil.Args{"userID": token.UserID, "name": token.Name}}
|
wantErr := ErrAccessTokenAlreadyExist{args: errutil.Args{"userID": token.UserID, "name": token.Name}}
|
||||||
assert.Equal(t, expErr, err)
|
assert.Equal(t, wantErr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func accessTokensDeleteByID(t *testing.T, db *accessTokens) {
|
func accessTokensDeleteByID(t *testing.T, db *accessTokens) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
// Create an access token with name "Test"
|
// Create an access token with name "Test"
|
||||||
token, err := db.Create(1, "Test")
|
token, err := db.Create(ctx, 1, "Test")
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete a token with mismatched user ID is noop
|
// Delete a token with mismatched user ID is noop
|
||||||
err = db.DeleteByID(2, token.ID)
|
err = db.DeleteByID(ctx, 2, token.ID)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// We should be able to get it back
|
// We should be able to get it back
|
||||||
_, err = db.GetBySHA1(token.Sha1)
|
_, err = db.GetBySHA1(ctx, token.Sha1)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
_, err = db.GetBySHA1(ctx, token.Sha1)
|
||||||
}
|
require.NoError(t, err)
|
||||||
_, err = db.GetBySHA1(token.Sha1)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now delete this token with correct user ID
|
// Now delete this token with correct user ID
|
||||||
err = db.DeleteByID(token.UserID, token.ID)
|
err = db.DeleteByID(ctx, token.UserID, token.ID)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// We should get token not found error
|
// We should get token not found error
|
||||||
_, err = db.GetBySHA1(token.Sha1)
|
_, err = db.GetBySHA1(ctx, token.Sha1)
|
||||||
expErr := ErrAccessTokenNotExist{args: errutil.Args{"sha": token.Sha1}}
|
expErr := ErrAccessTokenNotExist{args: errutil.Args{"sha": token.Sha1}}
|
||||||
assert.Equal(t, expErr, err)
|
assert.Equal(t, expErr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func accessTokensGetBySHA(t *testing.T, db *accessTokens) {
|
func accessTokensGetBySHA(t *testing.T, db *accessTokens) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
// Create an access token with name "Test"
|
// Create an access token with name "Test"
|
||||||
token, err := db.Create(1, "Test")
|
token, err := db.Create(ctx, 1, "Test")
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// We should be able to get it back
|
// We should be able to get it back
|
||||||
_, err = db.GetBySHA1(token.Sha1)
|
_, err = db.GetBySHA1(ctx, token.Sha1)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to get a non-existent token
|
// Try to get a non-existent token
|
||||||
_, err = db.GetBySHA1("bad_sha")
|
_, err = db.GetBySHA1(ctx, "bad_sha")
|
||||||
expErr := ErrAccessTokenNotExist{args: errutil.Args{"sha": "bad_sha"}}
|
expErr := ErrAccessTokenNotExist{args: errutil.Args{"sha": "bad_sha"}}
|
||||||
assert.Equal(t, expErr, err)
|
assert.Equal(t, expErr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func accessTokensList(t *testing.T, db *accessTokens) {
|
func accessTokensList(t *testing.T, db *accessTokens) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
// Create two access tokens for user 1
|
// Create two access tokens for user 1
|
||||||
_, err := db.Create(1, "user1_1")
|
_, err := db.Create(ctx, 1, "user1_1")
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
_, err = db.Create(ctx, 1, "user1_2")
|
||||||
}
|
require.NoError(t, err)
|
||||||
_, err = db.Create(1, "user1_2")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create one access token for user 2
|
// Create one access token for user 2
|
||||||
_, err = db.Create(2, "user2_1")
|
_, err = db.Create(ctx, 2, "user2_1")
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// List all access tokens for user 1
|
// List all access tokens for user 1
|
||||||
tokens, err := db.List(1)
|
tokens, err := db.List(ctx, 1)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, 2, len(tokens), "number of tokens")
|
assert.Equal(t, 2, len(tokens), "number of tokens")
|
||||||
|
|
||||||
assert.Equal(t, int64(1), tokens[0].UserID)
|
assert.Equal(t, int64(1), tokens[0].UserID)
|
||||||
|
@ -186,24 +170,20 @@ func accessTokensList(t *testing.T, db *accessTokens) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func accessTokensSave(t *testing.T, db *accessTokens) {
|
func accessTokensSave(t *testing.T, db *accessTokens) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
// Create an access token with name "Test"
|
// Create an access token with name "Test"
|
||||||
token, err := db.Create(1, "Test")
|
token, err := db.Create(ctx, 1, "Test")
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Updated field is zero now
|
// Updated field is zero now
|
||||||
assert.True(t, token.Updated.IsZero())
|
assert.True(t, token.Updated.IsZero())
|
||||||
|
|
||||||
err = db.Save(token)
|
err = db.Save(ctx, token)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get back from DB should have Updated set
|
// Get back from DB should have Updated set
|
||||||
token, err = db.GetBySHA1(token.Sha1)
|
token, err = db.GetBySHA1(ctx, token.Sha1)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, db.NowFunc().Format(time.RFC3339), token.Updated.UTC().Format(time.RFC3339))
|
assert.Equal(t, db.NowFunc().Format(time.RFC3339), token.Updated.UTC().Format(time.RFC3339))
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,214 @@
|
||||||
|
// Copyright 2020 The Gogs Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gogs.io/gogs/internal/lfsutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:generate go-mockgen -f gogs.io/gogs/internal/db -i AccessTokensStore -o mocks.go
|
||||||
|
|
||||||
|
func SetMockAccessTokensStore(t *testing.T, mock AccessTokensStore) {
|
||||||
|
before := AccessTokens
|
||||||
|
AccessTokens = mock
|
||||||
|
t.Cleanup(func() {
|
||||||
|
AccessTokens = before
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ LFSStore = (*MockLFSStore)(nil)
|
||||||
|
|
||||||
|
type MockLFSStore struct {
|
||||||
|
MockCreateObject func(repoID int64, oid lfsutil.OID, size int64, storage lfsutil.Storage) error
|
||||||
|
MockGetObjectByOID func(repoID int64, oid lfsutil.OID) (*LFSObject, error)
|
||||||
|
MockGetObjectsByOIDs func(repoID int64, oids ...lfsutil.OID) ([]*LFSObject, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockLFSStore) CreateObject(repoID int64, oid lfsutil.OID, size int64, storage lfsutil.Storage) error {
|
||||||
|
return m.MockCreateObject(repoID, oid, size, storage)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockLFSStore) GetObjectByOID(repoID int64, oid lfsutil.OID) (*LFSObject, error) {
|
||||||
|
return m.MockGetObjectByOID(repoID, oid)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockLFSStore) GetObjectsByOIDs(repoID int64, oids ...lfsutil.OID) ([]*LFSObject, error) {
|
||||||
|
return m.MockGetObjectsByOIDs(repoID, oids...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetMockLFSStore(t *testing.T, mock LFSStore) {
|
||||||
|
before := LFS
|
||||||
|
LFS = mock
|
||||||
|
t.Cleanup(func() {
|
||||||
|
LFS = before
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ loginSourceFilesStore = (*mockLoginSourceFilesStore)(nil)
|
||||||
|
|
||||||
|
type mockLoginSourceFilesStore struct {
|
||||||
|
MockGetByID func(id int64) (*LoginSource, error)
|
||||||
|
MockLen func() int
|
||||||
|
MockList func(opts ListLoginSourceOpts) []*LoginSource
|
||||||
|
MockUpdate func(source *LoginSource)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockLoginSourceFilesStore) GetByID(id int64) (*LoginSource, error) {
|
||||||
|
return m.MockGetByID(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockLoginSourceFilesStore) Len() int {
|
||||||
|
return m.MockLen()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockLoginSourceFilesStore) List(opts ListLoginSourceOpts) []*LoginSource {
|
||||||
|
return m.MockList(opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockLoginSourceFilesStore) Update(source *LoginSource) {
|
||||||
|
m.MockUpdate(source)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setMockLoginSourceFilesStore(t *testing.T, db *loginSources, mock loginSourceFilesStore) {
|
||||||
|
before := db.files
|
||||||
|
db.files = mock
|
||||||
|
t.Cleanup(func() {
|
||||||
|
db.files = before
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ loginSourceFileStore = (*mockLoginSourceFileStore)(nil)
|
||||||
|
|
||||||
|
type mockLoginSourceFileStore struct {
|
||||||
|
MockSetGeneral func(name, value string)
|
||||||
|
MockSetConfig func(cfg interface{}) error
|
||||||
|
MockSave func() error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockLoginSourceFileStore) SetGeneral(name, value string) {
|
||||||
|
m.MockSetGeneral(name, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockLoginSourceFileStore) SetConfig(cfg interface{}) error {
|
||||||
|
return m.MockSetConfig(cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockLoginSourceFileStore) Save() error {
|
||||||
|
return m.MockSave()
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ PermsStore = (*MockPermsStore)(nil)
|
||||||
|
|
||||||
|
type MockPermsStore struct {
|
||||||
|
MockAccessMode func(userID, repoID int64, opts AccessModeOptions) AccessMode
|
||||||
|
MockAuthorize func(userID, repoID int64, desired AccessMode, opts AccessModeOptions) bool
|
||||||
|
MockSetRepoPerms func(repoID int64, accessMap map[int64]AccessMode) error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockPermsStore) AccessMode(userID, repoID int64, opts AccessModeOptions) AccessMode {
|
||||||
|
return m.MockAccessMode(userID, repoID, opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockPermsStore) Authorize(userID, repoID int64, desired AccessMode, opts AccessModeOptions) bool {
|
||||||
|
return m.MockAuthorize(userID, repoID, desired, opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockPermsStore) SetRepoPerms(repoID int64, accessMap map[int64]AccessMode) error {
|
||||||
|
return m.MockSetRepoPerms(repoID, accessMap)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetMockPermsStore(t *testing.T, mock PermsStore) {
|
||||||
|
before := Perms
|
||||||
|
Perms = mock
|
||||||
|
t.Cleanup(func() {
|
||||||
|
Perms = before
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ ReposStore = (*MockReposStore)(nil)
|
||||||
|
|
||||||
|
type MockReposStore struct {
|
||||||
|
MockGetByName func(ownerID int64, name string) (*Repository, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockReposStore) GetByName(ownerID int64, name string) (*Repository, error) {
|
||||||
|
return m.MockGetByName(ownerID, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetMockReposStore(t *testing.T, mock ReposStore) {
|
||||||
|
before := Repos
|
||||||
|
Repos = mock
|
||||||
|
t.Cleanup(func() {
|
||||||
|
Repos = before
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ TwoFactorsStore = (*MockTwoFactorsStore)(nil)
|
||||||
|
|
||||||
|
type MockTwoFactorsStore struct {
|
||||||
|
MockCreate func(userID int64, key, secret string) error
|
||||||
|
MockGetByUserID func(userID int64) (*TwoFactor, error)
|
||||||
|
MockIsUserEnabled func(userID int64) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockTwoFactorsStore) Create(userID int64, key, secret string) error {
|
||||||
|
return m.MockCreate(userID, key, secret)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockTwoFactorsStore) GetByUserID(userID int64) (*TwoFactor, error) {
|
||||||
|
return m.MockGetByUserID(userID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockTwoFactorsStore) IsUserEnabled(userID int64) bool {
|
||||||
|
return m.MockIsUserEnabled(userID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetMockTwoFactorsStore(t *testing.T, mock TwoFactorsStore) {
|
||||||
|
before := TwoFactors
|
||||||
|
TwoFactors = mock
|
||||||
|
t.Cleanup(func() {
|
||||||
|
TwoFactors = before
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ UsersStore = (*MockUsersStore)(nil)
|
||||||
|
|
||||||
|
type MockUsersStore struct {
|
||||||
|
MockAuthenticate func(username, password string, loginSourceID int64) (*User, error)
|
||||||
|
MockCreate func(username, email string, opts CreateUserOpts) (*User, error)
|
||||||
|
MockGetByEmail func(email string) (*User, error)
|
||||||
|
MockGetByID func(id int64) (*User, error)
|
||||||
|
MockGetByUsername func(username string) (*User, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockUsersStore) Authenticate(username, password string, loginSourceID int64) (*User, error) {
|
||||||
|
return m.MockAuthenticate(username, password, loginSourceID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockUsersStore) Create(username, email string, opts CreateUserOpts) (*User, error) {
|
||||||
|
return m.MockCreate(username, email, opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockUsersStore) GetByEmail(email string) (*User, error) {
|
||||||
|
return m.MockGetByEmail(email)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockUsersStore) GetByID(id int64) (*User, error) {
|
||||||
|
return m.MockGetByID(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockUsersStore) GetByUsername(username string) (*User, error) {
|
||||||
|
return m.MockGetByUsername(username)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetMockUsersStore(t *testing.T, mock UsersStore) {
|
||||||
|
before := Users
|
||||||
|
Users = mock
|
||||||
|
t.Cleanup(func() {
|
||||||
|
Users = before
|
||||||
|
})
|
||||||
|
}
|
|
@ -1,244 +1,659 @@
|
||||||
// Copyright 2020 The Gogs Authors. All rights reserved.
|
// Code generated by go-mockgen 1.2.0; DO NOT EDIT.
|
||||||
// Use of this source code is governed by a MIT-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"context"
|
||||||
|
"sync"
|
||||||
"gogs.io/gogs/internal/lfsutil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NOTE: Mocks are sorted in alphabetical order.
|
// MockAccessTokensStore is a mock implementation of the AccessTokensStore
|
||||||
|
// interface (from the package gogs.io/gogs/internal/db) used for unit
|
||||||
var _ AccessTokensStore = (*MockAccessTokensStore)(nil)
|
// testing.
|
||||||
|
|
||||||
type MockAccessTokensStore struct {
|
type MockAccessTokensStore struct {
|
||||||
MockCreate func(userID int64, name string) (*AccessToken, error)
|
// CreateFunc is an instance of a mock function object controlling the
|
||||||
MockDeleteByID func(userID, id int64) error
|
// behavior of the method Create.
|
||||||
MockGetBySHA1 func(sha string) (*AccessToken, error)
|
CreateFunc *AccessTokensStoreCreateFunc
|
||||||
MockList func(userID int64) ([]*AccessToken, error)
|
// DeleteByIDFunc is an instance of a mock function object controlling
|
||||||
MockSave func(t *AccessToken) error
|
// the behavior of the method DeleteByID.
|
||||||
|
DeleteByIDFunc *AccessTokensStoreDeleteByIDFunc
|
||||||
|
// GetBySHA1Func is an instance of a mock function object controlling
|
||||||
|
// the behavior of the method GetBySHA1.
|
||||||
|
GetBySHA1Func *AccessTokensStoreGetBySHA1Func
|
||||||
|
// ListFunc is an instance of a mock function object controlling the
|
||||||
|
// behavior of the method List.
|
||||||
|
ListFunc *AccessTokensStoreListFunc
|
||||||
|
// SaveFunc is an instance of a mock function object controlling the
|
||||||
|
// behavior of the method Save.
|
||||||
|
SaveFunc *AccessTokensStoreSaveFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccessTokensStore) Create(userID int64, name string) (*AccessToken, error) {
|
// NewMockAccessTokensStore creates a new mock of the AccessTokensStore
|
||||||
return m.MockCreate(userID, name)
|
// interface. All methods return zero values for all results, unless
|
||||||
|
// overwritten.
|
||||||
|
func NewMockAccessTokensStore() *MockAccessTokensStore {
|
||||||
|
return &MockAccessTokensStore{
|
||||||
|
CreateFunc: &AccessTokensStoreCreateFunc{
|
||||||
|
defaultHook: func(context.Context, int64, string) (r0 *AccessToken, r1 error) {
|
||||||
|
return
|
||||||
|
},
|
||||||
|
},
|
||||||
|
DeleteByIDFunc: &AccessTokensStoreDeleteByIDFunc{
|
||||||
|
defaultHook: func(context.Context, int64, int64) (r0 error) {
|
||||||
|
return
|
||||||
|
},
|
||||||
|
},
|
||||||
|
GetBySHA1Func: &AccessTokensStoreGetBySHA1Func{
|
||||||
|
defaultHook: func(context.Context, string) (r0 *AccessToken, r1 error) {
|
||||||
|
return
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ListFunc: &AccessTokensStoreListFunc{
|
||||||
|
defaultHook: func(context.Context, int64) (r0 []*AccessToken, r1 error) {
|
||||||
|
return
|
||||||
|
},
|
||||||
|
},
|
||||||
|
SaveFunc: &AccessTokensStoreSaveFunc{
|
||||||
|
defaultHook: func(context.Context, *AccessToken) (r0 error) {
|
||||||
|
return
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccessTokensStore) DeleteByID(userID, id int64) error {
|
// NewStrictMockAccessTokensStore creates a new mock of the
|
||||||
return m.MockDeleteByID(userID, id)
|
// AccessTokensStore interface. All methods panic on invocation, unless
|
||||||
|
// overwritten.
|
||||||
|
func NewStrictMockAccessTokensStore() *MockAccessTokensStore {
|
||||||
|
return &MockAccessTokensStore{
|
||||||
|
CreateFunc: &AccessTokensStoreCreateFunc{
|
||||||
|
defaultHook: func(context.Context, int64, string) (*AccessToken, error) {
|
||||||
|
panic("unexpected invocation of MockAccessTokensStore.Create")
|
||||||
|
},
|
||||||
|
},
|
||||||
|
DeleteByIDFunc: &AccessTokensStoreDeleteByIDFunc{
|
||||||
|
defaultHook: func(context.Context, int64, int64) error {
|
||||||
|
panic("unexpected invocation of MockAccessTokensStore.DeleteByID")
|
||||||
|
},
|
||||||
|
},
|
||||||
|
GetBySHA1Func: &AccessTokensStoreGetBySHA1Func{
|
||||||
|
defaultHook: func(context.Context, string) (*AccessToken, error) {
|
||||||
|
panic("unexpected invocation of MockAccessTokensStore.GetBySHA1")
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ListFunc: &AccessTokensStoreListFunc{
|
||||||
|
defaultHook: func(context.Context, int64) ([]*AccessToken, error) {
|
||||||
|
panic("unexpected invocation of MockAccessTokensStore.List")
|
||||||
|
},
|
||||||
|
},
|
||||||
|
SaveFunc: &AccessTokensStoreSaveFunc{
|
||||||
|
defaultHook: func(context.Context, *AccessToken) error {
|
||||||
|
panic("unexpected invocation of MockAccessTokensStore.Save")
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccessTokensStore) GetBySHA1(sha string) (*AccessToken, error) {
|
// NewMockAccessTokensStoreFrom creates a new mock of the
|
||||||
return m.MockGetBySHA1(sha)
|
// MockAccessTokensStore interface. All methods delegate to the given
|
||||||
|
// implementation, unless overwritten.
|
||||||
|
func NewMockAccessTokensStoreFrom(i AccessTokensStore) *MockAccessTokensStore {
|
||||||
|
return &MockAccessTokensStore{
|
||||||
|
CreateFunc: &AccessTokensStoreCreateFunc{
|
||||||
|
defaultHook: i.Create,
|
||||||
|
},
|
||||||
|
DeleteByIDFunc: &AccessTokensStoreDeleteByIDFunc{
|
||||||
|
defaultHook: i.DeleteByID,
|
||||||
|
},
|
||||||
|
GetBySHA1Func: &AccessTokensStoreGetBySHA1Func{
|
||||||
|
defaultHook: i.GetBySHA1,
|
||||||
|
},
|
||||||
|
ListFunc: &AccessTokensStoreListFunc{
|
||||||
|
defaultHook: i.List,
|
||||||
|
},
|
||||||
|
SaveFunc: &AccessTokensStoreSaveFunc{
|
||||||
|
defaultHook: i.Save,
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccessTokensStore) List(userID int64) ([]*AccessToken, error) {
|
// AccessTokensStoreCreateFunc describes the behavior when the Create method
|
||||||
return m.MockList(userID)
|
// of the parent MockAccessTokensStore instance is invoked.
|
||||||
|
type AccessTokensStoreCreateFunc struct {
|
||||||
|
defaultHook func(context.Context, int64, string) (*AccessToken, error)
|
||||||
|
hooks []func(context.Context, int64, string) (*AccessToken, error)
|
||||||
|
history []AccessTokensStoreCreateFuncCall
|
||||||
|
mutex sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccessTokensStore) Save(t *AccessToken) error {
|
// Create delegates to the next hook function in the queue and stores the
|
||||||
return m.MockSave(t)
|
// parameter and result values of this invocation.
|
||||||
|
func (m *MockAccessTokensStore) Create(v0 context.Context, v1 int64, v2 string) (*AccessToken, error) {
|
||||||
|
r0, r1 := m.CreateFunc.nextHook()(v0, v1, v2)
|
||||||
|
m.CreateFunc.appendCall(AccessTokensStoreCreateFuncCall{v0, v1, v2, r0, r1})
|
||||||
|
return r0, r1
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetMockAccessTokensStore(t *testing.T, mock AccessTokensStore) {
|
// SetDefaultHook sets function that is called when the Create method of the
|
||||||
before := AccessTokens
|
// parent MockAccessTokensStore instance is invoked and the hook queue is
|
||||||
AccessTokens = mock
|
// empty.
|
||||||
t.Cleanup(func() {
|
func (f *AccessTokensStoreCreateFunc) SetDefaultHook(hook func(context.Context, int64, string) (*AccessToken, error)) {
|
||||||
AccessTokens = before
|
f.defaultHook = hook
|
||||||
|
}
|
||||||
|
|
||||||
|
// PushHook adds a function to the end of hook queue. Each invocation of the
|
||||||
|
// Create method of the parent MockAccessTokensStore instance invokes the
|
||||||
|
// hook at the front of the queue and discards it. After the queue is empty,
|
||||||
|
// the default hook function is invoked for any future action.
|
||||||
|
func (f *AccessTokensStoreCreateFunc) PushHook(hook func(context.Context, int64, string) (*AccessToken, error)) {
|
||||||
|
f.mutex.Lock()
|
||||||
|
f.hooks = append(f.hooks, hook)
|
||||||
|
f.mutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDefaultReturn calls SetDefaultHook with a function that returns the
|
||||||
|
// given values.
|
||||||
|
func (f *AccessTokensStoreCreateFunc) SetDefaultReturn(r0 *AccessToken, r1 error) {
|
||||||
|
f.SetDefaultHook(func(context.Context, int64, string) (*AccessToken, error) {
|
||||||
|
return r0, r1
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ LFSStore = (*MockLFSStore)(nil)
|
// PushReturn calls PushHook with a function that returns the given values.
|
||||||
|
func (f *AccessTokensStoreCreateFunc) PushReturn(r0 *AccessToken, r1 error) {
|
||||||
type MockLFSStore struct {
|
f.PushHook(func(context.Context, int64, string) (*AccessToken, error) {
|
||||||
MockCreateObject func(repoID int64, oid lfsutil.OID, size int64, storage lfsutil.Storage) error
|
return r0, r1
|
||||||
MockGetObjectByOID func(repoID int64, oid lfsutil.OID) (*LFSObject, error)
|
|
||||||
MockGetObjectsByOIDs func(repoID int64, oids ...lfsutil.OID) ([]*LFSObject, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockLFSStore) CreateObject(repoID int64, oid lfsutil.OID, size int64, storage lfsutil.Storage) error {
|
|
||||||
return m.MockCreateObject(repoID, oid, size, storage)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockLFSStore) GetObjectByOID(repoID int64, oid lfsutil.OID) (*LFSObject, error) {
|
|
||||||
return m.MockGetObjectByOID(repoID, oid)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockLFSStore) GetObjectsByOIDs(repoID int64, oids ...lfsutil.OID) ([]*LFSObject, error) {
|
|
||||||
return m.MockGetObjectsByOIDs(repoID, oids...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetMockLFSStore(t *testing.T, mock LFSStore) {
|
|
||||||
before := LFS
|
|
||||||
LFS = mock
|
|
||||||
t.Cleanup(func() {
|
|
||||||
LFS = before
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ loginSourceFilesStore = (*mockLoginSourceFilesStore)(nil)
|
func (f *AccessTokensStoreCreateFunc) nextHook() func(context.Context, int64, string) (*AccessToken, error) {
|
||||||
|
f.mutex.Lock()
|
||||||
|
defer f.mutex.Unlock()
|
||||||
|
|
||||||
type mockLoginSourceFilesStore struct {
|
if len(f.hooks) == 0 {
|
||||||
MockGetByID func(id int64) (*LoginSource, error)
|
return f.defaultHook
|
||||||
MockLen func() int
|
|
||||||
MockList func(opts ListLoginSourceOpts) []*LoginSource
|
|
||||||
MockUpdate func(source *LoginSource)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockLoginSourceFilesStore) GetByID(id int64) (*LoginSource, error) {
|
hook := f.hooks[0]
|
||||||
return m.MockGetByID(id)
|
f.hooks = f.hooks[1:]
|
||||||
|
return hook
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockLoginSourceFilesStore) Len() int {
|
func (f *AccessTokensStoreCreateFunc) appendCall(r0 AccessTokensStoreCreateFuncCall) {
|
||||||
return m.MockLen()
|
f.mutex.Lock()
|
||||||
|
f.history = append(f.history, r0)
|
||||||
|
f.mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockLoginSourceFilesStore) List(opts ListLoginSourceOpts) []*LoginSource {
|
// History returns a sequence of AccessTokensStoreCreateFuncCall objects
|
||||||
return m.MockList(opts)
|
// describing the invocations of this function.
|
||||||
|
func (f *AccessTokensStoreCreateFunc) History() []AccessTokensStoreCreateFuncCall {
|
||||||
|
f.mutex.Lock()
|
||||||
|
history := make([]AccessTokensStoreCreateFuncCall, len(f.history))
|
||||||
|
copy(history, f.history)
|
||||||
|
f.mutex.Unlock()
|
||||||
|
|
||||||
|
return history
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockLoginSourceFilesStore) Update(source *LoginSource) {
|
// AccessTokensStoreCreateFuncCall is an object that describes an invocation
|
||||||
m.MockUpdate(source)
|
// of method Create on an instance of MockAccessTokensStore.
|
||||||
|
type AccessTokensStoreCreateFuncCall struct {
|
||||||
|
// Arg0 is the value of the 1st argument passed to this method
|
||||||
|
// invocation.
|
||||||
|
Arg0 context.Context
|
||||||
|
// Arg1 is the value of the 2nd argument passed to this method
|
||||||
|
// invocation.
|
||||||
|
Arg1 int64
|
||||||
|
// Arg2 is the value of the 3rd argument passed to this method
|
||||||
|
// invocation.
|
||||||
|
Arg2 string
|
||||||
|
// Result0 is the value of the 1st result returned from this method
|
||||||
|
// invocation.
|
||||||
|
Result0 *AccessToken
|
||||||
|
// Result1 is the value of the 2nd result returned from this method
|
||||||
|
// invocation.
|
||||||
|
Result1 error
|
||||||
}
|
}
|
||||||
|
|
||||||
func setMockLoginSourceFilesStore(t *testing.T, db *loginSources, mock loginSourceFilesStore) {
|
// Args returns an interface slice containing the arguments of this
|
||||||
before := db.files
|
// invocation.
|
||||||
db.files = mock
|
func (c AccessTokensStoreCreateFuncCall) Args() []interface{} {
|
||||||
t.Cleanup(func() {
|
return []interface{}{c.Arg0, c.Arg1, c.Arg2}
|
||||||
db.files = before
|
}
|
||||||
|
|
||||||
|
// Results returns an interface slice containing the results of this
|
||||||
|
// invocation.
|
||||||
|
func (c AccessTokensStoreCreateFuncCall) Results() []interface{} {
|
||||||
|
return []interface{}{c.Result0, c.Result1}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AccessTokensStoreDeleteByIDFunc describes the behavior when the
|
||||||
|
// DeleteByID method of the parent MockAccessTokensStore instance is
|
||||||
|
// invoked.
|
||||||
|
type AccessTokensStoreDeleteByIDFunc struct {
|
||||||
|
defaultHook func(context.Context, int64, int64) error
|
||||||
|
hooks []func(context.Context, int64, int64) error
|
||||||
|
history []AccessTokensStoreDeleteByIDFuncCall
|
||||||
|
mutex sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteByID delegates to the next hook function in the queue and stores
|
||||||
|
// the parameter and result values of this invocation.
|
||||||
|
func (m *MockAccessTokensStore) DeleteByID(v0 context.Context, v1 int64, v2 int64) error {
|
||||||
|
r0 := m.DeleteByIDFunc.nextHook()(v0, v1, v2)
|
||||||
|
m.DeleteByIDFunc.appendCall(AccessTokensStoreDeleteByIDFuncCall{v0, v1, v2, r0})
|
||||||
|
return r0
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDefaultHook sets function that is called when the DeleteByID method of
|
||||||
|
// the parent MockAccessTokensStore instance is invoked and the hook queue
|
||||||
|
// is empty.
|
||||||
|
func (f *AccessTokensStoreDeleteByIDFunc) SetDefaultHook(hook func(context.Context, int64, int64) error) {
|
||||||
|
f.defaultHook = hook
|
||||||
|
}
|
||||||
|
|
||||||
|
// PushHook adds a function to the end of hook queue. Each invocation of the
|
||||||
|
// DeleteByID method of the parent MockAccessTokensStore instance invokes
|
||||||
|
// the hook at the front of the queue and discards it. After the queue is
|
||||||
|
// empty, the default hook function is invoked for any future action.
|
||||||
|
func (f *AccessTokensStoreDeleteByIDFunc) PushHook(hook func(context.Context, int64, int64) error) {
|
||||||
|
f.mutex.Lock()
|
||||||
|
f.hooks = append(f.hooks, hook)
|
||||||
|
f.mutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDefaultReturn calls SetDefaultHook with a function that returns the
|
||||||
|
// given values.
|
||||||
|
func (f *AccessTokensStoreDeleteByIDFunc) SetDefaultReturn(r0 error) {
|
||||||
|
f.SetDefaultHook(func(context.Context, int64, int64) error {
|
||||||
|
return r0
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ loginSourceFileStore = (*mockLoginSourceFileStore)(nil)
|
// PushReturn calls PushHook with a function that returns the given values.
|
||||||
|
func (f *AccessTokensStoreDeleteByIDFunc) PushReturn(r0 error) {
|
||||||
type mockLoginSourceFileStore struct {
|
f.PushHook(func(context.Context, int64, int64) error {
|
||||||
MockSetGeneral func(name, value string)
|
return r0
|
||||||
MockSetConfig func(cfg interface{}) error
|
|
||||||
MockSave func() error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockLoginSourceFileStore) SetGeneral(name, value string) {
|
|
||||||
m.MockSetGeneral(name, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockLoginSourceFileStore) SetConfig(cfg interface{}) error {
|
|
||||||
return m.MockSetConfig(cfg)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockLoginSourceFileStore) Save() error {
|
|
||||||
return m.MockSave()
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ PermsStore = (*MockPermsStore)(nil)
|
|
||||||
|
|
||||||
type MockPermsStore struct {
|
|
||||||
MockAccessMode func(userID, repoID int64, opts AccessModeOptions) AccessMode
|
|
||||||
MockAuthorize func(userID, repoID int64, desired AccessMode, opts AccessModeOptions) bool
|
|
||||||
MockSetRepoPerms func(repoID int64, accessMap map[int64]AccessMode) error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockPermsStore) AccessMode(userID, repoID int64, opts AccessModeOptions) AccessMode {
|
|
||||||
return m.MockAccessMode(userID, repoID, opts)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockPermsStore) Authorize(userID, repoID int64, desired AccessMode, opts AccessModeOptions) bool {
|
|
||||||
return m.MockAuthorize(userID, repoID, desired, opts)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockPermsStore) SetRepoPerms(repoID int64, accessMap map[int64]AccessMode) error {
|
|
||||||
return m.MockSetRepoPerms(repoID, accessMap)
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetMockPermsStore(t *testing.T, mock PermsStore) {
|
|
||||||
before := Perms
|
|
||||||
Perms = mock
|
|
||||||
t.Cleanup(func() {
|
|
||||||
Perms = before
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ ReposStore = (*MockReposStore)(nil)
|
func (f *AccessTokensStoreDeleteByIDFunc) nextHook() func(context.Context, int64, int64) error {
|
||||||
|
f.mutex.Lock()
|
||||||
|
defer f.mutex.Unlock()
|
||||||
|
|
||||||
type MockReposStore struct {
|
if len(f.hooks) == 0 {
|
||||||
MockGetByName func(ownerID int64, name string) (*Repository, error)
|
return f.defaultHook
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockReposStore) GetByName(ownerID int64, name string) (*Repository, error) {
|
hook := f.hooks[0]
|
||||||
return m.MockGetByName(ownerID, name)
|
f.hooks = f.hooks[1:]
|
||||||
|
return hook
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetMockReposStore(t *testing.T, mock ReposStore) {
|
func (f *AccessTokensStoreDeleteByIDFunc) appendCall(r0 AccessTokensStoreDeleteByIDFuncCall) {
|
||||||
before := Repos
|
f.mutex.Lock()
|
||||||
Repos = mock
|
f.history = append(f.history, r0)
|
||||||
t.Cleanup(func() {
|
f.mutex.Unlock()
|
||||||
Repos = before
|
}
|
||||||
|
|
||||||
|
// History returns a sequence of AccessTokensStoreDeleteByIDFuncCall objects
|
||||||
|
// describing the invocations of this function.
|
||||||
|
func (f *AccessTokensStoreDeleteByIDFunc) History() []AccessTokensStoreDeleteByIDFuncCall {
|
||||||
|
f.mutex.Lock()
|
||||||
|
history := make([]AccessTokensStoreDeleteByIDFuncCall, len(f.history))
|
||||||
|
copy(history, f.history)
|
||||||
|
f.mutex.Unlock()
|
||||||
|
|
||||||
|
return history
|
||||||
|
}
|
||||||
|
|
||||||
|
// AccessTokensStoreDeleteByIDFuncCall is an object that describes an
|
||||||
|
// invocation of method DeleteByID on an instance of MockAccessTokensStore.
|
||||||
|
type AccessTokensStoreDeleteByIDFuncCall struct {
|
||||||
|
// Arg0 is the value of the 1st argument passed to this method
|
||||||
|
// invocation.
|
||||||
|
Arg0 context.Context
|
||||||
|
// Arg1 is the value of the 2nd argument passed to this method
|
||||||
|
// invocation.
|
||||||
|
Arg1 int64
|
||||||
|
// Arg2 is the value of the 3rd argument passed to this method
|
||||||
|
// invocation.
|
||||||
|
Arg2 int64
|
||||||
|
// Result0 is the value of the 1st result returned from this method
|
||||||
|
// invocation.
|
||||||
|
Result0 error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Args returns an interface slice containing the arguments of this
|
||||||
|
// invocation.
|
||||||
|
func (c AccessTokensStoreDeleteByIDFuncCall) Args() []interface{} {
|
||||||
|
return []interface{}{c.Arg0, c.Arg1, c.Arg2}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Results returns an interface slice containing the results of this
|
||||||
|
// invocation.
|
||||||
|
func (c AccessTokensStoreDeleteByIDFuncCall) Results() []interface{} {
|
||||||
|
return []interface{}{c.Result0}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AccessTokensStoreGetBySHA1Func describes the behavior when the GetBySHA1
|
||||||
|
// method of the parent MockAccessTokensStore instance is invoked.
|
||||||
|
type AccessTokensStoreGetBySHA1Func struct {
|
||||||
|
defaultHook func(context.Context, string) (*AccessToken, error)
|
||||||
|
hooks []func(context.Context, string) (*AccessToken, error)
|
||||||
|
history []AccessTokensStoreGetBySHA1FuncCall
|
||||||
|
mutex sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBySHA1 delegates to the next hook function in the queue and stores the
|
||||||
|
// parameter and result values of this invocation.
|
||||||
|
func (m *MockAccessTokensStore) GetBySHA1(v0 context.Context, v1 string) (*AccessToken, error) {
|
||||||
|
r0, r1 := m.GetBySHA1Func.nextHook()(v0, v1)
|
||||||
|
m.GetBySHA1Func.appendCall(AccessTokensStoreGetBySHA1FuncCall{v0, v1, r0, r1})
|
||||||
|
return r0, r1
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDefaultHook sets function that is called when the GetBySHA1 method of
|
||||||
|
// the parent MockAccessTokensStore instance is invoked and the hook queue
|
||||||
|
// is empty.
|
||||||
|
func (f *AccessTokensStoreGetBySHA1Func) SetDefaultHook(hook func(context.Context, string) (*AccessToken, error)) {
|
||||||
|
f.defaultHook = hook
|
||||||
|
}
|
||||||
|
|
||||||
|
// PushHook adds a function to the end of hook queue. Each invocation of the
|
||||||
|
// GetBySHA1 method of the parent MockAccessTokensStore instance invokes the
|
||||||
|
// hook at the front of the queue and discards it. After the queue is empty,
|
||||||
|
// the default hook function is invoked for any future action.
|
||||||
|
func (f *AccessTokensStoreGetBySHA1Func) PushHook(hook func(context.Context, string) (*AccessToken, error)) {
|
||||||
|
f.mutex.Lock()
|
||||||
|
f.hooks = append(f.hooks, hook)
|
||||||
|
f.mutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDefaultReturn calls SetDefaultHook with a function that returns the
|
||||||
|
// given values.
|
||||||
|
func (f *AccessTokensStoreGetBySHA1Func) SetDefaultReturn(r0 *AccessToken, r1 error) {
|
||||||
|
f.SetDefaultHook(func(context.Context, string) (*AccessToken, error) {
|
||||||
|
return r0, r1
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ TwoFactorsStore = (*MockTwoFactorsStore)(nil)
|
// PushReturn calls PushHook with a function that returns the given values.
|
||||||
|
func (f *AccessTokensStoreGetBySHA1Func) PushReturn(r0 *AccessToken, r1 error) {
|
||||||
type MockTwoFactorsStore struct {
|
f.PushHook(func(context.Context, string) (*AccessToken, error) {
|
||||||
MockCreate func(userID int64, key, secret string) error
|
return r0, r1
|
||||||
MockGetByUserID func(userID int64) (*TwoFactor, error)
|
|
||||||
MockIsUserEnabled func(userID int64) bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockTwoFactorsStore) Create(userID int64, key, secret string) error {
|
|
||||||
return m.MockCreate(userID, key, secret)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockTwoFactorsStore) GetByUserID(userID int64) (*TwoFactor, error) {
|
|
||||||
return m.MockGetByUserID(userID)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockTwoFactorsStore) IsUserEnabled(userID int64) bool {
|
|
||||||
return m.MockIsUserEnabled(userID)
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetMockTwoFactorsStore(t *testing.T, mock TwoFactorsStore) {
|
|
||||||
before := TwoFactors
|
|
||||||
TwoFactors = mock
|
|
||||||
t.Cleanup(func() {
|
|
||||||
TwoFactors = before
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ UsersStore = (*MockUsersStore)(nil)
|
func (f *AccessTokensStoreGetBySHA1Func) nextHook() func(context.Context, string) (*AccessToken, error) {
|
||||||
|
f.mutex.Lock()
|
||||||
|
defer f.mutex.Unlock()
|
||||||
|
|
||||||
type MockUsersStore struct {
|
if len(f.hooks) == 0 {
|
||||||
MockAuthenticate func(username, password string, loginSourceID int64) (*User, error)
|
return f.defaultHook
|
||||||
MockCreate func(username, email string, opts CreateUserOpts) (*User, error)
|
|
||||||
MockGetByEmail func(email string) (*User, error)
|
|
||||||
MockGetByID func(id int64) (*User, error)
|
|
||||||
MockGetByUsername func(username string) (*User, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockUsersStore) Authenticate(username, password string, loginSourceID int64) (*User, error) {
|
hook := f.hooks[0]
|
||||||
return m.MockAuthenticate(username, password, loginSourceID)
|
f.hooks = f.hooks[1:]
|
||||||
|
return hook
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockUsersStore) Create(username, email string, opts CreateUserOpts) (*User, error) {
|
func (f *AccessTokensStoreGetBySHA1Func) appendCall(r0 AccessTokensStoreGetBySHA1FuncCall) {
|
||||||
return m.MockCreate(username, email, opts)
|
f.mutex.Lock()
|
||||||
|
f.history = append(f.history, r0)
|
||||||
|
f.mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockUsersStore) GetByEmail(email string) (*User, error) {
|
// History returns a sequence of AccessTokensStoreGetBySHA1FuncCall objects
|
||||||
return m.MockGetByEmail(email)
|
// describing the invocations of this function.
|
||||||
|
func (f *AccessTokensStoreGetBySHA1Func) History() []AccessTokensStoreGetBySHA1FuncCall {
|
||||||
|
f.mutex.Lock()
|
||||||
|
history := make([]AccessTokensStoreGetBySHA1FuncCall, len(f.history))
|
||||||
|
copy(history, f.history)
|
||||||
|
f.mutex.Unlock()
|
||||||
|
|
||||||
|
return history
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockUsersStore) GetByID(id int64) (*User, error) {
|
// AccessTokensStoreGetBySHA1FuncCall is an object that describes an
|
||||||
return m.MockGetByID(id)
|
// invocation of method GetBySHA1 on an instance of MockAccessTokensStore.
|
||||||
|
type AccessTokensStoreGetBySHA1FuncCall struct {
|
||||||
|
// Arg0 is the value of the 1st argument passed to this method
|
||||||
|
// invocation.
|
||||||
|
Arg0 context.Context
|
||||||
|
// Arg1 is the value of the 2nd argument passed to this method
|
||||||
|
// invocation.
|
||||||
|
Arg1 string
|
||||||
|
// Result0 is the value of the 1st result returned from this method
|
||||||
|
// invocation.
|
||||||
|
Result0 *AccessToken
|
||||||
|
// Result1 is the value of the 2nd result returned from this method
|
||||||
|
// invocation.
|
||||||
|
Result1 error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockUsersStore) GetByUsername(username string) (*User, error) {
|
// Args returns an interface slice containing the arguments of this
|
||||||
return m.MockGetByUsername(username)
|
// invocation.
|
||||||
|
func (c AccessTokensStoreGetBySHA1FuncCall) Args() []interface{} {
|
||||||
|
return []interface{}{c.Arg0, c.Arg1}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetMockUsersStore(t *testing.T, mock UsersStore) {
|
// Results returns an interface slice containing the results of this
|
||||||
before := Users
|
// invocation.
|
||||||
Users = mock
|
func (c AccessTokensStoreGetBySHA1FuncCall) Results() []interface{} {
|
||||||
t.Cleanup(func() {
|
return []interface{}{c.Result0, c.Result1}
|
||||||
Users = before
|
}
|
||||||
|
|
||||||
|
// AccessTokensStoreListFunc describes the behavior when the List method of
|
||||||
|
// the parent MockAccessTokensStore instance is invoked.
|
||||||
|
type AccessTokensStoreListFunc struct {
|
||||||
|
defaultHook func(context.Context, int64) ([]*AccessToken, error)
|
||||||
|
hooks []func(context.Context, int64) ([]*AccessToken, error)
|
||||||
|
history []AccessTokensStoreListFuncCall
|
||||||
|
mutex sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// List delegates to the next hook function in the queue and stores the
|
||||||
|
// parameter and result values of this invocation.
|
||||||
|
func (m *MockAccessTokensStore) List(v0 context.Context, v1 int64) ([]*AccessToken, error) {
|
||||||
|
r0, r1 := m.ListFunc.nextHook()(v0, v1)
|
||||||
|
m.ListFunc.appendCall(AccessTokensStoreListFuncCall{v0, v1, r0, r1})
|
||||||
|
return r0, r1
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDefaultHook sets function that is called when the List method of the
|
||||||
|
// parent MockAccessTokensStore instance is invoked and the hook queue is
|
||||||
|
// empty.
|
||||||
|
func (f *AccessTokensStoreListFunc) SetDefaultHook(hook func(context.Context, int64) ([]*AccessToken, error)) {
|
||||||
|
f.defaultHook = hook
|
||||||
|
}
|
||||||
|
|
||||||
|
// PushHook adds a function to the end of hook queue. Each invocation of the
|
||||||
|
// List method of the parent MockAccessTokensStore instance invokes the hook
|
||||||
|
// at the front of the queue and discards it. After the queue is empty, the
|
||||||
|
// default hook function is invoked for any future action.
|
||||||
|
func (f *AccessTokensStoreListFunc) PushHook(hook func(context.Context, int64) ([]*AccessToken, error)) {
|
||||||
|
f.mutex.Lock()
|
||||||
|
f.hooks = append(f.hooks, hook)
|
||||||
|
f.mutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDefaultReturn calls SetDefaultHook with a function that returns the
|
||||||
|
// given values.
|
||||||
|
func (f *AccessTokensStoreListFunc) SetDefaultReturn(r0 []*AccessToken, r1 error) {
|
||||||
|
f.SetDefaultHook(func(context.Context, int64) ([]*AccessToken, error) {
|
||||||
|
return r0, r1
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PushReturn calls PushHook with a function that returns the given values.
|
||||||
|
func (f *AccessTokensStoreListFunc) PushReturn(r0 []*AccessToken, r1 error) {
|
||||||
|
f.PushHook(func(context.Context, int64) ([]*AccessToken, error) {
|
||||||
|
return r0, r1
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *AccessTokensStoreListFunc) nextHook() func(context.Context, int64) ([]*AccessToken, error) {
|
||||||
|
f.mutex.Lock()
|
||||||
|
defer f.mutex.Unlock()
|
||||||
|
|
||||||
|
if len(f.hooks) == 0 {
|
||||||
|
return f.defaultHook
|
||||||
|
}
|
||||||
|
|
||||||
|
hook := f.hooks[0]
|
||||||
|
f.hooks = f.hooks[1:]
|
||||||
|
return hook
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *AccessTokensStoreListFunc) appendCall(r0 AccessTokensStoreListFuncCall) {
|
||||||
|
f.mutex.Lock()
|
||||||
|
f.history = append(f.history, r0)
|
||||||
|
f.mutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// History returns a sequence of AccessTokensStoreListFuncCall objects
|
||||||
|
// describing the invocations of this function.
|
||||||
|
func (f *AccessTokensStoreListFunc) History() []AccessTokensStoreListFuncCall {
|
||||||
|
f.mutex.Lock()
|
||||||
|
history := make([]AccessTokensStoreListFuncCall, len(f.history))
|
||||||
|
copy(history, f.history)
|
||||||
|
f.mutex.Unlock()
|
||||||
|
|
||||||
|
return history
|
||||||
|
}
|
||||||
|
|
||||||
|
// AccessTokensStoreListFuncCall is an object that describes an invocation
|
||||||
|
// of method List on an instance of MockAccessTokensStore.
|
||||||
|
type AccessTokensStoreListFuncCall struct {
|
||||||
|
// Arg0 is the value of the 1st argument passed to this method
|
||||||
|
// invocation.
|
||||||
|
Arg0 context.Context
|
||||||
|
// Arg1 is the value of the 2nd argument passed to this method
|
||||||
|
// invocation.
|
||||||
|
Arg1 int64
|
||||||
|
// Result0 is the value of the 1st result returned from this method
|
||||||
|
// invocation.
|
||||||
|
Result0 []*AccessToken
|
||||||
|
// Result1 is the value of the 2nd result returned from this method
|
||||||
|
// invocation.
|
||||||
|
Result1 error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Args returns an interface slice containing the arguments of this
|
||||||
|
// invocation.
|
||||||
|
func (c AccessTokensStoreListFuncCall) Args() []interface{} {
|
||||||
|
return []interface{}{c.Arg0, c.Arg1}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Results returns an interface slice containing the results of this
|
||||||
|
// invocation.
|
||||||
|
func (c AccessTokensStoreListFuncCall) Results() []interface{} {
|
||||||
|
return []interface{}{c.Result0, c.Result1}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AccessTokensStoreSaveFunc describes the behavior when the Save method of
|
||||||
|
// the parent MockAccessTokensStore instance is invoked.
|
||||||
|
type AccessTokensStoreSaveFunc struct {
|
||||||
|
defaultHook func(context.Context, *AccessToken) error
|
||||||
|
hooks []func(context.Context, *AccessToken) error
|
||||||
|
history []AccessTokensStoreSaveFuncCall
|
||||||
|
mutex sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save delegates to the next hook function in the queue and stores the
|
||||||
|
// parameter and result values of this invocation.
|
||||||
|
func (m *MockAccessTokensStore) Save(v0 context.Context, v1 *AccessToken) error {
|
||||||
|
r0 := m.SaveFunc.nextHook()(v0, v1)
|
||||||
|
m.SaveFunc.appendCall(AccessTokensStoreSaveFuncCall{v0, v1, r0})
|
||||||
|
return r0
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDefaultHook sets function that is called when the Save method of the
|
||||||
|
// parent MockAccessTokensStore instance is invoked and the hook queue is
|
||||||
|
// empty.
|
||||||
|
func (f *AccessTokensStoreSaveFunc) SetDefaultHook(hook func(context.Context, *AccessToken) error) {
|
||||||
|
f.defaultHook = hook
|
||||||
|
}
|
||||||
|
|
||||||
|
// PushHook adds a function to the end of hook queue. Each invocation of the
|
||||||
|
// Save method of the parent MockAccessTokensStore instance invokes the hook
|
||||||
|
// at the front of the queue and discards it. After the queue is empty, the
|
||||||
|
// default hook function is invoked for any future action.
|
||||||
|
func (f *AccessTokensStoreSaveFunc) PushHook(hook func(context.Context, *AccessToken) error) {
|
||||||
|
f.mutex.Lock()
|
||||||
|
f.hooks = append(f.hooks, hook)
|
||||||
|
f.mutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDefaultReturn calls SetDefaultHook with a function that returns the
|
||||||
|
// given values.
|
||||||
|
func (f *AccessTokensStoreSaveFunc) SetDefaultReturn(r0 error) {
|
||||||
|
f.SetDefaultHook(func(context.Context, *AccessToken) error {
|
||||||
|
return r0
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// PushReturn calls PushHook with a function that returns the given values.
|
||||||
|
func (f *AccessTokensStoreSaveFunc) PushReturn(r0 error) {
|
||||||
|
f.PushHook(func(context.Context, *AccessToken) error {
|
||||||
|
return r0
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *AccessTokensStoreSaveFunc) nextHook() func(context.Context, *AccessToken) error {
|
||||||
|
f.mutex.Lock()
|
||||||
|
defer f.mutex.Unlock()
|
||||||
|
|
||||||
|
if len(f.hooks) == 0 {
|
||||||
|
return f.defaultHook
|
||||||
|
}
|
||||||
|
|
||||||
|
hook := f.hooks[0]
|
||||||
|
f.hooks = f.hooks[1:]
|
||||||
|
return hook
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *AccessTokensStoreSaveFunc) appendCall(r0 AccessTokensStoreSaveFuncCall) {
|
||||||
|
f.mutex.Lock()
|
||||||
|
f.history = append(f.history, r0)
|
||||||
|
f.mutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// History returns a sequence of AccessTokensStoreSaveFuncCall objects
|
||||||
|
// describing the invocations of this function.
|
||||||
|
func (f *AccessTokensStoreSaveFunc) History() []AccessTokensStoreSaveFuncCall {
|
||||||
|
f.mutex.Lock()
|
||||||
|
history := make([]AccessTokensStoreSaveFuncCall, len(f.history))
|
||||||
|
copy(history, f.history)
|
||||||
|
f.mutex.Unlock()
|
||||||
|
|
||||||
|
return history
|
||||||
|
}
|
||||||
|
|
||||||
|
// AccessTokensStoreSaveFuncCall is an object that describes an invocation
|
||||||
|
// of method Save on an instance of MockAccessTokensStore.
|
||||||
|
type AccessTokensStoreSaveFuncCall struct {
|
||||||
|
// Arg0 is the value of the 1st argument passed to this method
|
||||||
|
// invocation.
|
||||||
|
Arg0 context.Context
|
||||||
|
// Arg1 is the value of the 2nd argument passed to this method
|
||||||
|
// invocation.
|
||||||
|
Arg1 *AccessToken
|
||||||
|
// Result0 is the value of the 1st result returned from this method
|
||||||
|
// invocation.
|
||||||
|
Result0 error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Args returns an interface slice containing the arguments of this
|
||||||
|
// invocation.
|
||||||
|
func (c AccessTokensStoreSaveFuncCall) Args() []interface{} {
|
||||||
|
return []interface{}{c.Arg0, c.Arg1}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Results returns an interface slice containing the results of this
|
||||||
|
// invocation.
|
||||||
|
func (c AccessTokensStoreSaveFuncCall) Results() []interface{} {
|
||||||
|
return []interface{}{c.Result0}
|
||||||
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func ListAccessTokens(c *context.APIContext) {
|
func ListAccessTokens(c *context.APIContext) {
|
||||||
tokens, err := db.AccessTokens.List(c.User.ID)
|
tokens, err := db.AccessTokens.List(c.Req.Context(), c.User.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Error(err, "list access tokens")
|
c.Error(err, "list access tokens")
|
||||||
return
|
return
|
||||||
|
@ -28,7 +28,7 @@ func ListAccessTokens(c *context.APIContext) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateAccessToken(c *context.APIContext, form api.CreateAccessTokenOption) {
|
func CreateAccessToken(c *context.APIContext, form api.CreateAccessTokenOption) {
|
||||||
t, err := db.AccessTokens.Create(c.User.ID, form.Name)
|
t, err := db.AccessTokens.Create(c.Req.Context(), c.User.ID, form.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if db.IsErrAccessTokenAlreadyExist(err) {
|
if db.IsErrAccessTokenAlreadyExist(err) {
|
||||||
c.ErrorStatus(http.StatusUnprocessableEntity, err)
|
c.ErrorStatus(http.StatusUnprocessableEntity, err)
|
||||||
|
|
|
@ -72,7 +72,7 @@ func authenticate() macaron.Handler {
|
||||||
|
|
||||||
// If username and password authentication failed, try again using username as an access token.
|
// If username and password authentication failed, try again using username as an access token.
|
||||||
if auth.IsErrBadCredentials(err) {
|
if auth.IsErrBadCredentials(err) {
|
||||||
token, err := db.AccessTokens.GetBySHA1(username)
|
token, err := db.AccessTokens.GetBySHA1(c.Req.Context(), username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if db.IsErrAccessTokenNotExist(err) {
|
if db.IsErrAccessTokenNotExist(err) {
|
||||||
askCredentials(c.Resp)
|
askCredentials(c.Resp)
|
||||||
|
@ -82,7 +82,7 @@ func authenticate() macaron.Handler {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err = db.AccessTokens.Save(token); err != nil {
|
if err = db.AccessTokens.Save(c.Req.Context(), token); err != nil {
|
||||||
log.Error("Failed to update access token: %v", err)
|
log.Error("Failed to update access token: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,7 @@ func Test_authenticate(t *testing.T) {
|
||||||
header http.Header
|
header http.Header
|
||||||
mockUsersStore *db.MockUsersStore
|
mockUsersStore *db.MockUsersStore
|
||||||
mockTwoFactorsStore *db.MockTwoFactorsStore
|
mockTwoFactorsStore *db.MockTwoFactorsStore
|
||||||
mockAccessTokensStore *db.MockAccessTokensStore
|
mockAccessTokensStore func() db.AccessTokensStore
|
||||||
expStatusCode int
|
expStatusCode int
|
||||||
expHeader http.Header
|
expHeader http.Header
|
||||||
expBody string
|
expBody string
|
||||||
|
@ -74,10 +74,10 @@ func Test_authenticate(t *testing.T) {
|
||||||
return nil, auth.ErrBadCredentials{}
|
return nil, auth.ErrBadCredentials{}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
mockAccessTokensStore: &db.MockAccessTokensStore{
|
mockAccessTokensStore: func() db.AccessTokensStore {
|
||||||
MockGetBySHA1: func(sha string) (*db.AccessToken, error) {
|
mock := db.NewMockAccessTokensStore()
|
||||||
return nil, db.ErrAccessTokenNotExist{}
|
mock.GetBySHA1Func.SetDefaultReturn(nil, db.ErrAccessTokenNotExist{})
|
||||||
},
|
return mock
|
||||||
},
|
},
|
||||||
expStatusCode: http.StatusUnauthorized,
|
expStatusCode: http.StatusUnauthorized,
|
||||||
expHeader: http.Header{
|
expHeader: http.Header{
|
||||||
|
@ -119,13 +119,10 @@ func Test_authenticate(t *testing.T) {
|
||||||
return &db.User{ID: 1, Name: "unknwon"}, nil
|
return &db.User{ID: 1, Name: "unknwon"}, nil
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
mockAccessTokensStore: &db.MockAccessTokensStore{
|
mockAccessTokensStore: func() db.AccessTokensStore {
|
||||||
MockGetBySHA1: func(sha string) (*db.AccessToken, error) {
|
mock := db.NewMockAccessTokensStore()
|
||||||
return &db.AccessToken{}, nil
|
mock.GetBySHA1Func.SetDefaultReturn(&db.AccessToken{}, nil)
|
||||||
},
|
return mock
|
||||||
MockSave: func(t *db.AccessToken) error {
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
expStatusCode: http.StatusOK,
|
expStatusCode: http.StatusOK,
|
||||||
expHeader: http.Header{},
|
expHeader: http.Header{},
|
||||||
|
@ -136,7 +133,10 @@ func Test_authenticate(t *testing.T) {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
db.SetMockUsersStore(t, test.mockUsersStore)
|
db.SetMockUsersStore(t, test.mockUsersStore)
|
||||||
db.SetMockTwoFactorsStore(t, test.mockTwoFactorsStore)
|
db.SetMockTwoFactorsStore(t, test.mockTwoFactorsStore)
|
||||||
db.SetMockAccessTokensStore(t, test.mockAccessTokensStore)
|
|
||||||
|
if test.mockAccessTokensStore != nil {
|
||||||
|
db.SetMockAccessTokensStore(t, test.mockAccessTokensStore())
|
||||||
|
}
|
||||||
|
|
||||||
r, err := http.NewRequest("GET", "/", nil)
|
r, err := http.NewRequest("GET", "/", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -131,7 +131,7 @@ func HTTPContexter() macaron.Handler {
|
||||||
|
|
||||||
// If username and password combination failed, try again using username as a token.
|
// If username and password combination failed, try again using username as a token.
|
||||||
if authUser == nil {
|
if authUser == nil {
|
||||||
token, err := db.AccessTokens.GetBySHA1(authUsername)
|
token, err := db.AccessTokens.GetBySHA1(c.Req.Context(), authUsername)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if db.IsErrAccessTokenNotExist(err) {
|
if db.IsErrAccessTokenNotExist(err) {
|
||||||
askCredentials(c, http.StatusUnauthorized, "")
|
askCredentials(c, http.StatusUnauthorized, "")
|
||||||
|
@ -141,7 +141,7 @@ func HTTPContexter() macaron.Handler {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err = db.AccessTokens.Save(token); err != nil {
|
if err = db.AccessTokens.Save(c.Req.Context(), token); err != nil {
|
||||||
log.Error("Failed to update access token: %v", err)
|
log.Error("Failed to update access token: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -581,7 +581,7 @@ func SettingsApplications(c *context.Context) {
|
||||||
c.Title("settings.applications")
|
c.Title("settings.applications")
|
||||||
c.PageIs("SettingsApplications")
|
c.PageIs("SettingsApplications")
|
||||||
|
|
||||||
tokens, err := db.AccessTokens.List(c.User.ID)
|
tokens, err := db.AccessTokens.List(c.Req.Context(), c.User.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Errorf(err, "list access tokens")
|
c.Errorf(err, "list access tokens")
|
||||||
return
|
return
|
||||||
|
@ -596,7 +596,7 @@ func SettingsApplicationsPost(c *context.Context, f form.NewAccessToken) {
|
||||||
c.PageIs("SettingsApplications")
|
c.PageIs("SettingsApplications")
|
||||||
|
|
||||||
if c.HasError() {
|
if c.HasError() {
|
||||||
tokens, err := db.AccessTokens.List(c.User.ID)
|
tokens, err := db.AccessTokens.List(c.Req.Context(), c.User.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Errorf(err, "list access tokens")
|
c.Errorf(err, "list access tokens")
|
||||||
return
|
return
|
||||||
|
@ -607,7 +607,7 @@ func SettingsApplicationsPost(c *context.Context, f form.NewAccessToken) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
t, err := db.AccessTokens.Create(c.User.ID, f.Name)
|
t, err := db.AccessTokens.Create(c.Req.Context(), c.User.ID, f.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if db.IsErrAccessTokenAlreadyExist(err) {
|
if db.IsErrAccessTokenAlreadyExist(err) {
|
||||||
c.Flash.Error(c.Tr("settings.token_name_exists"))
|
c.Flash.Error(c.Tr("settings.token_name_exists"))
|
||||||
|
@ -624,7 +624,7 @@ func SettingsApplicationsPost(c *context.Context, f form.NewAccessToken) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func SettingsDeleteApplication(c *context.Context) {
|
func SettingsDeleteApplication(c *context.Context) {
|
||||||
if err := db.AccessTokens.DeleteByID(c.User.ID, c.QueryInt64("id")); err != nil {
|
if err := db.AccessTokens.DeleteByID(c.Req.Context(), c.User.ID, c.QueryInt64("id")); err != nil {
|
||||||
c.Flash.Error("DeleteAccessTokenByID: " + err.Error())
|
c.Flash.Error("DeleteAccessTokenByID: " + err.Error())
|
||||||
} else {
|
} else {
|
||||||
c.Flash.Success(c.Tr("settings.delete_token_success"))
|
c.Flash.Success(c.Tr("settings.delete_token_success"))
|
||||||
|
|
Loading…
Reference in New Issue