Files

131 lines
4.6 KiB
Go

package fiber
import (
"net/http"
"strings"
"testing"
"github.com/google/uuid"
)
// TestController_UsersCreate checks one registration contract scenario per subtest.
func TestController_UsersCreate(t *testing.T) {
cases := []struct {
name string
body string
status int
}{
{"Positive: valid Unicode display name", `{"password":"fixture-password-123","username":"alice","display_name":"Алиса 👋"}`, http.StatusCreated},
{"Positive: display name at rune limit", `{"password":"fixture-password-123","username":"alice","display_name":"` + strings.Repeat("👋", 100) + `"}`, http.StatusCreated},
{"Negative: display name exceeds rune limit", `{"password":"fixture-password-123","username":"alice","display_name":"` + strings.Repeat("👋", 101) + `"}`, http.StatusBadRequest},
{"Negative: uppercase username", `{"password":"fixture-password-123","username":"Alice","display_name":"Alice"}`, http.StatusBadRequest},
{"Negative: malformed JSON", `{`, http.StatusBadRequest},
{"Negative: null root", `null`, http.StatusBadRequest},
{"Negative: array root", `[]`, http.StatusBadRequest},
{"Negative: missing fields", `{}`, http.StatusBadRequest},
{"Negative: null field", `{"password":"fixture-password-123","username":"alice","display_name":null}`, http.StatusBadRequest},
{"Negative: wrong field type", `{"password":"fixture-password-123","username":5,"display_name":"Alice"}`, http.StatusBadRequest},
{"Negative: unknown field", `{"password":"fixture-password-123","username":"alice","display_name":"Alice","unknown":"secret"}`, http.StatusBadRequest},
{"Negative: field name case", `{"password":"fixture-password-123","Username":"alice","display_name":"Alice"}`, http.StatusBadRequest},
{"Negative: duplicate field", `{"password":"fixture-password-123","username":"alice","username":"bob","display_name":"Alice"}`, http.StatusBadRequest},
{"Negative: trailing object", `{"password":"fixture-password-123","username":"alice","display_name":"Alice"} {}`, http.StatusBadRequest},
{"Negative: invalid UTF-8", "{\"username\":\"alice\",\"display_name\":\"\xff\"}", http.StatusBadRequest},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
// Arrange.
ctrl := testController(t, nil)
// Act.
status, body, err := request(ctrl, http.MethodPost, "/api/v1/users", "", []byte(tc.body))
// Assert.
if err != nil {
t.Fatal(err)
}
assertHTTPStatus(t, status, tc.status, body)
if tc.status == http.StatusBadRequest {
assertErrorCode(t, body, "invalid_request")
return
}
if body["username"] != "alice" || body["display_name"] == "" {
t.Fatalf("registration fields changed: %v", body)
}
if _, err := uuid.Parse(body["id"].(string)); err != nil {
t.Fatalf("invalid assigned ID: %v", err)
}
})
}
}
// TestController_UsersGetMe verifies profile selection and authentication failures.
func TestController_UsersGetMe(t *testing.T) {
t.Run("Positive: own profile", func(t *testing.T) {
// Arrange.
ctrl := testController(t, nil)
actor := user(t, ctrl)
identity := actor
// Act.
status, body, err := request(ctrl, http.MethodGet, "/api/v1/users/me", identity, nil)
// Assert.
if err != nil {
t.Fatal(err)
}
assertHTTPStatus(t, status, http.StatusOK, body)
if body["id"] != actor || body["display_name"] != "Алиса 👋" {
t.Fatalf("wrong profile: %v", body)
}
})
t.Run("Negative: missing identity", func(t *testing.T) {
// Arrange.
ctrl := testController(t, nil)
identity := ""
// Act.
status, body, err := request(ctrl, http.MethodGet, "/api/v1/users/me", identity, nil)
// Assert.
if err != nil {
t.Fatal(err)
}
assertHTTPStatus(t, status, http.StatusUnauthorized, body)
assertErrorCode(t, body, "unauthorized")
})
t.Run("Negative: unknown identity", func(t *testing.T) {
// Arrange.
ctrl := testController(t, nil)
identity := uuid.NewString()
// Act.
status, body, err := request(ctrl, http.MethodGet, "/api/v1/users/me", identity, nil)
// Assert.
if err != nil {
t.Fatal(err)
}
assertHTTPStatus(t, status, http.StatusUnauthorized, body)
assertErrorCode(t, body, "unauthorized")
})
}
// assertHTTPStatus reports the response body alongside a mismatched status.
func assertHTTPStatus(t *testing.T, got, want int, body map[string]any) {
t.Helper()
if got != want {
t.Fatalf("status=%d, want=%d, body=%v", got, want, body)
}
}
// assertErrorCode checks the documented error classification independently of status.
func assertErrorCode(t *testing.T, body map[string]any, want string) {
t.Helper()
envelope, ok := body["error"].(map[string]any)
if !ok || envelope["code"] != want {
t.Fatalf("error=%v, want code %q", body, want)
}
}