Getting mail for Azure provider fix + tests
This commit is contained in:
parent
f9e649456b
commit
5acf96b75d
@ -7,6 +7,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"github.com/bitly/go-simplejson"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AzureProvider struct {
|
type AzureProvider struct {
|
||||||
@ -65,7 +66,27 @@ func getAzureHeader(access_token string) http.Header {
|
|||||||
return header
|
return header
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getEmailFromJSON(json *simplejson.Json) (string, error) {
|
||||||
|
var email string
|
||||||
|
var err error
|
||||||
|
|
||||||
|
email, err = json.Get("mail").String()
|
||||||
|
|
||||||
|
if err != nil || email == "" {
|
||||||
|
otherMails, otherMailsErr := json.Get("otherMails").Array()
|
||||||
|
if len(otherMails) > 0{
|
||||||
|
email = otherMails[0].(string)
|
||||||
|
}
|
||||||
|
err = otherMailsErr
|
||||||
|
}
|
||||||
|
|
||||||
|
return email, err
|
||||||
|
}
|
||||||
|
|
||||||
func (p *AzureProvider) GetEmailAddress(s *SessionState) (string, error) {
|
func (p *AzureProvider) GetEmailAddress(s *SessionState) (string, error) {
|
||||||
|
var email string
|
||||||
|
var err error
|
||||||
|
|
||||||
if s.AccessToken == "" {
|
if s.AccessToken == "" {
|
||||||
return "", errors.New("missing access token")
|
return "", errors.New("missing access token")
|
||||||
}
|
}
|
||||||
@ -77,10 +98,27 @@ func (p *AzureProvider) GetEmailAddress(s *SessionState) (string, error) {
|
|||||||
|
|
||||||
json, err := api.Request(req)
|
json, err := api.Request(req)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
email, err = getEmailFromJSON(json)
|
||||||
|
|
||||||
|
if err == nil && email != "" {
|
||||||
|
return email, err
|
||||||
|
}
|
||||||
|
|
||||||
|
email, err = json.Get("userPrincipalName").String()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed making request %s", err)
|
log.Printf("failed making request %s", err)
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return json.Get("mail").String()
|
if email == "" {
|
||||||
|
log.Printf("failed to get email address")
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return email, err
|
||||||
}
|
}
|
||||||
|
@ -125,11 +125,76 @@ func TestAzureProviderGetEmailAddress(t *testing.T) {
|
|||||||
b := testAzureBackend(`{ "mail": "user@windows.net" }`)
|
b := testAzureBackend(`{ "mail": "user@windows.net" }`)
|
||||||
defer b.Close()
|
defer b.Close()
|
||||||
|
|
||||||
b_url, _ := url.Parse(b.URL)
|
bURL, _ := url.Parse(b.URL)
|
||||||
p := testAzureProvider(b_url.Host)
|
p := testAzureProvider(bURL.Host)
|
||||||
|
|
||||||
session := &SessionState{AccessToken: "imaginary_access_token"}
|
session := &SessionState{AccessToken: "imaginary_access_token"}
|
||||||
email, err := p.GetEmailAddress(session)
|
email, err := p.GetEmailAddress(session)
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
assert.Equal(t, "user@windows.net", email)
|
assert.Equal(t, "user@windows.net", email)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAzureProviderGetEmailAddressMailNull(t *testing.T) {
|
||||||
|
b := testAzureBackend(`{ "mail": null, "otherMails": ["user@windows.net", "altuser@windows.net"] }`)
|
||||||
|
defer b.Close()
|
||||||
|
|
||||||
|
bURL, _ := url.Parse(b.URL)
|
||||||
|
p := testAzureProvider(bURL.Host)
|
||||||
|
|
||||||
|
session := &SessionState{AccessToken: "imaginary_access_token"}
|
||||||
|
email, err := p.GetEmailAddress(session)
|
||||||
|
assert.Equal(t, nil, err)
|
||||||
|
assert.Equal(t, "user@windows.net", email)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAzureProviderGetEmailAddressGetUserPrincipalName(t *testing.T) {
|
||||||
|
b := testAzureBackend(`{ "mail": null, "otherMails": [], "userPrincipalName": "user@windows.net" }`)
|
||||||
|
defer b.Close()
|
||||||
|
|
||||||
|
bURL, _ := url.Parse(b.URL)
|
||||||
|
p := testAzureProvider(bURL.Host)
|
||||||
|
|
||||||
|
session := &SessionState{AccessToken: "imaginary_access_token"}
|
||||||
|
email, err := p.GetEmailAddress(session)
|
||||||
|
assert.Equal(t, nil, err)
|
||||||
|
assert.Equal(t, "user@windows.net", email)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAzureProviderGetEmailAddressFailToGetEmailAddress(t *testing.T) {
|
||||||
|
b := testAzureBackend(`{ "mail": null, "otherMails": [], "userPrincipalName": null }`)
|
||||||
|
defer b.Close()
|
||||||
|
|
||||||
|
bURL, _ := url.Parse(b.URL)
|
||||||
|
p := testAzureProvider(bURL.Host)
|
||||||
|
|
||||||
|
session := &SessionState{AccessToken: "imaginary_access_token"}
|
||||||
|
email, err := p.GetEmailAddress(session)
|
||||||
|
assert.Equal(t, "type assertion to string failed", err.Error())
|
||||||
|
assert.Equal(t, "", email)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAzureProviderGetEmailAddressEmptyUserPrincipalName(t *testing.T) {
|
||||||
|
b := testAzureBackend(`{ "mail": null, "otherMails": [], "userPrincipalName": "" }`)
|
||||||
|
defer b.Close()
|
||||||
|
|
||||||
|
bURL, _ := url.Parse(b.URL)
|
||||||
|
p := testAzureProvider(bURL.Host)
|
||||||
|
|
||||||
|
session := &SessionState{AccessToken: "imaginary_access_token"}
|
||||||
|
email, err := p.GetEmailAddress(session)
|
||||||
|
assert.Equal(t, nil, err)
|
||||||
|
assert.Equal(t, "", email)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAzureProviderGetEmailAddressIncorrectOtherMails(t *testing.T) {
|
||||||
|
b := testAzureBackend(`{ "mail": null, "otherMails": "", "userPrincipalName": null }`)
|
||||||
|
defer b.Close()
|
||||||
|
|
||||||
|
bURL, _ := url.Parse(b.URL)
|
||||||
|
p := testAzureProvider(bURL.Host)
|
||||||
|
|
||||||
|
session := &SessionState{AccessToken: "imaginary_access_token"}
|
||||||
|
email, err := p.GetEmailAddress(session)
|
||||||
|
assert.Equal(t, "type assertion to string failed", err.Error())
|
||||||
|
assert.Equal(t, "", email)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user