Backend: Add unit tests for internal/api

This commit is contained in:
Theresa Gresch 2020-07-14 14:42:23 +02:00
parent 6090f875db
commit e6cc639040
2 changed files with 94 additions and 0 deletions

View file

@ -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)
})
}

View file

@ -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.