package main import ( "os" "strings" "testing" ) func TestParseAccounts(t *testing.T) { f, err := os.Open("testdata/accounts.cf") if err != nil { t.Fatal(err) } defer f.Close() accounts, err := parseAccounts(f) if err != nil { t.Fatal(err) } if len(accounts) != 5 { t.Fatalf("got %d accounts, want 5", len(accounts)) } want := []string{ "info@produktor.io", "andriy.oblivantsev@produktor.io", "postmaster@produktor.io", "postman@produktor.io", "ano@produktor.io", } for i, w := range want { if accounts[i].Email != w { t.Errorf("account %d = %q, want %q", i, accounts[i].Email, w) } } if accounts[0].Hash == "" { t.Error("expected a hash to be parsed") } } func TestParseAccountsSkipsMalformed(t *testing.T) { in := "# comment\n\nalice@example.org|hash1\nbroken-line-no-pipe\n|hash-no-email\n" accounts, err := parseAccounts(strings.NewReader(in)) if err != nil { t.Fatal(err) } if len(accounts) != 1 || accounts[0].Email != "alice@example.org" { t.Fatalf("got %+v, want only alice@example.org", accounts) } }