-
-
Notifications
You must be signed in to change notification settings - Fork 290
Expand file tree
/
Copy pathsshurl_test.go
More file actions
215 lines (200 loc) · 5.94 KB
/
sshurl_test.go
File metadata and controls
215 lines (200 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package credentials
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNormalizeSSHRemoteURL(t *testing.T) {
tests := []struct {
name string
remoteURL string
sshUser string
want string
wantErr string
}{
// Basic HTTPS to SSH conversion
{
name: "https URL with git user",
remoteURL: "https://github.com/org-name/some-test-repo.git",
sshUser: "git",
want: "git@github.com:org-name/some-test-repo.git",
},
{
name: "https URL without .git suffix",
remoteURL: "https://github.com/org-name/some-test-repo",
sshUser: "git",
want: "git@github.com:org-name/some-test-repo",
},
{
name: "https URL with custom user",
remoteURL: "https://github.com/org-name/some-test-repo.git",
sshUser: "myuser",
want: "myuser@github.com:org-name/some-test-repo.git",
},
// Plain host/path format
{
name: "host/path format with git user",
remoteURL: "github.com/org-name/some-test-repo.git",
sshUser: "git",
want: "git@github.com:org-name/some-test-repo.git",
},
{
name: "host/path format with empty user defaults to git",
remoteURL: "github.com/org-name/some-test-repo.git",
sshUser: "",
want: "git@github.com:org-name/some-test-repo.git",
},
// Already valid SSH URLs
{
name: "already valid SSH URL with matching user",
remoteURL: "git@github.com:org-name/some-test-repo.git",
sshUser: "git",
want: "git@github.com:org-name/some-test-repo.git",
},
{
name: "already valid SSH URL with custom matching user",
remoteURL: "deploy@gitlab.com:org/repo.git",
sshUser: "deploy",
want: "deploy@gitlab.com:org/repo.git",
},
// SSH protocol URLs
{
name: "ssh:// protocol URL",
remoteURL: "ssh://git@github.com/org-name/some-test-repo.git",
sshUser: "git",
want: "git@github.com:org-name/some-test-repo.git",
},
{
name: "ssh:// protocol URL without user",
remoteURL: "ssh://github.com/org-name/some-test-repo.git",
sshUser: "git",
want: "git@github.com:org-name/some-test-repo.git",
},
// HTTP (non-HTTPS) URL
{
name: "http URL converts to SSH",
remoteURL: "http://github.com/org-name/some-test-repo.git",
sshUser: "git",
want: "git@github.com:org-name/some-test-repo.git",
},
// Whitespace handling
{
name: "URL with leading/trailing whitespace",
remoteURL: " https://github.com/org/repo.git ",
sshUser: "git",
want: "git@github.com:org/repo.git",
},
// Different Git providers
{
name: "GitLab URL",
remoteURL: "https://gitlab.com/group/project.git",
sshUser: "git",
want: "git@gitlab.com:group/project.git",
},
{
name: "Bitbucket URL",
remoteURL: "https://bitbucket.org/team/repo.git",
sshUser: "git",
want: "git@bitbucket.org:team/repo.git",
},
{
name: "Self-hosted GitLab",
remoteURL: "https://git.example.com/team/project.git",
sshUser: "git",
want: "git@git.example.com:team/project.git",
},
// URLs with explicit ports
{
name: "https URL with port 443",
remoteURL: "https://github.com:443/org/repo.git",
sshUser: "git",
want: "git@github.com:org/repo.git",
},
{
name: "ssh:// URL with port 22",
remoteURL: "ssh://git@github.com:22/org/repo.git",
sshUser: "git",
want: "git@github.com:org/repo.git",
},
{
name: "ssh:// URL without user but with port",
remoteURL: "ssh://github.com:22/org/repo.git",
sshUser: "git",
want: "git@github.com:org/repo.git",
},
{
name: "host:port/path format",
remoteURL: "github.com:443/org/repo.git",
sshUser: "git",
want: "git@github.com:org/repo.git",
},
// Nested paths (GitLab subgroups, etc.)
{
name: "GitLab nested subgroups",
remoteURL: "https://gitlab.com/group/subgroup/project.git",
sshUser: "git",
want: "git@gitlab.com:group/subgroup/project.git",
},
{
name: "deeply nested path",
remoteURL: "https://gitlab.com/org/team/product/service.git",
sshUser: "git",
want: "git@gitlab.com:org/team/product/service.git",
},
{
name: "ssh:// with nested path",
remoteURL: "ssh://git@gitlab.com/group/subgroup/project.git",
sshUser: "git",
want: "git@gitlab.com:group/subgroup/project.git",
},
{
name: "ssh:// with non-default port preserves protocol URL",
remoteURL: "ssh://git@gitlab.localhost:6022/root/demo.git",
sshUser: "git",
want: "ssh://git@gitlab.localhost:6022/root/demo.git",
},
{
name: "ssh:// without user and non-default port preserves protocol URL",
remoteURL: "ssh://gitlab.localhost:6022/root/demo.git",
sshUser: "git",
want: "ssh://git@gitlab.localhost:6022/root/demo.git",
},
// Error cases
{
name: "conflicting users in SSH URL",
remoteURL: "git@github.com:org-name/some-test-repo.git",
sshUser: "other",
wantErr: `conflicting SSH users: URL contains "git" but credentials specify "other"`,
},
{
name: "conflicting users in ssh:// URL",
remoteURL: "ssh://git@github.com/org/repo.git",
sshUser: "deploy",
wantErr: `conflicting SSH users: URL contains "git" but credentials specify "deploy"`,
},
{
name: "invalid URL without path",
remoteURL: "github.com",
sshUser: "git",
wantErr: `invalid remote URL format: "github.com" (expected host/path)`,
},
{
name: "invalid ssh:// URL without path",
remoteURL: "ssh://github.com",
sshUser: "git",
wantErr: `invalid ssh:// URL format`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NormalizeSSHRemoteURL(tt.remoteURL, tt.sshUser)
if tt.wantErr != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), tt.wantErr)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}