parent
f35c585edf
commit
c3c0e92964
@ -0,0 +1,53 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/duo-labs/webauthn/webauthn"
|
||||
)
|
||||
|
||||
/*
|
||||
`webauthn.User` 接口的实现
|
||||
*/
|
||||
|
||||
// WebAuthnID 返回用户ID
|
||||
func (user User) WebAuthnID() []byte {
|
||||
bs := make([]byte, 8)
|
||||
binary.LittleEndian.PutUint64(bs, uint64(user.ID))
|
||||
return bs
|
||||
}
|
||||
|
||||
// WebAuthnName 返回用户名
|
||||
func (user User) WebAuthnName() string {
|
||||
return user.Email
|
||||
}
|
||||
|
||||
// WebAuthnDisplayName 获得用于展示的用户名
|
||||
func (user User) WebAuthnDisplayName() string {
|
||||
return user.Nick
|
||||
}
|
||||
|
||||
// WebAuthnIcon 获得用户头像
|
||||
func (user User) WebAuthnIcon() string {
|
||||
return "https://cdn4.buysellads.net/uu/1/46074/1559075156-slack-carbon-red_2x.png"
|
||||
}
|
||||
|
||||
// WebAuthnCredentials 获得已注册的验证器凭证
|
||||
func (user User) WebAuthnCredentials() []webauthn.Credential {
|
||||
var res []webauthn.Credential
|
||||
err := json.Unmarshal([]byte(user.Authn), &res)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// RegisterAuthn 添加新的验证器
|
||||
func (user *User) RegisterAuthn(credential *webauthn.Credential) {
|
||||
res, err := json.Marshal([]webauthn.Credential{*credential})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
DB.Model(user).UpdateColumn("authn", string(res))
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/duo-labs/webauthn/webauthn"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUser_RegisterAuthn(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
credential := webauthn.Credential{}
|
||||
user := User{
|
||||
Model: gorm.Model{ID: 1},
|
||||
}
|
||||
|
||||
{
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
user.RegisterAuthn(&credential)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUser_WebAuthnCredentials(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
user := User{
|
||||
Model: gorm.Model{ID: 1},
|
||||
Authn: `[{"ID":"123","PublicKey":"+4sg1vYcjg/+=","AttestationType":"packed","Authenticator":{"AAGUID":"+lg==","SignCount":0,"CloneWarning":false}}]`,
|
||||
}
|
||||
{
|
||||
credentials := user.WebAuthnCredentials()
|
||||
asserts.Len(credentials, 1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUser_WebAuthnDisplayName(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
user := User{
|
||||
Model: gorm.Model{ID: 1},
|
||||
Nick: "123",
|
||||
}
|
||||
{
|
||||
nick := user.WebAuthnDisplayName()
|
||||
asserts.Equal("123", nick)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUser_WebAuthnIcon(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
user := User{
|
||||
Model: gorm.Model{ID: 1},
|
||||
}
|
||||
{
|
||||
icon := user.WebAuthnIcon()
|
||||
asserts.NotEmpty(icon)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUser_WebAuthnID(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
user := User{
|
||||
Model: gorm.Model{ID: 1},
|
||||
}
|
||||
{
|
||||
id := user.WebAuthnID()
|
||||
asserts.Len(id, 8)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUser_WebAuthnName(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
user := User{
|
||||
Model: gorm.Model{ID: 1},
|
||||
Email: "abslant@foxmail.com",
|
||||
}
|
||||
{
|
||||
name := user.WebAuthnName()
|
||||
asserts.Equal("abslant@foxmail.com", name)
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package authn
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestInit(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
asserts.NotPanics(func() {
|
||||
Init()
|
||||
})
|
||||
asserts.NotNil(AuthnInstance)
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package conf
|
||||
|
||||
import "github.com/mojocn/base64Captcha"
|
||||
|
||||
// RedisConfig Redis服务器配置
|
||||
var RedisConfig = &redis{
|
||||
Server: "",
|
||||
Password: "",
|
||||
DB: "0",
|
||||
}
|
||||
|
||||
// DatabaseConfig 数据库配置
|
||||
var DatabaseConfig = &database{
|
||||
Type: "UNSET",
|
||||
}
|
||||
|
||||
// SystemConfig 系统公用配置
|
||||
var SystemConfig = &system{
|
||||
Debug: false,
|
||||
}
|
||||
|
||||
// CaptchaConfig 验证码配置
|
||||
var CaptchaConfig = &captcha{
|
||||
Height: 60,
|
||||
Width: 240,
|
||||
Mode: 3,
|
||||
ComplexOfNoiseText: base64Captcha.CaptchaComplexLower,
|
||||
ComplexOfNoiseDot: base64Captcha.CaptchaComplexLower,
|
||||
IsShowHollowLine: false,
|
||||
IsShowNoiseDot: false,
|
||||
IsShowNoiseText: false,
|
||||
IsShowSlimeLine: false,
|
||||
IsShowSineLine: false,
|
||||
CaptchaLen: 6,
|
||||
}
|
||||
|
||||
var ThumbConfig = &thumb{
|
||||
MaxWidth: 400,
|
||||
MaxHeight: 300,
|
||||
FileSuffix: "._thumb",
|
||||
}
|
@ -0,0 +1,156 @@
|
||||
package filesystem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/conf"
|
||||
"github.com/HFO4/cloudreve/pkg/filesystem/response"
|
||||
"github.com/HFO4/cloudreve/pkg/util"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
testMock "github.com/stretchr/testify/mock"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func CreateTestImage() *os.File {
|
||||
file, err := os.Create("TestFileSystem_GenerateThumbnail.jpeg")
|
||||
alpha := image.NewAlpha(image.Rect(0, 0, 500, 200))
|
||||
jpeg.Encode(file, alpha, nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
_, _ = file.Seek(0, 0)
|
||||
return file
|
||||
}
|
||||
|
||||
func TestFileSystem_GetThumb(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
fs := FileSystem{
|
||||
User: &model.User{
|
||||
Model: gorm.Model{ID: 1},
|
||||
},
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
// 正常
|
||||
{
|
||||
testHandler := new(FileHeaderMock)
|
||||
testHandler.On("Thumb", testMock.Anything, "123.jpg").Return(&response.ContentResponse{URL: "123"}, nil)
|
||||
fs.Handler = testHandler
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(10, 1).
|
||||
WillReturnRows(
|
||||
sqlmock.NewRows(
|
||||
[]string{"id", "pic_info", "source_name"}).
|
||||
AddRow(10, "10,10", "123.jpg"),
|
||||
)
|
||||
|
||||
res, err := fs.GetThumb(ctx, 10)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
testHandler.AssertExpectations(t)
|
||||
asserts.NoError(err)
|
||||
asserts.Equal("123", res.URL)
|
||||
}
|
||||
|
||||
// 文件不存在
|
||||
{
|
||||
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(10, 1).
|
||||
WillReturnRows(
|
||||
sqlmock.NewRows(
|
||||
[]string{"id", "pic_info", "source_name"}),
|
||||
)
|
||||
|
||||
_, err := fs.GetThumb(ctx, 10)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileSystem_GenerateThumbnail(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
fs := FileSystem{
|
||||
User: &model.User{
|
||||
Model: gorm.Model{ID: 1},
|
||||
},
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
// 成功
|
||||
{
|
||||
src := CreateTestImage()
|
||||
testHandler := new(FileHeaderMock)
|
||||
testHandler.On("Get", testMock.Anything, "TestFileSystem_GenerateThumbnail.jpeg").Return(src, nil)
|
||||
fs.Handler = testHandler
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
|
||||
file := &model.File{
|
||||
Name: "123.jpg",
|
||||
SourceName: "TestFileSystem_GenerateThumbnail.jpeg",
|
||||
}
|
||||
|
||||
fs.GenerateThumbnail(ctx, file)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
testHandler.AssertExpectations(t)
|
||||
asserts.True(util.Exists("TestFileSystem_GenerateThumbnail.jpeg" + conf.ThumbConfig.FileSuffix))
|
||||
|
||||
}
|
||||
|
||||
// 更新信息失败后删除文件
|
||||
{
|
||||
src := CreateTestImage()
|
||||
testHandler := new(FileHeaderMock)
|
||||
testHandler.On("Get", testMock.Anything, "TestFileSystem_GenerateThumbnail.jpeg").Return(src, nil)
|
||||
testHandler.On("Delete", testMock.Anything, testMock.Anything).Return([]string{}, nil)
|
||||
fs.Handler = testHandler
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").WillReturnError(errors.New("error"))
|
||||
mock.ExpectRollback()
|
||||
|
||||
file := &model.File{
|
||||
Name: "123.jpg",
|
||||
SourceName: "TestFileSystem_GenerateThumbnail.jpeg",
|
||||
}
|
||||
|
||||
fs.GenerateThumbnail(ctx, file)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
testHandler.AssertExpectations(t)
|
||||
|
||||
}
|
||||
|
||||
// 不能生成缩略图
|
||||
{
|
||||
file := &model.File{
|
||||
Name: "123.123",
|
||||
SourceName: "TestFileSystem_GenerateThumbnail.jpeg",
|
||||
}
|
||||
|
||||
fs.GenerateThumbnail(ctx, file)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestFileSystem_GenerateThumbnailSize(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
fs := FileSystem{
|
||||
User: &model.User{
|
||||
Model: gorm.Model{ID: 1},
|
||||
},
|
||||
}
|
||||
asserts.NotPanics(func() {
|
||||
_, _ = fs.GenerateThumbnailSize(0, 0)
|
||||
})
|
||||
|
||||
}
|
@ -0,0 +1,101 @@
|
||||
package thumb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/HFO4/cloudreve/pkg/util"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func CreateTestImage() *os.File {
|
||||
file, err := os.Create("TestNewThumbFromFile.jpeg")
|
||||
alpha := image.NewAlpha(image.Rect(0, 0, 500, 200))
|
||||
jpeg.Encode(file, alpha, nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
_, _ = file.Seek(0, 0)
|
||||
return file
|
||||
}
|
||||
|
||||
func TestNewThumbFromFile(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
file := CreateTestImage()
|
||||
defer file.Close()
|
||||
|
||||
// 无扩展名时
|
||||
{
|
||||
thumb, err := NewThumbFromFile(file, "123")
|
||||
asserts.Error(err)
|
||||
asserts.Nil(thumb)
|
||||
}
|
||||
|
||||
{
|
||||
thumb, err := NewThumbFromFile(file, "123.jpg")
|
||||
asserts.NoError(err)
|
||||
asserts.NotNil(thumb)
|
||||
}
|
||||
{
|
||||
thumb, err := NewThumbFromFile(file, "123.jpeg")
|
||||
asserts.Error(err)
|
||||
asserts.Nil(thumb)
|
||||
}
|
||||
{
|
||||
thumb, err := NewThumbFromFile(file, "123.png")
|
||||
asserts.Error(err)
|
||||
asserts.Nil(thumb)
|
||||
}
|
||||
{
|
||||
thumb, err := NewThumbFromFile(file, "123.gif")
|
||||
asserts.Error(err)
|
||||
asserts.Nil(thumb)
|
||||
}
|
||||
{
|
||||
thumb, err := NewThumbFromFile(file, "123.3211")
|
||||
asserts.Error(err)
|
||||
asserts.Nil(thumb)
|
||||
}
|
||||
}
|
||||
|
||||
func TestThumb_GetSize(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
file := CreateTestImage()
|
||||
defer file.Close()
|
||||
thumb, err := NewThumbFromFile(file, "123.jpg")
|
||||
asserts.NoError(err)
|
||||
|
||||
w, h := thumb.GetSize()
|
||||
asserts.Equal(500, w)
|
||||
asserts.Equal(200, h)
|
||||
}
|
||||
|
||||
func TestThumb_GetThumb(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
file := CreateTestImage()
|
||||
defer file.Close()
|
||||
thumb, err := NewThumbFromFile(file, "123.jpg")
|
||||
asserts.NoError(err)
|
||||
|
||||
asserts.NotPanics(func() {
|
||||
thumb.GetThumb(10, 10)
|
||||
})
|
||||
}
|
||||
|
||||
func TestThumb_Save(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
file := CreateTestImage()
|
||||
defer file.Close()
|
||||
thumb, err := NewThumbFromFile(file, "123.jpg")
|
||||
asserts.NoError(err)
|
||||
|
||||
err = thumb.Save("/:noteexist/")
|
||||
asserts.Error(err)
|
||||
|
||||
err = thumb.Save("TestThumb_Save.png")
|
||||
asserts.NoError(err)
|
||||
asserts.True(util.Exists("TestThumb_Save.png"))
|
||||
|
||||
}
|
Loading…
Reference in new issue