| 1 | package xoauth2 |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/stretchr/testify/assert" |
| 9 | "github.com/stretchr/testify/require" |
| 10 | "golang.org/x/oauth2" |
| 11 | ) |
| 12 | |
| 13 | func TestGenerator_Generate(t *testing.T) { |
| 14 | t.Parallel() |
| 15 | |
| 16 | tests := []struct { |
| 17 | name string |
| 18 | props map[string]string |
| 19 | args []string |
| 20 | wantErr bool |
| 21 | expectPanic bool |
| 22 | errMsg string |
| 23 | }{ |
| 24 | { |
| 25 | name: "gmail provider with credentials file", |
| 26 | props: map[string]string{"provider": "gmail"}, |
| 27 | args: []string{"/nonexistent/credentials.json"}, |
| 28 | wantErr: true, |
| 29 | errMsg: "failed to read file", |
| 30 | }, |
| 31 | { |
| 32 | name: "gmail provider without args", |
| 33 | props: map[string]string{"provider": "gmail"}, |
| 34 | args: []string{}, |
| 35 | expectPanic: true, |
| 36 | }, |
| 37 | { |
| 38 | name: "non-gmail provider with file arg", |
| 39 | props: map[string]string{"provider": "other"}, |
| 40 | args: []string{"/nonexistent/config.json"}, |
| 41 | wantErr: true, |
| 42 | errMsg: "failed to read file", |
| 43 | }, |
| 44 | { |
| 45 | name: "no provider with file arg", |
| 46 | props: map[string]string{}, |
| 47 | args: []string{"/nonexistent/config.json"}, |
| 48 | wantErr: true, |
| 49 | errMsg: "failed to read file", |
| 50 | }, |
| 51 | { |
| 52 | name: "interactive generator no args fails at scan", |
| 53 | props: map[string]string{}, |
| 54 | args: []string{}, |
| 55 | wantErr: true, |
| 56 | errMsg: "failed to scan input", |
| 57 | }, |
| 58 | } |
| 59 | |
| 60 | for _, tt := range tests { |
| 61 | t.Run(tt.name, func(t *testing.T) { |
| 62 | t.Parallel() |
| 63 | |
| 64 | g := &Generator{} |
| 65 | |
| 66 | if tt.expectPanic { |
| 67 | assert.Panics(t, func() { |
| 68 | _, _ = g.Generate(nil, tt.props, tt.args) |
| 69 | }) |
| 70 | |
| 71 | return |
| 72 | } |
| 73 | |
| 74 | got, err := g.Generate(nil, tt.props, tt.args) |
| 75 | |
| 76 | if tt.wantErr { |
| 77 | require.Error(t, err) |
| 78 | assert.Contains(t, err.Error(), tt.errMsg) |
| 79 | |
| 80 | return |
| 81 | } |
| 82 | |
| 83 | require.NoError(t, err) |
| 84 | assert.NotNil(t, got) |
| 85 | }) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func Test_generateOauth2Config(t *testing.T) { |
| 90 | t.Parallel() |
| 91 | |
| 92 | type args struct { |
| 93 | conf *oauth2.Config |
| 94 | host string |
| 95 | } |
| 96 | |
| 97 | tests := []struct { |
| 98 | name string |
| 99 | args args |
| 100 | wantErr bool |
| 101 | errMsg string |
| 102 | }{} |
| 103 | |
| 104 | for _, tt := range tests { |
| 105 | t.Run(tt.name, func(t *testing.T) { |
| 106 | t.Parallel() |
| 107 | |
| 108 | got, err := generateOauth2Config(tt.args.conf, tt.args.host) |
| 109 | |
| 110 | if tt.wantErr { |
| 111 | require.Error(t, err) |
| 112 | assert.Contains(t, err.Error(), tt.errMsg) |
| 113 | |
| 114 | return |
| 115 | } |
| 116 | |
| 117 | require.NoError(t, err) |
| 118 | assert.NotNil(t, got) |
| 119 | }) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func Test_oauth2GeneratorFile(t *testing.T) { |
| 124 | t.Parallel() |
| 125 | |
| 126 | tests := []struct { |
| 127 | name string |
| 128 | file string |
| 129 | wantErr bool |
| 130 | errType error |
| 131 | errMsg string |
| 132 | }{ |
| 133 | { |
| 134 | name: "nonexistent file", |
| 135 | file: "/nonexistent/path/config.json", |
| 136 | wantErr: true, |
| 137 | errType: ErrReadFileFailed, |
| 138 | errMsg: "failed to read file", |
| 139 | }, |
| 140 | { |
| 141 | name: "invalid JSON file", |
| 142 | file: createTempFile(t, "invalid json content"), |
| 143 | wantErr: true, |
| 144 | errType: ErrUnmarshalFailed, |
| 145 | errMsg: "failed to unmarshal JSON", |
| 146 | }, |
| 147 | } |
| 148 | |
| 149 | for _, tt := range tests { |
| 150 | t.Run(tt.name, func(t *testing.T) { |
| 151 | t.Parallel() |
| 152 | |
| 153 | got, err := oauth2GeneratorFile(tt.file) |
| 154 | |
| 155 | if tt.wantErr { |
| 156 | require.Error(t, err) |
| 157 | assert.Contains(t, err.Error(), tt.errMsg) |
| 158 | |
| 159 | if tt.errType != nil { |
| 160 | require.ErrorIs(t, err, tt.errType) |
| 161 | } |
| 162 | |
| 163 | return |
| 164 | } |
| 165 | |
| 166 | require.NoError(t, err) |
| 167 | assert.NotNil(t, got) |
| 168 | }) |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | func Test_oauth2GeneratorGmail(t *testing.T) { |
| 173 | t.Parallel() |
| 174 | |
| 175 | tests := []struct { |
| 176 | name string |
| 177 | file string |
| 178 | wantErr bool |
| 179 | errType error |
| 180 | errMsg string |
| 181 | }{ |
| 182 | { |
| 183 | name: "nonexistent credentials file", |
| 184 | file: "/nonexistent/path/credentials.json", |
| 185 | wantErr: true, |
| 186 | errType: ErrReadFileFailed, |
| 187 | errMsg: "failed to read file", |
| 188 | }, |
| 189 | { |
| 190 | name: "invalid JSON credentials file", |
| 191 | file: createTempFile(t, "invalid json content"), |
| 192 | wantErr: true, |
| 193 | errMsg: "invalid character", |
| 194 | }, |
| 195 | } |
| 196 | |
| 197 | for _, tt := range tests { |
| 198 | t.Run(tt.name, func(t *testing.T) { |
| 199 | t.Parallel() |
| 200 | |
| 201 | got, err := oauth2GeneratorGmail(tt.file) |
| 202 | |
| 203 | if tt.wantErr { |
| 204 | require.Error(t, err) |
| 205 | assert.Contains(t, err.Error(), tt.errMsg) |
| 206 | |
| 207 | if tt.errType != nil { |
| 208 | require.ErrorIs(t, err, tt.errType) |
| 209 | } |
| 210 | |
| 211 | return |
| 212 | } |
| 213 | |
| 214 | require.NoError(t, err) |
| 215 | assert.NotNil(t, got) |
| 216 | }) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // createTempFile creates a temporary file with the given content and returns its path. |
| 221 | func createTempFile(t *testing.T, content string) string { |
| 222 | t.Helper() |
| 223 | |
| 224 | tmpDir := t.TempDir() |
| 225 | tmpFile := filepath.Join(tmpDir, "test-config.json") |
| 226 | |
| 227 | err := os.WriteFile(tmpFile, []byte(content), 0o600) |
| 228 | require.NoError(t, err, "failed to create temp file") |
| 229 | |
| 230 | return tmpFile |
| 231 | } |