Integrate users and books

- new table userbook linking users and book
- moved rating to users book
- updated demo data and tests
- updated front to hide routes from non connected users
This commit is contained in:
2025-10-05 16:14:53 +02:00
parent f18e6a3ba8
commit cb1f974f02
16 changed files with 143 additions and 73 deletions

View File

@@ -35,32 +35,42 @@ func connectDemoUser(router *gin.Engine) string {
req, _ := http.NewRequest("POST", "/auth/login", strings.NewReader(loginJson))
router.ServeHTTP(w, req)
var parsedResponse loginResponse
body := w.Body.String()
err := json.Unmarshal([]byte(body), &parsedResponse)
err := json.Unmarshal(w.Body.Bytes(), &parsedResponse)
if err != nil {
log.Fatal(err)
}
return parsedResponse.Token
}
type bookUserGet struct {
Title string `json:"title" binding:"required,max=300"`
Author string `json:"author" binding:"max=100"`
Rating int `json:"rating" binding:"min=0,max=10"`
}
func TestGetBooksHandler(t *testing.T) {
router := testSetup()
w := httptest.NewRecorder()
token := connectDemoUser(router)
req, _ := http.NewRequest("GET", "/books", nil)
req, _ := http.NewRequest("GET", "/mybooks", nil)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
router.ServeHTTP(w, req)
var parsedResponse []bookUserGet
err := json.Unmarshal(w.Body.Bytes(), &parsedResponse)
if err != nil {
log.Fatal(err)
}
assert.Equal(t, 200, w.Code)
assert.Equal(t, 28, len(parsedResponse))
}
func TestPostBookHandler_Ok(t *testing.T) {
bookJson :=
`{
"title": "Le château",
"author": "Kafka",
"rating": 9
"author": "Kafka"
}`
testPostBookHandler(t, bookJson, 200)
}
@@ -76,18 +86,7 @@ func TestPostBookHandler_OkOnlyTitle(t *testing.T) {
func TestPostBookHandler_noTitle(t *testing.T) {
bookJson :=
`{
"author": "Kafka",
"rating": 9
}`
testPostBookHandler(t, bookJson, 400)
}
func TestPostBookHandler_WrongRating(t *testing.T) {
bookJson :=
`{
"title": "Le château",
"author": "Kafka",
"rating": 15
"author": "Kafka"
}`
testPostBookHandler(t, bookJson, 400)
}
@@ -96,8 +95,7 @@ func TestPostBookHandler_TitleTooLong(t *testing.T) {
bookJson :=
`{
"title": "Noisy outlaws, unfriendly blobs, and some other things that aren't as scary, maybe, depending on how you feel about lost lands, stray cellphones, creatures from the sky, parents who disappear in Peru, a man named Lars Farf, and one other story we couldn't quite finish, so maybe you could help us out.",
"author": "Eli Horowitz",
"rating": 2
"author": "Eli Horowitz"
}`
testPostBookHandler(t, bookJson, 400)
}
@@ -106,8 +104,7 @@ func TestPostBookHandler_AuthorTooLong(t *testing.T) {
bookJson :=
`{
"title": "something",
"author": "Wolfeschlegelsteinhausenbergerdorffwelchevoralternwarengewissenhaftschaferswessenschafewarenwohlgepflegeundsorgfaltigkeitbeschutzenvonangreifendurchihrraubgierigfeindewelchevoralternzwolftausendjahresvorandieerscheinenvanderersteerdemenschderraumschiffgebrauchlichtalsseinursprungvonkraftgestartseinlangefahrthinzwischensternartigraumaufdersuchenachdiesternwelchegehabtbewohnbarplanetenkreisedrehensichundwohinderneurassevonverstandigmenschlichkeitkonntefortpflanzenundsicherfreuenanlebenslanglichfreudeundruhemitnichteinfurchtvorangreifenvonandererintelligentgeschopfsvonhinzwischensternartigraum",
"rating": 2
"author": "Wolfeschlegelsteinhausenbergerdorffwelchevoralternwarengewissenhaftschaferswessenschafewarenwohlgepflegeundsorgfaltigkeitbeschutzenvonangreifendurchihrraubgierigfeindewelchevoralternzwolftausendjahresvorandieerscheinenvanderersteerdemenschderraumschiffgebrauchlichtalsseinursprungvonkraftgestartseinlangefahrthinzwischensternartigraumaufdersuchenachdiesternwelchegehabtbewohnbarplanetenkreisedrehensichundwohinderneurassevonverstandigmenschlichkeitkonntefortpflanzenundsicherfreuenanlebenslanglichfreudeundruhemitnichteinfurchtvorangreifenvonandererintelligentgeschopfsvonhinzwischensternartigraum"
}`
testPostBookHandler(t, bookJson, 400)
}