Skip to content
This repository has been archived by the owner on Jan 24, 2019. It is now read-only.

Commit

Permalink
Merge pull request #274 from ElDiabloComputatore/Azure_provider_mail_fix
Browse files Browse the repository at this point in the history
Getting mail for Azure provider fix + tests
  • Loading branch information
jehiah authored Jul 4, 2016
2 parents 9babca5 + 5acf96b commit da0bb76
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 4 deletions.
42 changes: 40 additions & 2 deletions providers/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"net/http"
"net/url"
"github.com/bitly/go-simplejson"
)

type AzureProvider struct {
Expand Down Expand Up @@ -65,7 +66,27 @@ func getAzureHeader(access_token string) http.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) {
var email string
var err error

if s.AccessToken == "" {
return "", errors.New("missing access token")
}
Expand All @@ -78,9 +99,26 @@ func (p *AzureProvider) GetEmailAddress(s *SessionState) (string, error) {
json, err := api.Request(req)

if err != nil {
log.Printf("failed making request %s", err)
return "", err
}

return json.Get("mail").String()
email, err = getEmailFromJSON(json)

if err == nil && email != "" {
return email, err
}

email, err = json.Get("userPrincipalName").String()

if err != nil {
log.Printf("failed making request %s", err)
return "", err
}

if email == "" {
log.Printf("failed to get email address")
return "", err
}

return email, err
}
69 changes: 67 additions & 2 deletions providers/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,76 @@ func TestAzureProviderGetEmailAddress(t *testing.T) {
b := testAzureBackend(`{ "mail": "user@windows.net" }`)
defer b.Close()

b_url, _ := url.Parse(b.URL)
p := testAzureProvider(b_url.Host)
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 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)
}

0 comments on commit da0bb76

Please sign in to comment.