forked from mtusi2026/mtusi2026lab2
Compare commits
2
Commits
5bd210b59d
...
d7002b4dfe
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d7002b4dfe | ||
|
|
b540577cf9 |
@@ -33,7 +33,7 @@ func (h *AuthHandler) CreateSession(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpx.WriteInternalError(w, requestID)
|
httpx.WriteInternalError(w, requestID, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,7 +65,7 @@ func (h *AuthHandler) RevokeSession(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpx.WriteInternalError(w, requestID)
|
httpx.WriteInternalError(w, requestID, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ func (h *ChatHandler) Create(w http.ResponseWriter, r *http.Request) {
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
case err != nil:
|
case err != nil:
|
||||||
httpx.WriteInternalError(w, requestID)
|
httpx.WriteInternalError(w, requestID, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,7 +106,7 @@ func (h *ChatHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
case err != nil:
|
case err != nil:
|
||||||
httpx.WriteInternalError(w, requestID)
|
httpx.WriteInternalError(w, requestID, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +134,7 @@ func (h *ChatHandler) List(w http.ResponseWriter, r *http.Request) {
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
case err != nil:
|
case err != nil:
|
||||||
httpx.WriteInternalError(w, requestID)
|
httpx.WriteInternalError(w, requestID, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ func (h *MessageHandler) Create(w http.ResponseWriter, r *http.Request) {
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
case err != nil:
|
case err != nil:
|
||||||
httpx.WriteInternalError(w, requestID)
|
httpx.WriteInternalError(w, requestID, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,7 +114,7 @@ func (h *MessageHandler) List(w http.ResponseWriter, r *http.Request) {
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
case err != nil:
|
case err != nil:
|
||||||
httpx.WriteInternalError(w, requestID)
|
httpx.WriteInternalError(w, requestID, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ func (h *UserHandler) Create(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpx.WriteInternalError(w, requestID)
|
httpx.WriteInternalError(w, requestID, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,7 +82,7 @@ func (h *UserHandler) Me(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpx.WriteInternalError(w, requestID)
|
httpx.WriteInternalError(w, requestID, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,14 @@
|
|||||||
package httpx
|
package httpx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5/pgconn"
|
||||||
|
|
||||||
"mtusi2026-messenger/internal/dto"
|
"mtusi2026-messenger/internal/dto"
|
||||||
)
|
)
|
||||||
@@ -23,7 +29,18 @@ func WriteError(w http.ResponseWriter, status int, code dto.ErrorCode, message,
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteInternalError(w http.ResponseWriter, requestID string) {
|
func WriteInternalError(w http.ResponseWriter, requestID string, err error) {
|
||||||
|
if isUnavailable(err) {
|
||||||
|
WriteError(
|
||||||
|
w,
|
||||||
|
http.StatusServiceUnavailable,
|
||||||
|
dto.ErrorCodeUnavailable,
|
||||||
|
"dependency unavailable",
|
||||||
|
requestID,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
WriteError(
|
WriteError(
|
||||||
w,
|
w,
|
||||||
http.StatusInternalServerError,
|
http.StatusInternalServerError,
|
||||||
@@ -32,3 +49,19 @@ func WriteInternalError(w http.ResponseWriter, requestID string) {
|
|||||||
requestID,
|
requestID,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isUnavailable(err error) bool {
|
||||||
|
if errors.Is(err, context.DeadlineExceeded) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := errors.AsType[net.Error](err); ok {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok {
|
||||||
|
return strings.HasPrefix(pgErr.Code, "08") || pgErr.Code == "57P01"
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ func Auth(auth authenticator) func(http.Handler) http.Handler {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpx.WriteInternalError(w, requestID)
|
httpx.WriteInternalError(w, requestID, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
//go:build contract
|
||||||
|
|
||||||
|
package studenttests_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func basicLogin(t *testing.T, username, password string) *http.Response {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
req, err := http.NewRequest(http.MethodPost, *baseURL+"/api/v1/auth/sessions", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("build login request: %v", err)
|
||||||
|
}
|
||||||
|
req.SetBasicAuth(username, password)
|
||||||
|
|
||||||
|
resp, err := httpClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("login request failed: %v", err)
|
||||||
|
}
|
||||||
|
return resp
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoginWrongPassword(t *testing.T) {
|
||||||
|
user := createUser(t)
|
||||||
|
|
||||||
|
resp := basicLogin(t, user.Username, "wrong-"+testPassword)
|
||||||
|
requireStatus(t, resp, http.StatusUnauthorized)
|
||||||
|
resp.Body.Close()
|
||||||
|
|
||||||
|
resp = basicLogin(t, "no_such_user_"+user.Username, testPassword)
|
||||||
|
requireStatus(t, resp, http.StatusUnauthorized)
|
||||||
|
resp.Body.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLogout(t *testing.T) {
|
||||||
|
user := createUser(t)
|
||||||
|
|
||||||
|
resp := doRequest(t, http.MethodGet, "/api/v1/users/me", user.Token, nil)
|
||||||
|
requireStatus(t, resp, http.StatusOK)
|
||||||
|
resp.Body.Close()
|
||||||
|
|
||||||
|
resp = doRequest(t, http.MethodDelete, "/api/v1/auth/sessions/current", user.Token, nil)
|
||||||
|
if resp.StatusCode >= 300 {
|
||||||
|
t.Fatalf("logout: unexpected status %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
|
||||||
|
resp = doRequest(t, http.MethodGet, "/api/v1/users/me", user.Token, nil)
|
||||||
|
requireStatus(t, resp, http.StatusUnauthorized)
|
||||||
|
resp.Body.Close()
|
||||||
|
|
||||||
|
resp = doRequest(t, http.MethodDelete, "/api/v1/auth/sessions/current", user.Token, nil)
|
||||||
|
requireStatus(t, resp, http.StatusUnauthorized)
|
||||||
|
resp.Body.Close()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user