Реализовать lab3 на оценку 4: балансировка и отказ API

This commit is contained in:
Daria0w0
2026-10-23 17:14:32 +03:00
parent 0ab3a341aa
commit a67e7996ec
88 changed files with 5105 additions and 21 deletions
+55
View File
@@ -0,0 +1,55 @@
package service
import (
"context"
"errors"
"testing"
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model"
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/password"
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store"
)
type registrationWriter struct {
calls int
hash string
err error
}
func (w *registrationWriter) CreateUser(_ context.Context, username, displayName, hash string) (model.User, error) {
w.calls++
w.hash = hash
return model.User{Username: username, DisplayName: displayName}, w.err
}
func TestRegistrationHashesBeforeStorage(t *testing.T) {
w := &registrationWriter{}
s := NewRegistration(w)
user, err := s.Register(context.Background(), "alice", "Алиса", "учебный:пароль")
if err != nil || user.Username != "alice" || w.calls != 1 {
t.Fatalf("registration failed: %v", err)
}
ok, err := password.Verify("учебный:пароль", w.hash)
if err != nil || !ok {
t.Fatalf("stored value is not a valid password hash: %v", err)
}
w.err = store.ErrUsernameTaken
_, err = s.Register(context.Background(), "alice", "Алиса", "учебный:пароль")
if !errors.Is(err, store.ErrUsernameTaken) {
t.Fatalf("conflict lost: %v", err)
}
}
func TestRegistrationRejectsBeforeStorage(t *testing.T) {
w := &registrationWriter{}
s := NewRegistration(w)
for _, fields := range [][3]string{{"Alice", "Алиса", "учебный:пароль"}, {"alice", " ", "учебный:пароль"}, {"alice", "Алиса", "short"}} {
_, err := s.Register(context.Background(), fields[0], fields[1], fields[2])
if !errors.Is(err, ErrInvalidRequest) {
t.Fatalf("invalid input accepted: %v", err)
}
}
if w.calls != 0 {
t.Fatal("invalid data reached storage")
}
}