package apitest import ( "encoding/json" "fmt" "net/http" "net/http/httptest" "testing" "git.artlef.fr/PersonalLibraryManager/internal/testutils" "github.com/stretchr/testify/assert" ) type countResponse struct { Count int } func TestGetReadBooksCountHandler_Demo(t *testing.T) { router := testutils.TestSetup() token := testutils.ConnectDemoUser(router) req, _ := http.NewRequest("GET", "/mybooks/read/count", nil) req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token)) w := httptest.NewRecorder() router.ServeHTTP(w, req) var c countResponse err := json.Unmarshal(w.Body.Bytes(), &c) if err != nil { t.Error(err) } assert.Equal(t, 22, c.Count) } func TestGetWantReadBooksCountHandler_Demo(t *testing.T) { router := testutils.TestSetup() token := testutils.ConnectDemoUser(router) req, _ := http.NewRequest("GET", "/mybooks/wantread/count", nil) req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token)) w := httptest.NewRecorder() router.ServeHTTP(w, req) var c countResponse err := json.Unmarshal(w.Body.Bytes(), &c) if err != nil { t.Error(err) } assert.Equal(t, 2, c.Count) } func TestGetReadingBooksCountHandler_Demo(t *testing.T) { router := testutils.TestSetup() token := testutils.ConnectDemoUser(router) req, _ := http.NewRequest("GET", "/mybooks/reading/count", nil) req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token)) w := httptest.NewRecorder() router.ServeHTTP(w, req) var c countResponse err := json.Unmarshal(w.Body.Bytes(), &c) if err != nil { t.Error(err) } assert.Equal(t, 2, c.Count) } func TestGetAuthorsBooksCountHandler_Demo(t *testing.T) { router := testutils.TestSetup() token := testutils.ConnectDemoUser(router) req, _ := http.NewRequest("GET", "/author/1/books/count", nil) req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token)) w := httptest.NewRecorder() router.ServeHTTP(w, req) var c countResponse err := json.Unmarshal(w.Body.Bytes(), &c) if err != nil { t.Error(err) } assert.Equal(t, 3, c.Count) }