增加单元测试

This commit is contained in:
xmdhs 2023-10-12 17:19:30 +08:00
parent 1cbcf7d686
commit 913815294b
No known key found for this signature in database
GPG Key ID: E809D6D43DEFCC95
4 changed files with 227 additions and 0 deletions

4
go.mod
View File

@ -17,6 +17,7 @@ require (
github.com/pelletier/go-toml/v2 v2.1.0
github.com/redis/go-redis/v9 v9.2.1
github.com/samber/lo v1.38.1
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.14.0
)
@ -25,6 +26,7 @@ require (
github.com/agext/levenshtein v1.2.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-openapi/inflect v0.19.0 // indirect
@ -35,10 +37,12 @@ require (
github.com/hashicorp/hcl/v2 v2.13.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/zclconf/go-cty v1.8.0 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

1
go.sum
View File

@ -127,6 +127,7 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1N
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

71
service/admin_test.go Normal file
View File

@ -0,0 +1,71 @@
package service
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xmdhs/authlib-skin/model"
)
func TestWebService_Auth(t *testing.T) {
ctx := context.Background()
err := webService.Reg(ctx, model.UserReg{
Email: "TestWebService_Auth@xmdhs.com",
Password: "TestWebService_Auth",
Name: "TestWebService_Auth",
CaptchaToken: "",
}, "127.0.1.0/24", "127.0.1.0")
require.Nil(t, err)
l, err := webService.Login(ctx, model.Login{
Email: "TestWebService_Auth@xmdhs.com",
Password: "TestWebService_Auth",
CaptchaToken: "",
}, "0.0.0.0")
require.Nil(t, err)
token, err := webService.Auth(ctx, l.Token)
require.Nil(t, err)
assert.Equal(t, token.Subject, l.UUID)
assert.Equal(t, token.Tid, "1")
type args struct {
ctx context.Context
token string
}
tests := []struct {
name string
w *WebService
args args
wantErr bool
}{
{
name: "some string",
w: webService,
args: args{
ctx: ctx,
token: "123213",
},
wantErr: true,
},
{
name: "valid jwt",
w: webService,
args: args{
ctx: ctx,
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjpbeyJ0b29sdHQiOiJodHRwczovL3Rvb2x0dC5jb20ifV0sImlhdCI6MTY5NzEwMjMzOCwiZXhwIjoxNjk3MTI2Mzk5LCJhdWQiOiIiLCJpc3MiOiIiLCJzdWIiOiIifQ.JTQWl1PEX8u7PhVc4dTtv1DRS6e1PbMDZNWOAFJmVqE",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if _, err := tt.w.Auth(tt.args.ctx, tt.args.token); (err != nil) != tt.wantErr {
t.Errorf("WebService.Reg() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

151
service/user_test.go Normal file
View File

@ -0,0 +1,151 @@
package service
import (
"context"
"crypto/rand"
"crypto/rsa"
"net/http"
"os"
"testing"
"github.com/samber/lo"
"github.com/xmdhs/authlib-skin/config"
"github.com/xmdhs/authlib-skin/db/cache"
"github.com/xmdhs/authlib-skin/db/ent"
"github.com/xmdhs/authlib-skin/db/ent/migrate"
"github.com/xmdhs/authlib-skin/model"
)
var webService *WebService
func TestMain(m *testing.M) {
ctx := context.Background()
clean := initWebService(ctx)
code := m.Run()
clean()
os.Exit(code)
}
func initWebService(ctx context.Context) func() {
c := lo.Must(ent.Open("mysql", "root:root@tcp(127.0.0.1)/test"))
lo.Must0(c.Schema.Create(context.Background(), migrate.WithForeignKeys(false), migrate.WithDropIndex(true), migrate.WithDropColumn(true)))
rsa4 := lo.Must(rsa.GenerateKey(rand.Reader, 4096))
webService = NewWebService(config.Default(), c, &http.Client{}, cache.NewFastCache(100000), rsa4)
return func() {
c.User.Delete().Exec(ctx)
c.Texture.Delete().Exec(ctx)
c.UserProfile.Delete().Exec(ctx)
c.UserTexture.Delete().Exec(ctx)
c.UserToken.Delete().Exec(ctx)
}
}
func TestWebService_Reg(t *testing.T) {
ctx := context.Background()
webService.config.MaxIpUser = 1
type args struct {
ctx context.Context
u model.UserReg
ipPrefix string
ip string
}
tests := []struct {
name string
w *WebService
args args
wantErr bool
}{
{
name: "1",
w: webService,
args: args{
ctx: ctx,
u: model.UserReg{
Email: "1@xmdhs.com",
Password: "123456",
Name: "111",
CaptchaToken: "",
},
ipPrefix: "127.0.0.0/24",
ip: "127.0.0.1",
},
wantErr: false,
},
{
name: "email duplicate",
w: webService,
args: args{
ctx: ctx,
u: model.UserReg{
Email: "1@xmdhs.com",
Password: "123456",
Name: "111",
CaptchaToken: "",
},
ipPrefix: "127.0.0.0/24",
ip: "127.0.0.1",
},
wantErr: true,
},
{
name: "name duplicate",
w: webService,
args: args{
ctx: ctx,
u: model.UserReg{
Email: "2@xmdhs.com",
Password: "123456",
Name: "111",
CaptchaToken: "",
},
ipPrefix: "127.0.0.0/24",
ip: "127.0.0.1",
},
wantErr: true,
},
{
name: "MaxIpUser",
w: webService,
args: args{
ctx: ctx,
u: model.UserReg{
Email: "3@xmdhs.com",
Password: "123456",
Name: "333",
CaptchaToken: "",
},
ipPrefix: "127.0.0.0/24",
ip: "127.0.0.1",
},
wantErr: true,
},
{
name: "MaxIpUser",
w: webService,
args: args{
ctx: ctx,
u: model.UserReg{
Email: "4@xmdhs.com",
Password: "123456",
Name: "444",
CaptchaToken: "",
},
ipPrefix: "127.0.0.2/24",
ip: "127.0.0.1",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.w.Reg(tt.args.ctx, tt.args.u, tt.args.ipPrefix, tt.args.ip); (err != nil) != tt.wantErr {
t.Errorf("WebService.Reg() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
webService.config.MaxIpUser = 0
}