Skip to content

Commit

Permalink
Add some tests for uncovered code (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
ewanharris authored Nov 10, 2023
1 parent 824469a commit 9daf157
Show file tree
Hide file tree
Showing 10 changed files with 462 additions and 0 deletions.
17 changes: 17 additions & 0 deletions auth0_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ func TestString(t *testing.T) {
}
}

func TestStringf(t *testing.T) {
for _, test := range []struct {
in *string
expected string
}{
{nil, ""},
{Stringf(""), ""},
{Stringf("%s", "foo"), "foo"},
{Stringf("%s %d", "bar", 1), "bar 1"},
} {
have := StringValue(test.in)
if have != test.expected {
t.Errorf("unexpected output. have %v, expected %v", have, test.expected)
}
}
}

func TestTime(t *testing.T) {
for _, test := range []struct {
in *time.Time
Expand Down
19 changes: 19 additions & 0 deletions management/branding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,23 @@ func TestBrandingColors(t *testing.T) {
assert.Equal(t, testCase.colors, &colors)
})
}

t.Run("Should error is not expected type", func(t *testing.T) {
var actual BrandingColors
err := json.Unmarshal([]byte(`{"page_background":123}`), &actual)
assert.Contains(t, err.Error(), "unexpected type for field page_background")
})

t.Run("Should disallow setting PageBackground and PageBackgroundGradient", func(t *testing.T) {
_, err := json.Marshal(&BrandingColors{
PageBackground: auth0.String("#ffffff"),
PageBackgroundGradient: &BrandingPageBackgroundGradient{
Type: auth0.String("linear-gradient"),
Start: auth0.String("#ffffff"),
End: auth0.String("#000000"),
AngleDegree: auth0.Int(3),
},
})
assert.Contains(t, err.Error(), "only one of PageBackground and PageBackgroundGradient is allowed")
})
}
9 changes: 9 additions & 0 deletions management/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ func TestJWTConfiguration(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, &actual, expected)
}

t.Run("Should error if unexpected type", func(t *testing.T) {
var actual ClientJWTConfiguration
err := json.Unmarshal([]byte(`{"lifetime_in_seconds":true}`), &actual)
assert.Contains(t, err.Error(), "unexpected type for field lifetime_in_seconds")

err = json.Unmarshal([]byte(`{"lifetime_in_seconds":"fooo"}`), &actual)
assert.Contains(t, err.Error(), "unexpected type for field lifetime_in_seconds")
})
})
}

Expand Down
24 changes: 24 additions & 0 deletions management/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ func TestLogManager(t *testing.T) {
)
assert.NoError(t, err)
assert.NotEmpty(t, logs)
assert.Equal(t, "API Operation", logs[0].TypeName())
}

func TestLogManager_Search(t *testing.T) {
configureHTTPTestRecordings(t)

// Limit results to 5 entries, starting from the first page.
logs, err := api.Log.Search(context.Background(), Page(1), PerPage(5))
assert.NoError(t, err)
assert.Greater(t, len(logs), 0, "can't seem to find any logs, have you ran any tests before?")

actualLog, err := api.Log.Read(context.Background(), logs[0].GetID())
assert.NoError(t, err)
assert.Equal(t, logs[0], actualLog)

// Search by type "Success API Operation" and limit results to 5 entries.
logs, err = api.Log.List(
context.Background(),
Parameter("q", fmt.Sprintf(`type:%q`, successfulAPIOperation)),
PerPage(5),
)
assert.NoError(t, err)
assert.NotEmpty(t, logs)
assert.Equal(t, "API Operation", logs[0].TypeName())
}

func TestLogManager_CheckpointPagination(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions management/management_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,14 @@ func TestNewError(t *testing.T) {
assert.Equal(t, &testCase.expectedError, actualError)
})
}

t.Run("Error should format into a string", func(t *testing.T) {
err := managementError{
StatusCode: 403,
Err: "Forbidden",
Message: "message",
}

assert.Equal(t, "403 Forbidden: message", err.Error())
})
}
21 changes: 21 additions & 0 deletions management/management_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/base64"
"encoding/json"
"io"
"log"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -168,6 +169,26 @@ func TestOptionSort(t *testing.T) {
assert.Equal(t, "name:-1", sort)
}

func TestOptionHeader(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)

Header("foo", "bar").apply(r)

v := r.Header.Get("foo")
assert.Equal(t, "bar", v)
}

func TestOptionBody(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)

body := []byte("fooo")
Body(body).apply(r)

v, err := io.ReadAll(r.Body)
assert.NoError(t, err)
assert.Equal(t, body, v)
}

func TestStringify(t *testing.T) {
expected := `{
"foo": "bar"
Expand Down
19 changes: 19 additions & 0 deletions management/tenant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ func TestTenantUniversalLoginColors_MarshalJSON(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, expected, string(payload))
}

t.Run("Should disallow setting PageBackground and PageBackgroundGradient", func(t *testing.T) {
_, err := json.Marshal(&TenantUniversalLoginColors{
PageBackground: auth0.String("#ffffff"),
PageBackgroundGradient: &BrandingPageBackgroundGradient{
Type: auth0.String("linear-gradient"),
Start: auth0.String("#ffffff"),
End: auth0.String("#000000"),
AngleDegree: auth0.Int(3),
},
})
assert.Contains(t, err.Error(), "only one of PageBackground and PageBackgroundGradient is allowed")
})
}

func TestTenantUniversalLoginColors_UnmarshalJSON(t *testing.T) {
Expand All @@ -123,4 +136,10 @@ func TestTenantUniversalLoginColors_UnmarshalJSON(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, &actual, expected)
}

t.Run("Should error is not expected type", func(t *testing.T) {
var actual TenantUniversalLoginColors
err := json.Unmarshal([]byte(`{"page_background":123}`), &actual)
assert.Contains(t, err.Error(), "unexpected type for field page_background")
})
}
15 changes: 15 additions & 0 deletions management/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,21 @@ func TestUserManager_AuthenticationMethods(t *testing.T) {
assert.Len(t, methods.Authenticators, 0)
}

func TestUserManager_Organizations(t *testing.T) {
configureHTTPTestRecordings(t)

user := givenAUser(t)
org := givenAnOrganization(t)

err := api.Organization.AddMembers(context.Background(), org.GetID(), []string{user.GetID()})
require.NoError(t, err)

orgs, err := api.User.Organizations(context.Background(), user.GetID())
require.NoError(t, err)
assert.Len(t, orgs.Organizations, 1)
assert.Equal(t, org.GetID(), orgs.Organizations[0].GetID())
}

func givenAUser(t *testing.T) *User {
t.Helper()

Expand Down
111 changes: 111 additions & 0 deletions test/data/recordings/TestLogManager_Search.yaml

Large diffs are not rendered by default.

Loading

0 comments on commit 9daf157

Please sign in to comment.