| 1 |
package cli |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/stretchr/testify/assert" |
| 7 |
"github.com/stretchr/testify/require" |
| 8 |
) |
| 9 |
|
| 10 |
func TestCallbackServerConfig(t *testing.T) { |
| 11 |
tests := []struct { |
| 12 |
name string |
| 13 |
redirectURI string |
| 14 |
callbackPath string |
| 15 |
port string |
| 16 |
wantListen string |
| 17 |
wantLoginURL string |
| 18 |
wantErr bool |
| 19 |
}{ |
| 20 |
{ |
| 21 |
name: "IPv4 loopback", |
| 22 |
redirectURI: "http://127.0.0.1:3000/callback", |
| 23 |
callbackPath: "/callback", |
| 24 |
port: "3000", |
| 25 |
wantListen: "127.0.0.1:3000", |
| 26 |
wantLoginURL: "http://127.0.0.1:3000/login", |
| 27 |
}, |
| 28 |
{ |
| 29 |
name: "IPv6 loopback", |
| 30 |
redirectURI: "http://[::1]:3000/callback", |
| 31 |
callbackPath: "/callback", |
| 32 |
port: "3000", |
| 33 |
wantListen: "[::1]:3000", |
| 34 |
wantLoginURL: "http://[::1]:3000/login", |
| 35 |
}, |
| 36 |
{ |
| 37 |
name: "localhost compatibility", |
| 38 |
redirectURI: "http://localhost:3000/callback", |
| 39 |
callbackPath: "/callback", |
| 40 |
port: "3000", |
| 41 |
wantListen: ":3000", |
| 42 |
wantLoginURL: "http://localhost:3000/login", |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: "missing port", |
| 46 |
redirectURI: "http://127.0.0.1/callback", |
| 47 |
callbackPath: "/callback", |
| 48 |
port: "3000", |
| 49 |
wantErr: true, |
| 50 |
}, |
| 51 |
{ |
| 52 |
name: "port mismatch", |
| 53 |
redirectURI: "http://127.0.0.1:3001/callback", |
| 54 |
callbackPath: "/callback", |
| 55 |
port: "3000", |
| 56 |
wantErr: true, |
| 57 |
}, |
| 58 |
{ |
| 59 |
name: "HTTPS", |
| 60 |
redirectURI: "https://127.0.0.1:3000/callback", |
| 61 |
callbackPath: "/callback", |
| 62 |
port: "3000", |
| 63 |
wantErr: true, |
| 64 |
}, |
| 65 |
{ |
| 66 |
name: "remote host", |
| 67 |
redirectURI: "http://example.com:3000/callback", |
| 68 |
callbackPath: "/callback", |
| 69 |
port: "3000", |
| 70 |
wantErr: true, |
| 71 |
}, |
| 72 |
{ |
| 73 |
name: "localhost lookalike", |
| 74 |
redirectURI: "http://localhost.attacker.com:3000/callback", |
| 75 |
callbackPath: "/callback", |
| 76 |
port: "3000", |
| 77 |
wantErr: true, |
| 78 |
}, |
| 79 |
{ |
| 80 |
name: "IPv4 lookalike", |
| 81 |
redirectURI: "http://127.0.0.1.attacker.com:3000/callback", |
| 82 |
callbackPath: "/callback", |
| 83 |
port: "3000", |
| 84 |
wantErr: true, |
| 85 |
}, |
| 86 |
{ |
| 87 |
name: "callback path mismatch", |
| 88 |
redirectURI: "http://127.0.0.1:3000/callback", |
| 89 |
callbackPath: "/other", |
| 90 |
port: "3000", |
| 91 |
wantErr: true, |
| 92 |
}, |
| 93 |
} |
| 94 |
|
| 95 |
for _, tt := range tests { |
| 96 |
t.Run(tt.name, func(t *testing.T) { |
| 97 |
listenAddress, loginURL, err := callbackServerConfig(tt.redirectURI, tt.callbackPath, tt.port) |
| 98 |
if tt.wantErr { |
| 99 |
require.Error(t, err) |
| 100 |
return |
| 101 |
} |
| 102 |
require.NoError(t, err) |
| 103 |
assert.Equal(t, tt.wantListen, listenAddress) |
| 104 |
assert.Equal(t, tt.wantLoginURL, loginURL) |
| 105 |
}) |
| 106 |
} |
| 107 |
} |