| 
 | 1 | +package controllers  | 
 | 2 | + | 
 | 3 | +import (  | 
 | 4 | +	"testing"  | 
 | 5 | +)  | 
 | 6 | + | 
 | 7 | +func TestReplaceForkUsername_SSH_OK(t *testing.T) {  | 
 | 8 | +	cases := []struct {  | 
 | 9 | +		name     string  | 
 | 10 | +		in       string  | 
 | 11 | +		forkUser string  | 
 | 12 | +		expected string  | 
 | 13 | +	}{  | 
 | 14 | +		{  | 
 | 15 | +			name:     "github ssh basic",  | 
 | 16 | +			in:        "[email protected]:old/repo.git",   | 
 | 17 | +			forkUser: "new",  | 
 | 18 | +			expected:  "[email protected]:new/repo.git",   | 
 | 19 | +		},  | 
 | 20 | +		{  | 
 | 21 | +			name:     "ssh no .git",  | 
 | 22 | + | 
 | 23 | +			forkUser: "new",  | 
 | 24 | +			expected:  "[email protected]:new/repo",   | 
 | 25 | +		},  | 
 | 26 | +		{  | 
 | 27 | +			name:     "gitlab subgroup ssh",  | 
 | 28 | +			in:        "[email protected]:group/sub/repo.git",   | 
 | 29 | +			forkUser: "alice",  | 
 | 30 | +			expected:  "[email protected]:alice/repo.git",   | 
 | 31 | +		},  | 
 | 32 | +	}  | 
 | 33 | + | 
 | 34 | +	for _, c := range cases {  | 
 | 35 | +		t.Run(c.name, func(t *testing.T) {  | 
 | 36 | +			got, err := replaceForkUsername(c.in, c.forkUser)  | 
 | 37 | +			if err != nil {  | 
 | 38 | +				t.Fatalf("unexpected error: %v", err)  | 
 | 39 | +			}  | 
 | 40 | +			if got != c.expected {  | 
 | 41 | +				t.Fatalf("expected %q, got %q", c.expected, got)  | 
 | 42 | +			}  | 
 | 43 | +		})  | 
 | 44 | +	}  | 
 | 45 | +}  | 
 | 46 | + | 
 | 47 | +func TestReplaceForkUsername_HTTPS_OK(t *testing.T) {  | 
 | 48 | +	cases := []struct {  | 
 | 49 | +		name     string  | 
 | 50 | +		in       string  | 
 | 51 | +		forkUser string  | 
 | 52 | +		expected string  | 
 | 53 | +	}{  | 
 | 54 | +		{  | 
 | 55 | +			name:     "github https basic",  | 
 | 56 | +			in:       "https://github.com/old/repo.git",  | 
 | 57 | +			forkUser: "new",  | 
 | 58 | +			expected: "https://github.com/new/repo.git",  | 
 | 59 | +		},  | 
 | 60 | +		{  | 
 | 61 | +			name:     "https no .git",  | 
 | 62 | +			in:       "https://github.com/old/repo",  | 
 | 63 | +			forkUser: "new",  | 
 | 64 | +			expected: "https://github.com/new/repo",  | 
 | 65 | +		},  | 
 | 66 | +		{  | 
 | 67 | +			name:     "https with port",  | 
 | 68 | +			in:       "https://git.example.com:8443/group/repo",  | 
 | 69 | +			forkUser: "me",  | 
 | 70 | +			expected: "https://git.example.com:8443/me/repo",  | 
 | 71 | +		},  | 
 | 72 | +		{  | 
 | 73 | +			name:     "gitlab multi subgroup https",  | 
 | 74 | +			in:       "https://gitlab.com/group/sub/sub2/repo",  | 
 | 75 | +			forkUser: "bob",  | 
 | 76 | +			expected: "https://gitlab.com/bob/repo",  | 
 | 77 | +		},  | 
 | 78 | +	}  | 
 | 79 | + | 
 | 80 | +	for _, c := range cases {  | 
 | 81 | +		t.Run(c.name, func(t *testing.T) {  | 
 | 82 | +			got, err := replaceForkUsername(c.in, c.forkUser)  | 
 | 83 | +			if err != nil {  | 
 | 84 | +				t.Fatalf("unexpected error: %v", err)  | 
 | 85 | +			}  | 
 | 86 | +			if got != c.expected {  | 
 | 87 | +				t.Fatalf("expected %q, got %q", c.expected, got)  | 
 | 88 | +			}  | 
 | 89 | +		})  | 
 | 90 | +	}  | 
 | 91 | +}  | 
 | 92 | + | 
 | 93 | +func TestReplaceForkUsername_Errors(t *testing.T) {  | 
 | 94 | +	cases := []struct {  | 
 | 95 | +		name     string  | 
 | 96 | +		in       string  | 
 | 97 | +		forkUser string  | 
 | 98 | +	}{  | 
 | 99 | +		{  | 
 | 100 | +			name:     "empty fork user",  | 
 | 101 | +			in:        "[email protected]:old/repo.git",   | 
 | 102 | +			forkUser: "",  | 
 | 103 | +		},  | 
 | 104 | +		{  | 
 | 105 | +			name:     "https host only",  | 
 | 106 | +			in:       "https://github.com",  | 
 | 107 | +			forkUser: "x",  | 
 | 108 | +		},  | 
 | 109 | +		{  | 
 | 110 | +			name:     "https host slash only",  | 
 | 111 | +			in:       "https://github.com/",  | 
 | 112 | +			forkUser: "x",  | 
 | 113 | +		},  | 
 | 114 | +		{  | 
 | 115 | +			name:     "https only repo (no owner)",  | 
 | 116 | +			in:       "https://github.com/repo.git",  | 
 | 117 | +			forkUser: "x",  | 
 | 118 | +		},  | 
 | 119 | +		{  | 
 | 120 | +			name:     "ssh missing path",  | 
 | 121 | + | 
 | 122 | +			forkUser: "x",  | 
 | 123 | +		},  | 
 | 124 | +		{  | 
 | 125 | +			name:     "ssh one segment only",  | 
 | 126 | + | 
 | 127 | +			forkUser: "x",  | 
 | 128 | +		},  | 
 | 129 | +		{  | 
 | 130 | +			name:     "unsupported scheme",  | 
 | 131 | +			in:        "ssh://[email protected]/old/repo.git",  // explicit ssh:// not supported here  | 
 | 132 | +			forkUser: "x",  | 
 | 133 | +		},  | 
 | 134 | +	}  | 
 | 135 | + | 
 | 136 | +	for _, c := range cases {  | 
 | 137 | +		t.Run(c.name, func(t *testing.T) {  | 
 | 138 | +			_, err := replaceForkUsername(c.in, c.forkUser)  | 
 | 139 | +			if err == nil {  | 
 | 140 | +				t.Fatalf("expected error but got nil")  | 
 | 141 | +			}  | 
 | 142 | +		})  | 
 | 143 | +	}  | 
 | 144 | +}  | 
0 commit comments