Skip to content

Commit

Permalink
tests: book update & delete
Browse files Browse the repository at this point in the history
  • Loading branch information
kayprogrammer committed Jan 21, 2025
1 parent 1c92b0e commit e1e34cc
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 21 deletions.
2 changes: 1 addition & 1 deletion managers/books.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (b BookManager) GetByAuthorAndSlug(db *gorm.DB, author *models.User, slug s
book := models.Book{AuthorID: author.ID, Slug: slug}
db.Scopes(scopes.AuthorGenreTagBookScope).Preload("Chapters").Take(&book, book)
if book.ID == uuid.Nil {
errD := utils.RequestErr(utils.ERR_NON_EXISTENT, "Writer has no book with that slug")
errD := utils.RequestErr(utils.ERR_NON_EXISTENT, "Author has no book with that slug")
return nil, &errD
}
return &book, nil
Expand Down
2 changes: 1 addition & 1 deletion routes/books.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (ep Endpoint) DeleteBook(c *fiber.Ctx) error {
return c.Status(404).JSON(err)
}
db.Delete(&book)
return c.Status(200).JSON(ResponseMessage("Book deleted successfuly"))
return c.Status(200).JSON(ResponseMessage("Book deleted successfully"))
}

// @Summary Add A Chapter to a Book
Expand Down
10 changes: 5 additions & 5 deletions tests/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func sendPasswordResetLink(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl st
func verifyPasswordResetToken(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string) {
t.Run("Reject verification due to invalid token", func(t *testing.T) {
url := fmt.Sprintf("%s/verify-password-reset-token/invalid-token-string", baseUrl)
res := ProcessTestGet(app, url)
res := ProcessTestGetOrDelete(app, "GET", url)

// Assert Status code
assert.Equal(t, 404, res.StatusCode)
Expand All @@ -225,7 +225,7 @@ func verifyPasswordResetToken(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl
db.Save(&user)

url := fmt.Sprintf("%s/verify-password-reset-token/%s", baseUrl, *user.TokenString)
res := ProcessTestGet(app, url)
res := ProcessTestGetOrDelete(app, "GET", url)

// Assert Status code
assert.Equal(t, 400, res.StatusCode)
Expand All @@ -242,7 +242,7 @@ func verifyPasswordResetToken(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl
db.Save(&user)

url := fmt.Sprintf("%s/verify-password-reset-token/%s", baseUrl, *user.TokenString)
res := ProcessTestGet(app, url)
res := ProcessTestGetOrDelete(app, "GET", url)

// Assert Status code
assert.Equal(t, 200, res.StatusCode)
Expand Down Expand Up @@ -441,7 +441,7 @@ func refresh(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string) {
func logout(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string) {
t.Run("Reject Logout Due To Invalid Token", func(t *testing.T) {
url := fmt.Sprintf("%s/logout", baseUrl)
res := ProcessTestGet(app, url, "invalid_token")
res := ProcessTestGetOrDelete(app, url, "GET", "invalid_token")
// Assert Status code
assert.Equal(t, 401, res.StatusCode)

Expand All @@ -454,7 +454,7 @@ func logout(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string) {
t.Run("Accept Logout Due To Valid Token", func(t *testing.T) {
url := fmt.Sprintf("%s/logout", baseUrl)
token := AccessToken(db, TestVerifiedUser(db))
res := ProcessTestGet(app, url, token)
res := ProcessTestGetOrDelete(app, url, "GET", token)
// Assert Status code
assert.Equal(t, 200, res.StatusCode)

Expand Down
76 changes: 64 additions & 12 deletions tests/books_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func getBookTags(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string) {
t.Run("Accept Book Tags Fetch", func(t *testing.T) {
TagData(db) // Get or create tag
url := fmt.Sprintf("%s/tags", baseUrl)
res := ProcessTestGet(app, url)
res := ProcessTestGetOrDelete(app, url, "GET")
// Assert Status code
assert.Equal(t, 200, res.StatusCode)

Expand All @@ -32,7 +32,7 @@ func getBookGenres(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string) {
t.Run("Accept Book Genres Fetch", func(t *testing.T) {
GenreData(db) // Get or create tag
url := fmt.Sprintf("%s/genres", baseUrl)
res := ProcessTestGet(app, url)
res := ProcessTestGetOrDelete(app, url, "GET")
// Assert Status code
assert.Equal(t, 200, res.StatusCode)

Expand All @@ -48,7 +48,7 @@ func getBooks(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string) {
BookData(db, user) // Get or create book
t.Run("Reject Books Fetch Due To Invalid Genre Slug", func(t *testing.T) {
url := fmt.Sprintf("%s?genre_slug=invalid-genre", baseUrl)
res := ProcessTestGet(app, url)
res := ProcessTestGetOrDelete(app, url, "GET")
// Assert Status code
assert.Equal(t, 404, res.StatusCode)

Expand All @@ -59,7 +59,7 @@ func getBooks(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string) {
})

t.Run("Accept Books Fetch", func(t *testing.T) {
res := ProcessTestGet(app, baseUrl)
res := ProcessTestGetOrDelete(app, baseUrl, "GET")
// Assert Status code
assert.Equal(t, 200, res.StatusCode)

Expand All @@ -73,7 +73,7 @@ func getBooks(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string) {
func getBooksByAuthor(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string) {
t.Run("Reject Books Fetch Due To Invalid Username", func(t *testing.T) {
url := fmt.Sprintf("%s/author/invalid-username", baseUrl)
res := ProcessTestGet(app, url)
res := ProcessTestGetOrDelete(app, url, "GET")
// Assert Status code
assert.Equal(t, 404, res.StatusCode)

Expand All @@ -87,7 +87,7 @@ func getBooksByAuthor(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string)
user := TestAuthor(db)
BookData(db, user) // Get or create book
url := fmt.Sprintf("%s/author/%s", baseUrl, user.Username)
res := ProcessTestGet(app, url)
res := ProcessTestGetOrDelete(app, url, "GET")
// Assert Status code
assert.Equal(t, 200, res.StatusCode)

Expand All @@ -101,7 +101,7 @@ func getBooksByAuthor(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string)
func getBookChapters(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string) {
t.Run("Reject Book Chapters Fetch Due To Invalid Slug", func(t *testing.T) {
url := fmt.Sprintf("%s/book/invalid-slug/chapters", baseUrl)
res := ProcessTestGet(app, url)
res := ProcessTestGetOrDelete(app, url, "GET")
// Assert Status code
assert.Equal(t, 404, res.StatusCode)

Expand All @@ -116,7 +116,7 @@ func getBookChapters(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string)
book := BookData(db, user) // Get or create book
ChapterData(db, book)
url := fmt.Sprintf("%s/book/%s/chapters", baseUrl, book.Slug)
res := ProcessTestGet(app, url)
res := ProcessTestGetOrDelete(app, url, "GET")
// Assert Status code
assert.Equal(t, 200, res.StatusCode)

Expand All @@ -130,7 +130,7 @@ func getBookChapters(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string)
func getBook(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string) {
t.Run("Reject Book Details Fetch Due To Invalid Slug", func(t *testing.T) {
url := fmt.Sprintf("%s/book/invalid-slug", baseUrl)
res := ProcessTestGet(app, url)
res := ProcessTestGetOrDelete(app, url, "GET")
// Assert Status code
assert.Equal(t, 404, res.StatusCode)

Expand All @@ -145,7 +145,7 @@ func getBook(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string) {
book := BookData(db, user) // Get or create book
ChapterData(db, book)
url := fmt.Sprintf("%s/book/%s", baseUrl, book.Slug)
res := ProcessTestGet(app, url)
res := ProcessTestGetOrDelete(app, url, "GET")
// Assert Status code
assert.Equal(t, 200, res.StatusCode)

Expand Down Expand Up @@ -209,18 +209,68 @@ func createBook(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string) {
tempFilePath := CreateTempImageFile(t)
defer os.Remove(tempFilePath)
res := ProcessMultipartTestBody(t, app, baseUrl, "POST", bookData, "cover_image", tempFilePath, token)
t.Log("RESP: ", res)
// Assert Status code
assert.Equal(t, 201, res.StatusCode)

// Parse and assert body
body := ParseResponseBody(t, res.Body).(map[string]interface{})
t.Log(body["data"])
assert.Equal(t, "success", body["status"])
assert.Equal(t, "Book created successfully", body["message"])
})
}

func updateBook(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string) {
genre := GenreData(db)
bookData := schemas.BookCreateSchema{
Title: "Test Book Title Updated", Blurb: "Test Blurb Updated",
GenreSlug: genre.Slug, TagSlugs: []string{genre.Tags[0].Slug}, AgeDiscretion: choices.ATYPE_EIGHTEEN,
}
author := TestAuthor(db)
token := AccessToken(db, author)
book := BookData(db, author)

t.Run("Reject Book Update Due To Invalid Slug", func(t *testing.T) {
url := fmt.Sprintf("%s/book/invalid-slug", baseUrl)
res := ProcessMultipartTestBody(t, app, url, "PUT", bookData, "", "", token)
assert.Equal(t, 404, res.StatusCode)

// Parse and assert body
body := ParseResponseBody(t, res.Body).(map[string]interface{})
assert.Equal(t, "failure", body["status"])
assert.Equal(t, "Author has no book with that slug", body["message"])
})

t.Run("Accept Book Update Due To Valid Data", func(t *testing.T) {
url := fmt.Sprintf("%s/book/%s", baseUrl, book.Slug)
res := ProcessMultipartTestBody(t, app, url, "PUT", bookData, "", "", token)
// Assert Status code
assert.Equal(t, 200, res.StatusCode)

// Parse and assert body
body := ParseResponseBody(t, res.Body).(map[string]interface{})
assert.Equal(t, "success", body["status"])
assert.Equal(t, "Book updated successfully", body["message"])
})
}

func deleteBook(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string) {
author := TestAuthor(db)
token := AccessToken(db, author)
book := BookData(db, author)

t.Run("Accept Book Delete Due To Valid Data", func(t *testing.T) {
url := fmt.Sprintf("%s/book/%s", baseUrl, book.Slug)
res := ProcessTestGetOrDelete(app, url, "DELETE", token)
// Assert Status code
assert.Equal(t, 200, res.StatusCode)

// Parse and assert body
body := ParseResponseBody(t, res.Body).(map[string]interface{})
assert.Equal(t, "success", body["status"])
assert.Equal(t, "Book deleted successfully", body["message"])
})
}


func TestBooks(t *testing.T) {
app := fiber.New()
Expand All @@ -235,6 +285,8 @@ func TestBooks(t *testing.T) {
getBookChapters(t, app, db, baseUrl)
getBook(t, app, db, baseUrl)
createBook(t, app, db, baseUrl)
updateBook(t, app, db, baseUrl)
deleteBook(t, app, db, baseUrl)

// Drop Tables and Close Connectiom
database.DropTables(db)
Expand Down
4 changes: 2 additions & 2 deletions tests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func ParseResponseBody(t *testing.T, b io.ReadCloser) interface{} {
return responseBody
}

func ProcessTestGet(app *fiber.App, url string, access ...string) *http.Response {
req := httptest.NewRequest("GET", url, nil)
func ProcessTestGetOrDelete(app *fiber.App, url string, method string, access ...string) *http.Response {
req := httptest.NewRequest(method, url, nil)
if access != nil {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", access[0]))
}
Expand Down

0 comments on commit e1e34cc

Please sign in to comment.