forked from mtusi2026/mtusi2026lab2
refactor: update project structure
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"messenger/src/backend/handlers"
|
||||
"messenger/src/backend/services"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPasswordKDFUsesSaltAndFullUTF8Password(t *testing.T) {
|
||||
password := "длинный-пароль-с-эмодзи-🔒-0123456789"
|
||||
first, err := services.HashPassword(password)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := services.HashPassword(password)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.HasPrefix(first, []byte("$argon2id$v=19$m=65536,t=3,p=2$")) || bytes.Equal(first, second) {
|
||||
t.Fatal("password hashes must use Argon2id with independent salts")
|
||||
}
|
||||
valid, err := services.VerifyPassword(password, first)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
invalid, err := services.VerifyPassword(password+"другое", first)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !valid || invalid {
|
||||
t.Fatal("Argon2id verification lost the full UTF-8 password")
|
||||
}
|
||||
if _, err := services.VerifyPassword(password, bytes.Repeat([]byte{1}, 64)); err == nil {
|
||||
t.Fatal("legacy scrypt hash must not be accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoredPasswordUsesArgon2id(t *testing.T) {
|
||||
fixture := newTestFixture(t)
|
||||
user, _ := createTestUser(t, fixture.users, "alice", "Алиса")
|
||||
var stored []byte
|
||||
if err := fixture.db.QueryRow(context.Background(), "SELECT password_hash FROM users WHERE id=$1", user.ID).Scan(&stored); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.HasPrefix(stored, []byte("$argon2id$")) {
|
||||
t.Fatalf("unexpected stored password hash format: %q", stored)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBearerRequiredIgnoresXUserID(t *testing.T) {
|
||||
handler := handlers.NewRouter(nil, nil, nil, nil, nil)
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/users/me", nil)
|
||||
request.Header.Set("X-User-Id", "11111111-1111-4111-8111-111111111111")
|
||||
response := httptest.NewRecorder()
|
||||
handler.ServeHTTP(response, request)
|
||||
if response.Code != 401 || response.Header().Get("WWW-Authenticate") != `Bearer realm="messenger"` {
|
||||
t.Fatalf("unexpected response: %d %v", response.Code, response.Header())
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageCursorPreservesPosition(t *testing.T) {
|
||||
original := messageCursor{ChatID: "11111111-1111-4111-8111-111111111111", CreatedAt: time.Date(2026, 9, 23, 10, 0, 0, 123000, time.UTC), MessageID: "22222222-2222-4222-8222-222222222222"}
|
||||
token, err := services.EncodeMessageCursor(original)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
decoded, err := services.DecodeMessageCursor(token)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if decoded != original {
|
||||
t.Fatalf("cursor changed: %+v", decoded)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user