|
5 | 5 | "github.com/gemfury/cli/internal/ctx" |
6 | 6 | "github.com/gemfury/cli/internal/testutil" |
7 | 7 | "github.com/gemfury/cli/pkg/terminal" |
| 8 | + "net/http" |
8 | 9 | "strings" |
9 | 10 | "testing" |
10 | 11 | ) |
@@ -85,6 +86,41 @@ func TestSharingAddCommandSuccess(t *testing.T) { |
85 | 86 | } |
86 | 87 | } |
87 | 88 |
|
| 89 | +func TestSharingAddWithRoleCommandSuccess(t *testing.T) { |
| 90 | + auth := terminal.TestAuther("user", "abc123", nil) |
| 91 | + term := terminal.NewForTest() |
| 92 | + |
| 93 | + // Fire up test server |
| 94 | + roleQuery := "unchanged-by-server" |
| 95 | + path := "/collaborators/owner@example.com" |
| 96 | + server := testutil.APIServerCustom(t, func(mux *http.ServeMux) { |
| 97 | + mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { |
| 98 | + if m := r.Method; m != "PUT" { |
| 99 | + t.Errorf("Incorrect method: %q", m) |
| 100 | + } |
| 101 | + roleQuery = r.URL.Query().Get("role") |
| 102 | + w.Write([]byte("{}")) |
| 103 | + }) |
| 104 | + }) |
| 105 | + defer server.Close() |
| 106 | + |
| 107 | + cc := cli.TestContext(term, auth) |
| 108 | + flags := ctx.GlobalFlags(cc) |
| 109 | + flags.Endpoint = server.URL |
| 110 | + |
| 111 | + err := runCommandNoErr(cc, []string{"sharing", "add", "owner@example.com", "--role", "owner"}) |
| 112 | + if err != nil { |
| 113 | + t.Fatal(err) |
| 114 | + } |
| 115 | + |
| 116 | + exp := "Invited \"owner@example.com\" as a collaborator\n" |
| 117 | + if outStr := string(term.OutBytes()); !strings.HasSuffix(outStr, exp) { |
| 118 | + t.Errorf("Expected output to include %q, got %q", exp, outStr) |
| 119 | + } else if r := roleQuery; r != "owner" { |
| 120 | + t.Errorf(`Expected role to be "owner", got %q`, r) |
| 121 | + } |
| 122 | +} |
| 123 | + |
88 | 124 | func TestSharingAddCommandUnauthorized(t *testing.T) { |
89 | 125 | path := "/collaborators/added@example.com" |
90 | 126 | server := testutil.APIServer(t, "PUT", path, "{}", 200) |
|
0 commit comments