108 lines
3.3 KiB
Go
108 lines
3.3 KiB
Go
package account
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestRegistrationAuthenticationPersistenceAndDeletion(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "private", "accounts.json")
|
|
store, err := Open(path, 10)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
registered, err := store.Register(RegisterInput{
|
|
Email: " Explorer@Example.Test ",
|
|
DisplayName: "Explorer",
|
|
AgeBand: "18-24",
|
|
ZIPCode: "85001",
|
|
Password: "a sufficiently long password",
|
|
Consent: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if registered.Email != "explorer@example.test" {
|
|
t.Fatalf("normalized email = %q", registered.Email)
|
|
}
|
|
if _, ok := store.Authenticate(registered.Email, "wrong password"); ok {
|
|
t.Fatal("wrong password authenticated")
|
|
}
|
|
if _, ok := store.Authenticate(registered.Email, "a sufficiently long password"); !ok {
|
|
t.Fatal("valid password did not authenticate")
|
|
}
|
|
|
|
updated, err := store.SaveAnswers(registered.ID, "bank-v1", map[string]int{"r01": 5})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
updated.Answers["r01"] = 1
|
|
stored, err := store.Get(registered.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if stored.Answers["r01"] != 5 {
|
|
t.Fatal("caller mutated persisted answers")
|
|
}
|
|
|
|
reopened, err := Open(path, 10)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, ok := reopened.Authenticate(registered.Email, "a sufficiently long password"); !ok {
|
|
t.Fatal("persisted account did not authenticate")
|
|
}
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(data), "a sufficiently long password") {
|
|
t.Fatal("account store contains plaintext password")
|
|
}
|
|
var decoded map[string]any
|
|
if err := json.Unmarshal(data, &decoded); err != nil {
|
|
t.Fatalf("account store is not valid JSON: %v", err)
|
|
}
|
|
|
|
if err := reopened.Delete(registered.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := reopened.Get(registered.ID); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("Get after delete error = %v, want ErrNotFound", err)
|
|
}
|
|
}
|
|
|
|
func TestRegistrationValidationAndDuplicateEmail(t *testing.T) {
|
|
store, err := Open(filepath.Join(t.TempDir(), "accounts.json"), 2)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
valid := RegisterInput{
|
|
Email: "person@example.test", DisplayName: "Person", AgeBand: "25-39",
|
|
ZIPCode: "85701", Password: "long enough password", Consent: true,
|
|
}
|
|
if _, err := store.Register(valid); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := store.Register(valid); !errors.Is(err, ErrDuplicateEmail) {
|
|
t.Fatalf("duplicate error = %v, want ErrDuplicateEmail", err)
|
|
}
|
|
|
|
cases := []RegisterInput{
|
|
{Email: "bad", DisplayName: "Person", AgeBand: "25-39", ZIPCode: "85701", Password: "long enough password", Consent: true},
|
|
{Email: "a@example.test", DisplayName: "Person", AgeBand: "unknown", ZIPCode: "85701", Password: "long enough password", Consent: true},
|
|
{Email: "a@example.test", DisplayName: "Person", AgeBand: "25-39", ZIPCode: "10001", Password: "long enough password", Consent: true},
|
|
{Email: "a@example.test", DisplayName: "Person", AgeBand: "25-39", ZIPCode: "85701", Password: "short", Consent: true},
|
|
{Email: "a@example.test", DisplayName: "Person", AgeBand: "25-39", ZIPCode: "85701", Password: "long enough password", Consent: false},
|
|
}
|
|
for index, input := range cases {
|
|
if _, err := store.Register(input); err == nil {
|
|
t.Fatalf("invalid case %d was accepted", index)
|
|
}
|
|
}
|
|
}
|