|
|
|
|
@ -4,6 +4,7 @@ import (
|
|
|
|
|
"context" |
|
|
|
|
"crypto/tls" |
|
|
|
|
"encoding/json" |
|
|
|
|
"errors" |
|
|
|
|
"fmt" |
|
|
|
|
"net/http" |
|
|
|
|
"net/http/httptest" |
|
|
|
|
@ -198,6 +199,290 @@ func TestLoginUsedAsIDWhenConfigured(t *testing.T) {
|
|
|
|
|
expectEquals(t, identity.Username, "Joe Bloggs") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestPreferredEmailDomainConfigured(t *testing.T) { |
|
|
|
|
ctx := context.Background() |
|
|
|
|
s := newTestServer(map[string]testResponse{ |
|
|
|
|
"/user": {data: user{Login: "some-login", ID: 12345678, Name: "Joe Bloggs"}}, |
|
|
|
|
"/user/emails": { |
|
|
|
|
data: []userEmail{ |
|
|
|
|
{ |
|
|
|
|
Email: "some@email.com", |
|
|
|
|
Verified: true, |
|
|
|
|
Primary: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Email: "another@email.com", |
|
|
|
|
Verified: true, |
|
|
|
|
Primary: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Email: "some@preferred-domain.com", |
|
|
|
|
Verified: true, |
|
|
|
|
Primary: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Email: "another@preferred-domain.com", |
|
|
|
|
Verified: true, |
|
|
|
|
Primary: false, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}) |
|
|
|
|
defer s.Close() |
|
|
|
|
|
|
|
|
|
hostURL, err := url.Parse(s.URL) |
|
|
|
|
expectNil(t, err) |
|
|
|
|
|
|
|
|
|
client := newClient() |
|
|
|
|
c := githubConnector{apiURL: s.URL, hostName: hostURL.Host, httpClient: client, preferredEmailDomain: "preferred-domain.com"} |
|
|
|
|
|
|
|
|
|
u, err := c.user(ctx, client) |
|
|
|
|
expectNil(t, err) |
|
|
|
|
expectEquals(t, u.Email, "some@preferred-domain.com") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestPreferredEmailDomainConfiguredWithGlob(t *testing.T) { |
|
|
|
|
ctx := context.Background() |
|
|
|
|
s := newTestServer(map[string]testResponse{ |
|
|
|
|
"/user": {data: user{Login: "some-login", ID: 12345678, Name: "Joe Bloggs"}}, |
|
|
|
|
"/user/emails": { |
|
|
|
|
data: []userEmail{ |
|
|
|
|
{ |
|
|
|
|
Email: "some@email.com", |
|
|
|
|
Verified: true, |
|
|
|
|
Primary: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Email: "another@email.com", |
|
|
|
|
Verified: true, |
|
|
|
|
Primary: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Email: "some@another.preferred-domain.com", |
|
|
|
|
Verified: true, |
|
|
|
|
Primary: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Email: "some@sub-domain.preferred-domain.co", |
|
|
|
|
Verified: true, |
|
|
|
|
Primary: false, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}) |
|
|
|
|
defer s.Close() |
|
|
|
|
|
|
|
|
|
hostURL, err := url.Parse(s.URL) |
|
|
|
|
expectNil(t, err) |
|
|
|
|
|
|
|
|
|
client := newClient() |
|
|
|
|
c := githubConnector{apiURL: s.URL, hostName: hostURL.Host, httpClient: client, preferredEmailDomain: "*.preferred-domain.co"} |
|
|
|
|
|
|
|
|
|
u, err := c.user(ctx, client) |
|
|
|
|
expectNil(t, err) |
|
|
|
|
expectEquals(t, u.Email, "some@sub-domain.preferred-domain.co") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestPreferredEmailDomainConfigured_UserHasNoPreferredDomainEmail(t *testing.T) { |
|
|
|
|
ctx := context.Background() |
|
|
|
|
s := newTestServer(map[string]testResponse{ |
|
|
|
|
"/user": {data: user{Login: "some-login", ID: 12345678, Name: "Joe Bloggs"}}, |
|
|
|
|
"/user/emails": { |
|
|
|
|
data: []userEmail{ |
|
|
|
|
{ |
|
|
|
|
Email: "some@email.com", |
|
|
|
|
Verified: true, |
|
|
|
|
Primary: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Email: "another@email.com", |
|
|
|
|
Verified: true, |
|
|
|
|
Primary: false, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}) |
|
|
|
|
defer s.Close() |
|
|
|
|
|
|
|
|
|
hostURL, err := url.Parse(s.URL) |
|
|
|
|
expectNil(t, err) |
|
|
|
|
|
|
|
|
|
client := newClient() |
|
|
|
|
c := githubConnector{apiURL: s.URL, hostName: hostURL.Host, httpClient: client, preferredEmailDomain: "preferred-domain.com"} |
|
|
|
|
|
|
|
|
|
u, err := c.user(ctx, client) |
|
|
|
|
expectNil(t, err) |
|
|
|
|
expectEquals(t, u.Email, "some@email.com") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestPreferredEmailDomainNotConfigured(t *testing.T) { |
|
|
|
|
ctx := context.Background() |
|
|
|
|
s := newTestServer(map[string]testResponse{ |
|
|
|
|
"/user": {data: user{Login: "some-login", ID: 12345678, Name: "Joe Bloggs"}}, |
|
|
|
|
"/user/emails": { |
|
|
|
|
data: []userEmail{ |
|
|
|
|
{ |
|
|
|
|
Email: "some@email.com", |
|
|
|
|
Verified: true, |
|
|
|
|
Primary: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Email: "another@email.com", |
|
|
|
|
Verified: true, |
|
|
|
|
Primary: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Email: "some@preferred-domain.com", |
|
|
|
|
Verified: true, |
|
|
|
|
Primary: false, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}) |
|
|
|
|
defer s.Close() |
|
|
|
|
|
|
|
|
|
hostURL, err := url.Parse(s.URL) |
|
|
|
|
expectNil(t, err) |
|
|
|
|
|
|
|
|
|
client := newClient() |
|
|
|
|
c := githubConnector{apiURL: s.URL, hostName: hostURL.Host, httpClient: client} |
|
|
|
|
|
|
|
|
|
u, err := c.user(ctx, client) |
|
|
|
|
expectNil(t, err) |
|
|
|
|
expectEquals(t, u.Email, "some@email.com") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestPreferredEmailDomainConfigured_Error_BothPrimaryAndPreferredDomainEmailNotFound(t *testing.T) { |
|
|
|
|
ctx := context.Background() |
|
|
|
|
s := newTestServer(map[string]testResponse{ |
|
|
|
|
"/user": {data: user{Login: "some-login", ID: 12345678, Name: "Joe Bloggs"}}, |
|
|
|
|
"/user/emails": { |
|
|
|
|
data: []userEmail{ |
|
|
|
|
{ |
|
|
|
|
Email: "some@email.com", |
|
|
|
|
Verified: true, |
|
|
|
|
Primary: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Email: "another@email.com", |
|
|
|
|
Verified: true, |
|
|
|
|
Primary: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Email: "some@preferred-domain.com", |
|
|
|
|
Verified: true, |
|
|
|
|
Primary: false, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}) |
|
|
|
|
defer s.Close() |
|
|
|
|
|
|
|
|
|
hostURL, err := url.Parse(s.URL) |
|
|
|
|
expectNil(t, err) |
|
|
|
|
|
|
|
|
|
client := newClient() |
|
|
|
|
c := githubConnector{apiURL: s.URL, hostName: hostURL.Host, httpClient: client, preferredEmailDomain: "foo.bar"} |
|
|
|
|
|
|
|
|
|
_, err = c.user(ctx, client) |
|
|
|
|
expectNotNil(t, err, "Email not found error") |
|
|
|
|
expectEquals(t, err.Error(), "github: user has no verified, primary email or preferred-domain email") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func Test_isPreferredEmailDomain(t *testing.T) { |
|
|
|
|
client := newClient() |
|
|
|
|
tests := []struct { |
|
|
|
|
preferredEmailDomain string |
|
|
|
|
email string |
|
|
|
|
expected bool |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
preferredEmailDomain: "example.com", |
|
|
|
|
email: "test@example.com", |
|
|
|
|
expected: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
preferredEmailDomain: "example.com", |
|
|
|
|
email: "test@another.com", |
|
|
|
|
expected: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
preferredEmailDomain: "*.example.com", |
|
|
|
|
email: "test@my.example.com", |
|
|
|
|
expected: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
preferredEmailDomain: "*.example.com", |
|
|
|
|
email: "test@my.another.com", |
|
|
|
|
expected: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
preferredEmailDomain: "*.example.com", |
|
|
|
|
email: "test@my.domain.example.com", |
|
|
|
|
expected: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
preferredEmailDomain: "*.example.com", |
|
|
|
|
email: "test@sub.domain.com", |
|
|
|
|
expected: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
preferredEmailDomain: "*.*.example.com", |
|
|
|
|
email: "test@sub.my.example.com", |
|
|
|
|
expected: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
preferredEmailDomain: "*.*.example.com", |
|
|
|
|
email: "test@a.my.google.com", |
|
|
|
|
expected: false, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
for _, test := range tests { |
|
|
|
|
t.Run(test.preferredEmailDomain, func(t *testing.T) { |
|
|
|
|
c := githubConnector{apiURL: "apiURL", hostName: "github.com", httpClient: client, preferredEmailDomain: test.preferredEmailDomain} |
|
|
|
|
_, domainPart, _ := strings.Cut(test.email, "@") |
|
|
|
|
res := c.isPreferredEmailDomain(domainPart) |
|
|
|
|
|
|
|
|
|
expectEquals(t, res, test.expected) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func Test_Open_PreferredDomainConfig(t *testing.T) { |
|
|
|
|
tests := []struct { |
|
|
|
|
preferredEmailDomain string |
|
|
|
|
email string |
|
|
|
|
expected error |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
preferredEmailDomain: "example.com", |
|
|
|
|
expected: nil, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
preferredEmailDomain: "*.example.com", |
|
|
|
|
expected: nil, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
preferredEmailDomain: "*.*.example.com", |
|
|
|
|
expected: nil, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
preferredEmailDomain: "example.*", |
|
|
|
|
expected: errors.New("invalid PreferredEmailDomain: glob pattern cannot end with \"*\""), |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
for _, test := range tests { |
|
|
|
|
t.Run(test.preferredEmailDomain, func(t *testing.T) { |
|
|
|
|
c := Config{ |
|
|
|
|
PreferredEmailDomain: test.preferredEmailDomain, |
|
|
|
|
} |
|
|
|
|
_, err := c.Open("id", nil) |
|
|
|
|
|
|
|
|
|
expectEquals(t, err, test.expected) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func newTestServer(responses map[string]testResponse) *httptest.Server { |
|
|
|
|
var s *httptest.Server |
|
|
|
|
s = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
@ -231,6 +516,12 @@ func expectNil(t *testing.T, a interface{}) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func expectNotNil(t *testing.T, a interface{}, msg string) { |
|
|
|
|
if a == nil { |
|
|
|
|
t.Errorf("Expected %+v to not to be nil", msg) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func expectEquals(t *testing.T, a interface{}, b interface{}) { |
|
|
|
|
if !reflect.DeepEqual(a, b) { |
|
|
|
|
t.Errorf("Expected %+v to equal %+v", a, b) |
|
|
|
|
|