mirror of
https://git.vectorsigma.ru/public/photoview.git
synced 2026-08-03 20:49:02 +00:00
Add tests for albums, users, site_info models
This commit is contained in:
93
api/graphql/models/album_test.go
Normal file
93
api/graphql/models/album_test.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package models_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/photoview/photoview/api/graphql/models"
|
||||
"github.com/photoview/photoview/api/test_utils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAlbumGetChildren(t *testing.T) {
|
||||
db := test_utils.DatabaseTest(t)
|
||||
|
||||
rootAlbum := models.Album{
|
||||
Title: "root",
|
||||
Path: "/photos",
|
||||
}
|
||||
|
||||
if !assert.NoError(t, db.Save(&rootAlbum).Error) {
|
||||
return
|
||||
}
|
||||
|
||||
children := []models.Album{
|
||||
{
|
||||
Title: "child1",
|
||||
Path: "/photos/child1",
|
||||
ParentAlbumID: &rootAlbum.ID,
|
||||
},
|
||||
{
|
||||
Title: "child2",
|
||||
Path: "/photos/child2",
|
||||
ParentAlbumID: &rootAlbum.ID,
|
||||
},
|
||||
{
|
||||
Title: "not_child",
|
||||
Path: "/videos",
|
||||
},
|
||||
}
|
||||
|
||||
if !assert.NoError(t, db.Save(&children).Error) {
|
||||
return
|
||||
}
|
||||
|
||||
sub_child := models.Album{
|
||||
Title: "subchild",
|
||||
Path: "/photos/child1/subchild",
|
||||
ParentAlbumID: &children[0].ID,
|
||||
}
|
||||
|
||||
if !assert.NoError(t, db.Save(&sub_child).Error) {
|
||||
return
|
||||
}
|
||||
|
||||
root_children, err := rootAlbum.GetChildren(db, nil)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
expected_children := []*models.Album{
|
||||
{
|
||||
Title: "root",
|
||||
Path: "/photos",
|
||||
},
|
||||
{
|
||||
Title: "child1",
|
||||
Path: "/photos/child1",
|
||||
},
|
||||
{
|
||||
Title: "child2",
|
||||
Path: "/photos/child2",
|
||||
},
|
||||
{
|
||||
Title: "subchild",
|
||||
Path: "/photos/child1/subchild",
|
||||
},
|
||||
}
|
||||
|
||||
assert.Equal(t, len(expected_children), len(root_children))
|
||||
|
||||
for _, expected := range expected_children {
|
||||
found_expected := false
|
||||
for _, item := range root_children {
|
||||
if item.Title == expected.Title && item.Path == expected.Path {
|
||||
found_expected = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found_expected {
|
||||
assert.Failf(t, "root children did not match", "expected to find item: %v", expected)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,11 @@
|
||||
package models
|
||||
package models_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/photoview/photoview/api/graphql/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSanitizeMediaName(t *testing.T) {
|
||||
@@ -12,9 +16,36 @@ func TestSanitizeMediaName(t *testing.T) {
|
||||
{"..\\/", "__"},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
if SanitizeMediaName(test[0]) != test[1] {
|
||||
t.Errorf("SanitizeMediaName test %d failed: got '%s', expected '%s'", i, test[1], SanitizeMediaName(test[0]))
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("sanitize: %s", test[0]), func(t *testing.T) {
|
||||
assert.Equal(t, test[1], models.SanitizeMediaName(test[0]))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMediaURLCachePath(t *testing.T) {
|
||||
mediaUrl := models.MediaURL{}
|
||||
mediaUrl.Media = nil
|
||||
|
||||
_, err := mediaUrl.CachedPath()
|
||||
assert.EqualError(t, err, "mediaURL.Media is nil")
|
||||
|
||||
mediaUrl = models.MediaURL{
|
||||
Purpose: models.PhotoThumbnail,
|
||||
MediaID: 1,
|
||||
Media: &models.Media{
|
||||
Model: models.Model{
|
||||
ID: 1,
|
||||
},
|
||||
Title: "media.jpg",
|
||||
AlbumID: 2,
|
||||
},
|
||||
MediaName: "media_thumb.jpg",
|
||||
}
|
||||
|
||||
path, err := mediaUrl.CachedPath()
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "media_cache/2/1/media_thumb.jpg", path)
|
||||
|
||||
}
|
||||
|
||||
@@ -6,16 +6,36 @@ import (
|
||||
"github.com/photoview/photoview/api/graphql/models"
|
||||
"github.com/photoview/photoview/api/test_utils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestSiteInfo(t *testing.T) {
|
||||
db := test_utils.DatabaseTest(t)
|
||||
|
||||
site_info, err := models.GetSiteInfo(db)
|
||||
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
assert.Equal(t, models.DefaultSiteInfo(), *site_info)
|
||||
|
||||
site_info.InitialSetup = false
|
||||
site_info.PeriodicScanInterval = 360
|
||||
site_info.ConcurrentWorkers = 10
|
||||
|
||||
if !assert.NoError(t, db.Session(&gorm.Session{AllowGlobalUpdate: true}).Save(&site_info).Error) {
|
||||
return
|
||||
}
|
||||
|
||||
site_info, err = models.GetSiteInfo(db)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
assert.Equal(t, models.SiteInfo{
|
||||
InitialSetup: false,
|
||||
PeriodicScanInterval: 360,
|
||||
ConcurrentWorkers: 10,
|
||||
}, *site_info)
|
||||
|
||||
}
|
||||
|
||||
@@ -108,14 +108,9 @@ func ValidRootPath(rootPath string) bool {
|
||||
}
|
||||
|
||||
func RegisterUser(db *gorm.DB, username string, password *string, admin bool) (*User, error) {
|
||||
// if !ValidRootPath(rootPath) {
|
||||
// return nil, ErrorInvalidRootPath
|
||||
// }
|
||||
|
||||
user := User{
|
||||
Username: username,
|
||||
// RootPath: rootPath,
|
||||
Admin: admin,
|
||||
Admin: admin,
|
||||
}
|
||||
|
||||
if password != nil {
|
||||
@@ -148,11 +143,6 @@ func (user *User) GenerateAccessToken(db *gorm.DB) (*AccessToken, error) {
|
||||
|
||||
token_value := string(bytes)
|
||||
expire := time.Now().Add(14 * 24 * time.Hour)
|
||||
// expireString := expire.UTC().Format("2006-01-02 15:04:05")
|
||||
|
||||
// if _, err := database.Exec("INSERT INTO access_token (value, expire, user_id) VALUES (?, ?, ?)", token_value, expireString, user.UserID); err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
token := AccessToken{
|
||||
UserID: user.ID,
|
||||
@@ -183,14 +173,11 @@ func (user *User) FillAlbums(db *gorm.DB) error {
|
||||
}
|
||||
|
||||
func (user *User) OwnsAlbum(db *gorm.DB, album *Album) (bool, error) {
|
||||
|
||||
// user.QueryUserAlbums(db, db.Where("id = ?", album.ID))
|
||||
|
||||
// TODO: Implement this
|
||||
return true, nil
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (user *User) OwnsMedia(db *gorm.DB, media *Media) (bool, error) {
|
||||
// TODO: implement this
|
||||
return true, nil
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package models_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/photoview/photoview/api/graphql/models"
|
||||
"github.com/photoview/photoview/api/test_utils"
|
||||
@@ -13,13 +14,47 @@ func TestUserRegistrationAuthorization(t *testing.T) {
|
||||
|
||||
password := "1234"
|
||||
user, err := models.RegisterUser(db, "admin", &password, true)
|
||||
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
assert.NotNil(t, user)
|
||||
assert.EqualValues(t, "admin", user.Username)
|
||||
assert.NotNil(t, user.Password)
|
||||
assert.NotEqualValues(t, "1234", user.Password) // should be hashed
|
||||
assert.True(t, user.Admin)
|
||||
|
||||
user, err = models.AuthorizeUser(db, "admin", "1234")
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
assert.NotNil(t, user)
|
||||
assert.EqualValues(t, "admin", user.Username)
|
||||
|
||||
}
|
||||
|
||||
func TestAccessToken(t *testing.T) {
|
||||
db := test_utils.DatabaseTest(t)
|
||||
|
||||
pass := "<hashed_password>"
|
||||
user := models.User{
|
||||
Username: "user1",
|
||||
Password: &pass,
|
||||
Admin: false,
|
||||
}
|
||||
|
||||
if !assert.NoError(t, db.Save(&user).Error) {
|
||||
return
|
||||
}
|
||||
|
||||
access_token, err := user.GenerateAccessToken(db)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
assert.NotNil(t, access_token)
|
||||
assert.Equal(t, user.ID, access_token.UserID)
|
||||
assert.NotEmpty(t, access_token.Value)
|
||||
assert.True(t, access_token.Expire.After(time.Now()))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user