photoprism/internal/api/album_test.go

135 lines
4 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)
2020-05-01 14:15:15 +02:00
result := PerformRequest(app, "GET", "/api/v1/albums/at9lxuqxpogaaba8")
2020-02-02 15:50:33 +01:00
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)
2020-05-01 14:15:15 +02:00
result := PerformRequest(app, "DELETE", "/api/v1/albums/at9lxuqxpogaaba9")
2020-02-02 15:50:33 +01:00
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)
2020-05-01 14:15:15 +02:00
result := PerformRequest(app, "POST", "/api/v1/albums/xxx/like")
2019-07-17 16:46:54 +02:00
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)
2020-05-01 14:15:15 +02:00
result := PerformRequest(app, "POST", "/api/v1/albums/at9lxuqxpogaaba7/like")
2020-02-02 15:50:33 +01:00
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
2020-05-01 14:15:15 +02:00
result := PerformRequest(app, "DELETE", "/api/v1/albums/at9lxuqxpogaaba8/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)
2020-05-01 14:15:15 +02:00
result := PerformRequest(app, "GET", "/api/v1/albums/at9lxuqxpogaaba8/download")
2020-02-02 18:41:03 +01:00
assert.Equal(t, http.StatusOK, result.Code)
})
2019-07-17 16:46:54 +02:00
}
func TestAlbumThumbnail(t *testing.T) {
t.Run("invalid type", func(t *testing.T) {
app, router, ctx := NewApiTest()
AlbumThumbnail(router, ctx)
2020-05-01 14:15:15 +02:00
result := PerformRequest(app, "GET", "/api/v1/albums/at9lxuqxpogaaba7/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)
})
}