Lab3 baseline: HAProxy, replicated stateless API, shared PostgreSQL/Valkey, idempotent messages
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
// Package model holds the public JSON shapes of the lab3 contract. They are
|
||||
// deliberately separate from the database scan helpers so an accidental new
|
||||
// column cannot silently become an API field.
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
// Time marshals an RFC 3339 UTC timestamp with exactly six fractional digits.
|
||||
// Go's default time.Time JSON encoder trims trailing zeros, which produces
|
||||
// 3-5 digit fractions that some Python 3.9 datetime parsers (used by the
|
||||
// public test client) reject. A fixed microsecond precision matches
|
||||
// PostgreSQL timestamptz and stays valid RFC 3339.
|
||||
type Time struct{ time.Time }
|
||||
|
||||
const timestampLayout = "2006-01-02T15:04:05.000000Z"
|
||||
|
||||
// MarshalJSON implements json.Marshaler.
|
||||
func (t Time) MarshalJSON() ([]byte, error) {
|
||||
return []byte(`"` + t.UTC().Format(timestampLayout) + `"`), nil
|
||||
}
|
||||
|
||||
// User is the public user shape. It never contains a password hash or session
|
||||
// data.
|
||||
type User struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
CreatedAt Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// Chat is the public chat shape. MemberIDs preserves the order supplied at
|
||||
// creation; membership is immutable in the mandatory lab3 API.
|
||||
type Chat struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
CreatedAt Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// Message is the public message shape. SenderID always comes from the server
|
||||
// session, never from the request body.
|
||||
type Message struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// ChatPage is one keyset page of accessible chats.
|
||||
type ChatPage struct {
|
||||
Items []Chat `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
// MessagePage is one keyset page of a chat history.
|
||||
type MessagePage struct {
|
||||
Items []Message `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
// Session is the login response. Token is only ever written to the response
|
||||
// body, never to logs or PostgreSQL.
|
||||
type Session struct {
|
||||
Token string `json:"token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresAt Time `json:"expires_at"`
|
||||
User User `json:"user"`
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestTimeMarshalUsesFixedMicroseconds(t *testing.T) {
|
||||
value := Time{Time: time.Date(2026, 11, 30, 12, 0, 0, 120000000, time.UTC)}
|
||||
data, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := string(data)
|
||||
// .120000 would be trimmed to .12 by the default encoder; the public
|
||||
// Python 3.9 client requires 3 or 6 fractional digits.
|
||||
if got != `"2026-11-30T12:00:00.120000Z"` {
|
||||
t.Fatalf("marshal = %s", got)
|
||||
}
|
||||
if !strings.HasSuffix(got, "Z\"") {
|
||||
t.Fatal("timestamp does not end in Z")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserJSONHasOnlyPublicFields(t *testing.T) {
|
||||
user := User{
|
||||
ID: "11111111-2222-4333-8444-555555555555",
|
||||
Username: "alice",
|
||||
DisplayName: "Алиса",
|
||||
CreatedAt: Time{Time: time.Date(2026, 9, 1, 12, 0, 0, 0, time.UTC)},
|
||||
}
|
||||
data, err := json.Marshal(user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var fields map[string]any
|
||||
if err := json.Unmarshal(data, &fields); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(fields) != 4 {
|
||||
t.Fatalf("user JSON has unexpected fields: %v", fields)
|
||||
}
|
||||
for _, forbidden := range []string{"password", "password_hash", "token", "token_hash"} {
|
||||
if _, present := fields[forbidden]; present {
|
||||
t.Fatalf("user JSON leaks %q", forbidden)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user