-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use go-validator with IDTokenClaims (#902)
Co-authored-by: Max Schäfer <[email protected]>
- Loading branch information
1 parent
265bbd8
commit dbf8671
Showing
2 changed files
with
113 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package auth | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestIDTokenClaims_AsExpected(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
claims IDTokenClaims | ||
expectedError bool | ||
}{ | ||
{ | ||
name: "Valid IDTokenClaims", | ||
claims: IDTokenClaims{ | ||
Sub: "6759b6d7-a864-800c-a2e9-a780a83ec767", | ||
Email: "[email protected]", | ||
Name: "Test User", | ||
PreferredUsername: "testuser", | ||
Organization: &OrganizationTokenClaim{ | ||
Id: "6759b6d7-a864-800c-a2e9-a780a83ec767", | ||
Name: "Example Org", | ||
}, | ||
}, | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "Missing Sub", | ||
claims: IDTokenClaims{ | ||
Email: "[email protected]", | ||
Name: "Test User", | ||
PreferredUsername: "testuser", | ||
Organization: &OrganizationTokenClaim{ | ||
Id: "6759b6d7-a864-800c-a2e9-a780a83ec767", | ||
Name: "Example Org", | ||
}, | ||
}, | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "Invalid Email", | ||
claims: IDTokenClaims{ | ||
Sub: "6759b6d7-a864-800c-a2e9-a780a83ec767", | ||
Email: "invalid-email", | ||
Name: "Test User", | ||
PreferredUsername: "testuser", | ||
Organization: &OrganizationTokenClaim{ | ||
Id: "6759b6d7-a864-800c-a2e9-a780a83ec767", | ||
Name: "Example Org", | ||
}, | ||
}, | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "Missing Organization", | ||
claims: IDTokenClaims{ | ||
Sub: "6759b6d7-a864-800c-a2e9-a780a83ec767", | ||
Email: "[email protected]", | ||
Name: "Test User", | ||
PreferredUsername: "testuser", | ||
}, | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "Invalid Organization ID", | ||
claims: IDTokenClaims{ | ||
Sub: "6759b6d7-a864-800c-a2e9-a780a83ec767", | ||
Email: "[email protected]", | ||
Name: "Test User", | ||
PreferredUsername: "testuser", | ||
Organization: &OrganizationTokenClaim{ | ||
Id: "asdasd", | ||
Name: "Example Org", | ||
}, | ||
}, | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "Default Organization ID", | ||
claims: IDTokenClaims{ | ||
Sub: "6759b6d7-a864-800c-a2e9-a780a83ec767", | ||
Email: "[email protected]", | ||
Name: "Test User", | ||
PreferredUsername: "testuser", | ||
Organization: &OrganizationTokenClaim{ | ||
Id: "", | ||
Name: "", | ||
}, | ||
}, | ||
expectedError: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.claims.AsExpected() | ||
if tt.expectedError { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |