From e6cc639040e475bc9086825c679242bce613710f Mon Sep 17 00:00:00 2001 From: Theresa Gresch Date: Tue, 14 Jul 2020 14:42:23 +0200 Subject: [PATCH] Backend: Add unit tests for internal/api --- internal/api/photo_test.go | 46 +++++++++++++++++++++++++++++ internal/entity/photo_fixtures.go | 48 +++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/internal/api/photo_test.go b/internal/api/photo_test.go index 5b2df5bbc..2626cc760 100644 --- a/internal/api/photo_test.go +++ b/internal/api/photo_test.go @@ -70,6 +70,13 @@ func TestGetPhotoDownload(t *testing.T) { r := PerformRequest(app, "GET", "/api/v1/photos/xxx/dl?t="+conf.DownloadToken()) assert.Equal(t, http.StatusNotFound, r.Code) }) + + t.Run("invalid token", func(t *testing.T) { + app, router, _ := NewApiTest() + GetPhotoDownload(router) + r := PerformRequest(app, "GET", "/api/v1/photos/pt9jtdre2lvl0yh7/dl?t=xxx") + assert.Equal(t, http.StatusForbidden, r.Code) + }) } func TestLikePhoto(t *testing.T) { @@ -136,3 +143,42 @@ func TestPhotoPrimary(t *testing.T) { assert.Equal(t, http.StatusNotFound, r.Code) }) } + +func TestGetPhotoYaml(t *testing.T) { + t.Run("success", func(t *testing.T) { + app, router, _ := NewApiTest() + GetPhotoYaml(router) + r := PerformRequest(app, "GET", "/api/v1/photos/pt9jtdre2lvl0yh7/yaml") + assert.Equal(t, http.StatusOK, r.Code) + }) + + t.Run("not existing photo", func(t *testing.T) { + app, router, _ := NewApiTest() + GetPhotoYaml(router) + r := PerformRequest(app, "GET", "/api/v1/photos/xxx/yaml") + assert.Equal(t, http.StatusNotFound, r.Code) + }) +} + +func TestApprovePhoto(t *testing.T) { + t.Run("existing photo", func(t *testing.T) { + app, router, _ := NewApiTest() + GetPhoto(router) + r3 := PerformRequest(app, "GET", "/api/v1/photos/pt9jtxrexxvl0y20") + val2 := gjson.Get(r3.Body.String(), "Quality") + assert.Equal(t, "1", val2.String()) + ApprovePhoto(router) + r := PerformRequest(app, "POST", "/api/v1/photos/pt9jtxrexxvl0y20/approve") + assert.Equal(t, http.StatusOK, r.Code) + r2 := PerformRequest(app, "GET", "/api/v1/photos/pt9jtxrexxvl0y20") + val := gjson.Get(r2.Body.String(), "Quality") + assert.Equal(t, "3", val.String()) + }) + + t.Run("not existing photo", func(t *testing.T) { + app, router, _ := NewApiTest() + ApprovePhoto(router) + r := PerformRequest(app, "POST", "/api/v1/photos/xxx/approve") + assert.Equal(t, http.StatusNotFound, r.Code) + }) +} diff --git a/internal/entity/photo_fixtures.go b/internal/entity/photo_fixtures.go index 05763abd2..85eb7a10c 100644 --- a/internal/entity/photo_fixtures.go +++ b/internal/entity/photo_fixtures.go @@ -1008,6 +1008,54 @@ var PhotoFixtures = PhotoMap{ EditedAt: nil, DeletedAt: nil, }, + "Photo20": { + ID: 1000020, + PhotoUID: "pt9jtxrexxvl0y20", + TakenAt: time.Date(2008, 1, 1, 0, 0, 0, 0, time.UTC), + TakenAtLocal: time.Time{}, + TakenSrc: "", + PhotoTitle: "", + TitleSrc: "", + PhotoPath: "1990/04", + PhotoName: "Photo03", + PhotoQuality: 1, + PhotoResolution: 2, + PhotoFavorite: false, + PhotoPrivate: false, + PhotoType: "image", + PhotoLat: 0, + PhotoLng: 0, + PhotoAltitude: 0, + PhotoIso: 0, + PhotoFocalLength: 0, + PhotoFNumber: 0, + PhotoExposure: "", + CameraSerial: "", + CameraSrc: "", + Place: &UnknownPlace, + Cell: &UnknownLocation, + PlaceID: UnknownPlace.ID, + CellID: UnknownLocation.ID, + PlaceSrc: "", + TimeZone: "", + PhotoCountry: UnknownPlace.CountryCode(), + PhotoYear: 1990, + PhotoMonth: 4, + Details: DetailsFixtures.Pointer("bridge", 1000019), + DescriptionSrc: "", + Camera: CameraFixtures.Pointer("canon-eos-6d"), + CameraID: CameraFixtures.Pointer("canon-eos-6d").ID, + Lens: LensFixtures.Pointer("lens-f-380"), + LensID: LensFixtures.Pointer("lens-f-380").ID, + Keywords: []Keyword{}, + Albums: []Album{}, + Files: []File{}, + Labels: []PhotoLabel{}, + CreatedAt: time.Date(2009, 1, 1, 0, 0, 0, 0, time.UTC), + UpdatedAt: time.Date(2008, 1, 1, 0, 0, 0, 0, time.UTC), + EditedAt: nil, + DeletedAt: nil, + }, } // CreatePhotoFixtures inserts known entities into the database for testing.