Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ Stores refresh tokens used to obtain new access tokens.
| `expires_at` | TIMESTAMP | NOT NULL, DEFAULT (creation + 60 days) | Timestamp when the token expires |
| `revoked_at` | TIMESTAMP | NULL | Timestamp if the token has been revoked |

## Running Tests

To run all the unit tests in the project, navigate to the root directory of the project in your terminal and execute the following command:

```bash
go test ./...
```

This command will discover and run all test files (files ending with `_test.go`) in the current directory and all its subdirectories.

---

Powered by Go!
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ require (
github.com/lib/pq v1.10.9
golang.org/x/crypto v0.38.0
)

require github.com/go-chi/chi/v5 v5.2.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/go-chi/chi/v5 v5.2.1 h1:KOIHODQj58PmL80G2Eak4WdvUzjSJSm0vG72crDCqb8=
github.com/go-chi/chi/v5 v5.2.1/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
github.com/golang-jwt/jwt/v5 v5.2.2 h1:Rl4B7itRWVtYIHFrSNd7vhTiz9UpLdi6gZhZ3wEeDy8=
github.com/golang-jwt/jwt/v5 v5.2.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
Expand Down
19 changes: 11 additions & 8 deletions handler_chirps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/acramatte/Chirpy/internal/auth"
// "github.com/acramatte/Chirpy/internal/auth" // Removed unused import
"github.com/acramatte/Chirpy/internal/database"
"github.com/go-chi/chi/v5" // Added for chi.URLParam
"github.com/google/uuid"
"net/http"
"strings"
Expand All @@ -21,13 +22,14 @@ type Chirp struct {
}

func (cfg *apiConfig) handlerChirpGet(w http.ResponseWriter, r *http.Request) {
userID, err := uuid.Parse(r.PathValue("chirpID"))
chirpIDStr := chi.URLParam(r, "chirpID") // Changed from r.PathValue
chirpID, err := uuid.Parse(chirpIDStr)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid chirp ID", err)
return
}

dbChirp, err := cfg.db.GetChirp(r.Context(), userID)
dbChirp, err := cfg.db.GetChirp(r.Context(), chirpID)
if err != nil {
respondWithError(w, http.StatusNotFound, "Chirp not found", err)
return
Expand All @@ -42,19 +44,20 @@ func (cfg *apiConfig) handlerChirpGet(w http.ResponseWriter, r *http.Request) {
}

func (cfg *apiConfig) handlerChirpDelete(w http.ResponseWriter, r *http.Request) {
token, err := auth.GetBearerToken(r.Header)
token, err := cfg.getBearerToken(r.Header) // Replaced auth.GetBearerToken
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't find JWT", err)
return
}

userID, err := auth.ValidateJWT(token, cfg.jwtSecret)
userID, err := cfg.validateJWT(token, cfg.jwtSecret) // Replaced auth.ValidateJWT
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't validate JWT", err)
return
}

chirpID, err := uuid.Parse(r.PathValue("chirpID"))
chirpIDStr := chi.URLParam(r, "chirpID") // Changed from r.PathValue
chirpID, err := uuid.Parse(chirpIDStr)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid chirp ID", err)
return
Expand Down Expand Up @@ -123,12 +126,12 @@ func (cfg *apiConfig) getChirps(c context.Context, authorId, sortOrder string) (
}

func (cfg *apiConfig) handlerChirpsCreate(w http.ResponseWriter, r *http.Request) {
token, err := auth.GetBearerToken(r.Header)
token, err := cfg.getBearerToken(r.Header) // Replaced auth.GetBearerToken
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Unauthorized", err)
return
}
userID, err := auth.ValidateJWT(token, cfg.jwtSecret)
userID, err := cfg.validateJWT(token, cfg.jwtSecret) // Replaced auth.ValidateJWT
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Unauthorized", err)
return
Expand Down
536 changes: 536 additions & 0 deletions handler_chirps_test.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions handler_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (cfg *apiConfig) handlerLogin(w http.ResponseWriter, r *http.Request) {
}

func (cfg *apiConfig) handlerRefresh(w http.ResponseWriter, r *http.Request) {
refreshToken, err := auth.GetBearerToken(r.Header)
refreshToken, err := cfg.getBearerToken(r.Header) // Replaced auth.GetBearerToken
if err != nil {
respondWithError(w, http.StatusBadRequest, "No token found", err)
return
Expand All @@ -79,7 +79,7 @@ func (cfg *apiConfig) handlerRefresh(w http.ResponseWriter, r *http.Request) {
}

func (cfg *apiConfig) handlerRevoke(w http.ResponseWriter, r *http.Request) {
refreshToken, err := auth.GetBearerToken(r.Header)
refreshToken, err := cfg.getBearerToken(r.Header) // Replaced auth.GetBearerToken
if err != nil {
respondWithError(w, http.StatusBadRequest, "No token found", err)
return
Expand Down
Loading