From 060e2008b76cc7e110df9db5459c3c372ac8d0b6 Mon Sep 17 00:00:00 2001 From: viktorstrate Date: Fri, 23 Apr 2021 23:07:18 +0200 Subject: [PATCH] Add tests for albums, users, site_info models --- api/graphql/models/album_test.go | 93 ++++++++++++++++++++++++++++ api/graphql/models/media_test.go | 41 ++++++++++-- api/graphql/models/site_info_test.go | 22 ++++++- api/graphql/models/user.go | 19 +----- api/graphql/models/user_test.go | 37 ++++++++++- 5 files changed, 189 insertions(+), 23 deletions(-) create mode 100644 api/graphql/models/album_test.go diff --git a/api/graphql/models/album_test.go b/api/graphql/models/album_test.go new file mode 100644 index 00000000..e73e8692 --- /dev/null +++ b/api/graphql/models/album_test.go @@ -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) + } + } + +} diff --git a/api/graphql/models/media_test.go b/api/graphql/models/media_test.go index 39ffac72..2cc40402 100644 --- a/api/graphql/models/media_test.go +++ b/api/graphql/models/media_test.go @@ -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) + +} diff --git a/api/graphql/models/site_info_test.go b/api/graphql/models/site_info_test.go index a947dce2..30ce0628 100644 --- a/api/graphql/models/site_info_test.go +++ b/api/graphql/models/site_info_test.go @@ -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) + } diff --git a/api/graphql/models/user.go b/api/graphql/models/user.go index 244221b4..b3879a19 100644 --- a/api/graphql/models/user.go +++ b/api/graphql/models/user.go @@ -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") } diff --git a/api/graphql/models/user_test.go b/api/graphql/models/user_test.go index a8232ffc..9f945132 100644 --- a/api/graphql/models/user_test.go +++ b/api/graphql/models/user_test.go @@ -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 := "" + 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())) }