package cli import ( "strings" "testing" ) // isPublisher asks the store whether addr is a publisher of dev@example.org. func (e *env) isPublisher(t *testing.T, addr string) bool { t.Helper() l, err := e.s.FindList("dev@example.org") if err != nil { t.Fatal(err) } ok, err := e.s.IsPublisher(l.ID, addr) if err != nil { t.Fatal(err) } return ok } func TestMemberAddWithThePublisherFlag(t *testing.T) { e := newEnv(t) e.ok(t, "list", "add", "dev@example.org") out := e.ok(t, "member", "add", "dev@example.org", "alice@example.com", "--publisher") if !strings.Contains(out, "Added alice@example.com") || !strings.Contains(out, "publisher") { t.Fatalf("add: %q", out) } if !e.isPublisher(t, "alice@example.com") { t.Fatal("--publisher makes a publisher") } // Without the flag an existing member keeps its role. if out := e.ok(t, "member", "add", "dev@example.org", "alice@example.com", "--name", "Alice"); !strings.Contains(out, "publisher") { t.Fatalf("re-add: %q", out) } if !e.isPublisher(t, "alice@example.com") { t.Fatal("re-adding without the flag keeps it") } if out := e.ok(t, "member", "add", "dev@example.org", "alice@example.com", "--publisher=false"); strings.Contains(out, "publisher") { t.Fatalf("demote: %q", out) } if e.isPublisher(t, "alice@example.com") { t.Fatal("--publisher=false demotes") } e.ok(t, "member", "add", "dev@example.org", "bob@example.com") if e.isPublisher(t, "bob@example.com") { t.Fatal("a plain add is a subscriber") } } func TestMemberLsShowsTheRole(t *testing.T) { e := newEnv(t) e.ok(t, "list", "add", "dev@example.org") e.ok(t, "member", "add", "dev@example.org", "alice@example.com", "--publisher") e.ok(t, "member", "add", "dev@example.org", "bob@example.com") out := e.ok(t, "member", "ls", "dev@example.org") lines := strings.Split(strings.TrimSpace(out), "\n") if len(lines) != 3 || !strings.Contains(lines[0], "ROLE") { t.Fatalf("ls:\n%s", out) } if !strings.Contains(lines[1], "alice@example.com") || !strings.Contains(lines[1], "publisher") { t.Fatalf("alice:\n%s", out) } if !strings.Contains(lines[2], "bob@example.com") || !strings.Contains(lines[2], "subscriber") { t.Fatalf("bob:\n%s", out) } if out := e.ok(t, "--json", "member", "ls", "dev@example.org"); !strings.Contains(out, `"publisher": true`) { t.Fatalf("json: %s", out) } } func TestMemberSetChangesTheRole(t *testing.T) { e := newEnv(t) e.ok(t, "list", "add", "dev@example.org") e.ok(t, "member", "add", "dev@example.org", "alice@example.com") out := e.ok(t, "member", "set", "dev@example.org", "Alice@Example.com", "--publisher=true") if !strings.Contains(out, "alice@example.com") || !strings.Contains(out, "publisher") { t.Fatalf("set: %q", out) } if !e.isPublisher(t, "alice@example.com") { t.Fatal("promoted") } out = e.ok(t, "member", "set", "dev@example.org", "alice@example.com", "--publisher=false") if !strings.Contains(out, "subscriber") { t.Fatalf("set false: %q", out) } if e.isPublisher(t, "alice@example.com") { t.Fatal("demoted") } e.ok(t, "member", "set", "dev@example.org", "alice@example.com", "--publisher") if !e.isPublisher(t, "alice@example.com") { t.Fatal("a bare --publisher means true") } e.fails(t, 2, "--publisher", "member", "set", "dev@example.org", "alice@example.com") e.fails(t, 2, "usage: parsimony member set", "member", "set", "dev@example.org", "--publisher") e.fails(t, 2, "invalid boolean", "member", "set", "dev@example.org", "alice@example.com", "--publisher=maybe") e.fails(t, 1, "not found", "member", "set", "dev@example.org", "bob@example.com", "--publisher") e.fails(t, 1, "no such list", "member", "set", "ghost@example.org", "alice@example.com", "--publisher") } func TestMemberImportReadsThePublisherColumn(t *testing.T) { e := newEnv(t) e.ok(t, "list", "add", "dev@example.org") e.ok(t, "member", "add", "dev@example.org", "erin@example.com", "--publisher") input := "alice@example.com,Alice,publisher\nbob@example.com,, PUBLISHER \ncarol@example.com,Carol,admin\ndave@example.com,Dave,\nerin@example.com,Erin\nfrank@example.com,Frank,publisher,x\n" code, out, errs := e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": e.srv.URL}, input, "member", "import", "dev@example.org", "-") if code != 1 || !strings.Contains(out, "Imported 4") || !strings.Contains(out, "2 failed") { t.Fatalf("%d %q %q", code, out, errs) } if !strings.Contains(errs, "line 3") || !strings.Contains(errs, "admin") || !strings.Contains(errs, "line 6") { t.Fatalf("the bad third columns are reported: %q", errs) } for addr, want := range map[string]bool{"alice@example.com": true, "bob@example.com": true, "dave@example.com": false, "erin@example.com": true} { if got := e.isPublisher(t, addr); got != want { t.Errorf("%s publisher = %v, want %v", addr, got, want) } } l, _ := e.s.FindList("dev@example.org") for _, addr := range []string{"carol@example.com", "frank@example.com"} { if ok, _ := e.s.IsMember(l.ID, addr); ok { t.Errorf("%s: a line with a bad third column imports nothing", addr) } } } func TestListSetPostPublishers(t *testing.T) { e := newEnv(t) e.ok(t, "list", "add", "dev@example.org") e.ok(t, "list", "set", "dev@example.org", "--post", "publishers") if out := e.ok(t, "list", "ls"); !strings.Contains(out, "publishers") { t.Fatalf("ls: %s", out) } e.fails(t, 1, "members, moderated or publishers", "list", "set", "dev@example.org", "--post", "bogus") } func TestHelpMentionsPublishers(t *testing.T) { e := newEnv(t) out := e.ok(t, "help") for _, want := range []string{"--post members|moderated|publishers", "member add [--name N] [--publisher]", "member set --publisher=true|false", "publisher"} { if !strings.Contains(out, want) { t.Errorf("help lacks %q", want) } } }