mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2025-12-17 13:42:29 +00:00
tests: fix api tests
This commit is contained in:
@@ -2,6 +2,7 @@ package api_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -122,9 +123,9 @@ func TestLogin(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test status
|
// Test user context
|
||||||
func TestStatus(t *testing.T) {
|
func TestUserContext(t *testing.T) {
|
||||||
t.Log("Testing status")
|
t.Log("Testing user context")
|
||||||
|
|
||||||
// Get API
|
// Get API
|
||||||
api := getAPI(t)
|
api := getAPI(t)
|
||||||
@@ -133,7 +134,7 @@ func TestStatus(t *testing.T) {
|
|||||||
recorder := httptest.NewRecorder()
|
recorder := httptest.NewRecorder()
|
||||||
|
|
||||||
// Create request
|
// Create request
|
||||||
req, err := http.NewRequest("GET", "/api/status", nil)
|
req, err := http.NewRequest("GET", "/api/user", nil)
|
||||||
|
|
||||||
// Check if there was an error
|
// Check if there was an error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -152,11 +153,31 @@ func TestStatus(t *testing.T) {
|
|||||||
// Assert
|
// Assert
|
||||||
assert.Equal(t, recorder.Code, http.StatusOK)
|
assert.Equal(t, recorder.Code, http.StatusOK)
|
||||||
|
|
||||||
// Parse the body
|
// Read the body of the response
|
||||||
body := recorder.Body.String()
|
body, bodyErr := io.ReadAll(recorder.Body)
|
||||||
|
|
||||||
if !strings.Contains(body, "user") {
|
// Check if there was an error
|
||||||
t.Fatalf("Expected user in body")
|
if bodyErr != nil {
|
||||||
|
t.Fatalf("Error getting body: %v", bodyErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmarshal the body into the user struct
|
||||||
|
type User struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var user User
|
||||||
|
|
||||||
|
jsonErr := json.Unmarshal(body, &user)
|
||||||
|
|
||||||
|
// Check if there was an error
|
||||||
|
if jsonErr != nil {
|
||||||
|
t.Fatalf("Error unmarshalling body: %v", jsonErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// We should get the username back
|
||||||
|
if user.Username != "user" {
|
||||||
|
t.Fatalf("Expected user, got %s", user.Username)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user