| 93 | } |
| 94 | |
| 95 | func TestTokenRun(t *testing.T) { |
| 96 | tests := []struct { |
| 97 | name string |
| 98 | opts TokenOptions |
| 99 | env map[string]string |
| 100 | cfgStubs func(*testing.T, gh.Config) |
| 101 | wantStdout string |
| 102 | wantErr bool |
| 103 | wantErrMsg string |
| 104 | }{ |
| 105 | { |
| 106 | name: "token", |
| 107 | opts: TokenOptions{}, |
| 108 | cfgStubs: func(t *testing.T, cfg gh.Config) { |
| 109 | login(t, cfg, "github.com", "test-user", "gho_ABCDEFG", "https", false) |
| 110 | }, |
| 111 | wantStdout: "gho_ABCDEFG\n", |
| 112 | }, |
| 113 | { |
| 114 | name: "token by hostname", |
| 115 | opts: TokenOptions{ |
| 116 | Hostname: "github.mycompany.com", |
| 117 | }, |
| 118 | cfgStubs: func(t *testing.T, cfg gh.Config) { |
| 119 | login(t, cfg, "github.com", "test-user", "gho_ABCDEFG", "https", false) |
| 120 | login(t, cfg, "github.mycompany.com", "test-user", "gho_1234567", "https", false) |
| 121 | }, |
| 122 | wantStdout: "gho_1234567\n", |
| 123 | }, |
| 124 | { |
| 125 | name: "no token", |
| 126 | opts: TokenOptions{}, |
| 127 | wantErr: true, |
| 128 | wantErrMsg: "no oauth token found for github.com", |
| 129 | }, |
| 130 | { |
| 131 | name: "no token for hostname user", |
| 132 | opts: TokenOptions{ |
| 133 | Hostname: "ghe.io", |
| 134 | Username: "test-user", |
| 135 | }, |
| 136 | wantErr: true, |
| 137 | wantErrMsg: "no oauth token found for ghe.io account test-user", |
| 138 | }, |
| 139 | { |
| 140 | name: "uses default host when one is not provided", |
| 141 | opts: TokenOptions{}, |
| 142 | cfgStubs: func(t *testing.T, cfg gh.Config) { |
| 143 | login(t, cfg, "github.com", "test-user", "gho_ABCDEFG", "https", false) |
| 144 | login(t, cfg, "github.mycompany.com", "test-user", "gho_1234567", "https", false) |
| 145 | }, |
| 146 | env: map[string]string{"GH_HOST": "github.mycompany.com"}, |
| 147 | wantStdout: "gho_1234567\n", |
| 148 | }, |
| 149 | { |
| 150 | name: "token for user", |
| 151 | opts: TokenOptions{ |
| 152 | Hostname: "github.com", |