photoprism/internal/api/album_test.go

141 lines
4.2 KiB
Go
Raw Normal View History

2019-07-17 16:46:54 +02:00
package api
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
2019-07-17 16:46:54 +02:00
)
func TestGetAlbums(t *testing.T) {
t.Run("successful request", func(t *testing.T) {
app, router, conf := NewApiTest()
GetAlbums(router, conf)
2019-07-17 16:46:54 +02:00
result := PerformRequest(app, "GET", "/api/v1/albums?count=10")
2020-02-02 15:50:33 +01:00
assert.Contains(t, result.Body.String(), "Christmas2030")
2019-07-17 16:46:54 +02:00
assert.Equal(t, http.StatusOK, result.Code)
})
t.Run("invalid request", func(t *testing.T) {
app, router, conf := NewApiTest()
GetAlbums(router, conf)
2019-07-17 16:46:54 +02:00
result := PerformRequest(app, "GET", "/api/v1/albums?xxx=10")
assert.Equal(t, http.StatusBadRequest, result.Code)
})
2020-02-02 15:50:33 +01:00
}
func TestGetAlbum(t *testing.T) {
t.Run("successful request", func(t *testing.T) {
app, router, conf := NewApiTest()
GetAlbum(router, conf)
result := PerformRequest(app, "GET", "/api/v1/albums/4")
assert.Contains(t, result.Body.String(), "holiday-2030")
assert.Equal(t, http.StatusOK, result.Code)
})
2019-07-17 16:46:54 +02:00
t.Run("invalid request", func(t *testing.T) {
app, router, conf := NewApiTest()
2020-02-02 15:50:33 +01:00
GetAlbum(router, conf)
result := PerformRequest(app, "GET", "/api/v1/albums/999000")
assert.Equal(t, http.StatusNotFound, result.Code)
})
}
2019-07-17 16:46:54 +02:00
2020-02-02 15:50:33 +01:00
func TestDeleteAlbum(t *testing.T) {
t.Run("delete existing album", func(t *testing.T) {
app, router, conf := NewApiTest()
DeleteAlbum(router, conf)
result := PerformRequest(app, "DELETE", "/api/v1/albums/5")
assert.Equal(t, http.StatusOK, result.Code)
assert.Contains(t, result.Body.String(), "Berlin2019")
GetAlbums(router, conf)
result2 := PerformRequest(app, "GET", "/api/v1/albums?count=10")
assert.NotContains(t, result2.Body.String(), "Berlin2019")
})
t.Run("delete not existing album", func(t *testing.T) {
app, router, conf := NewApiTest()
DeleteAlbum(router, conf)
result := PerformRequest(app, "DELETE", "/api/v1/albums/999000")
2019-07-17 16:46:54 +02:00
assert.Equal(t, http.StatusNotFound, result.Code)
})
}
func TestLikeAlbum(t *testing.T) {
t.Run("like not existing album", func(t *testing.T) {
app, router, ctx := NewApiTest()
LikeAlbum(router, ctx)
result := PerformRequest(app, "POST", "/api/v1/albums/98789876/like")
assert.Equal(t, http.StatusNotFound, result.Code)
})
2020-02-02 15:50:33 +01:00
t.Run("like existing album", func(t *testing.T) {
app, router, ctx := NewApiTest()
2019-07-17 16:46:54 +02:00
2020-02-02 15:50:33 +01:00
LikeAlbum(router, ctx)
result := PerformRequest(app, "POST", "/api/v1/albums/3/like")
assert.Equal(t, http.StatusOK, result.Code)
})
2019-07-17 16:46:54 +02:00
}
func TestDislikeAlbum(t *testing.T) {
t.Run("dislike not existing album", func(t *testing.T) {
app, router, conf := NewApiTest()
2019-07-17 16:46:54 +02:00
2020-02-02 18:41:03 +01:00
DislikeAlbum(router, conf)
2019-07-17 16:46:54 +02:00
2019-07-17 17:16:33 +02:00
result := PerformRequest(app, "DELETE", "/api/v1/albums/5678/like")
2020-02-02 15:50:33 +01:00
assert.Equal(t, http.StatusNotFound, result.Code)
})
t.Run("dislike existing album", func(t *testing.T) {
app, router, conf := NewApiTest()
2020-02-02 18:41:03 +01:00
DislikeAlbum(router, conf)
2020-02-02 15:50:33 +01:00
result := PerformRequest(app, "DELETE", "/api/v1/albums/4/like")
2020-02-02 18:41:03 +01:00
assert.Equal(t, http.StatusOK, result.Code)
})
}
func TestDownloadAlbum(t *testing.T) {
t.Run("download not existing album", func(t *testing.T) {
app, router, conf := NewApiTest()
DownloadAlbum(router, conf)
result := PerformRequest(app, "GET", "/api/v1/albums/5678/download")
2019-07-17 16:46:54 +02:00
assert.Equal(t, http.StatusNotFound, result.Code)
})
2020-02-02 18:41:03 +01:00
t.Run("download existing album", func(t *testing.T) {
app, router, conf := NewApiTest()
DownloadAlbum(router, conf)
result := PerformRequest(app, "GET", "/api/v1/albums/4/download")
assert.Equal(t, http.StatusOK, result.Code)
})
2019-07-17 16:46:54 +02:00
}
func TestAlbumThumbnail(t *testing.T) {
2020-02-02 18:41:03 +01:00
t.Run("could not find original", func(t *testing.T) {
app, router, ctx := NewApiTest()
AlbumThumbnail(router, ctx)
result := PerformRequest(app, "GET", "/api/v1/albums/4/thumbnail/tile_500")
assert.Equal(t, http.StatusNotFound, result.Code)
})
t.Run("invalid type", func(t *testing.T) {
app, router, ctx := NewApiTest()
AlbumThumbnail(router, ctx)
result := PerformRequest(app, "GET", "/api/v1/albums/1/thumbnail/xxx")
assert.Equal(t, http.StatusBadRequest, result.Code)
})
t.Run("album has no photo (because is not existing)", func(t *testing.T) {
app, router, ctx := NewApiTest()
AlbumThumbnail(router, ctx)
result := PerformRequest(app, "GET", "/api/v1/albums/987-986435/thumbnail/tile_500")
assert.Equal(t, http.StatusOK, result.Code)
})
}