@@ -2,8 +2,10 @@ package goauth2_test
22
33import (
44 "testing"
5+ "time"
56
67 "github.com/inklabs/rangedb"
8+ "github.com/inklabs/rangedb/pkg/clock/provider/seededclock"
79 "github.com/stretchr/testify/assert"
810 "github.com/stretchr/testify/require"
911
@@ -25,6 +27,12 @@ const (
2527 passwordHash = "$2a$10$U6ej0p2d9Y8OO2635R7l/O4oEBvxgc9o6gCaQ1wjMZ77dr4qGl8nu"
2628 password = "Pass123!"
2729 refreshToken = "1eff35434eee448884a2d7e2dd28b119"
30+ authorizationCode = "afa410b917034f67b64ec9164bf4140d"
31+ )
32+
33+ var (
34+ issueTime = time .Date (2020 , 04 , 1 , 8 , 0 , 0 , 0 , time .UTC )
35+ issueTimePlus10Minutes = issueTime .Add (10 * time .Minute )
2836)
2937
3038func Test_OnBoardUser (t * testing.T ) {
@@ -808,3 +816,117 @@ func Test_RequestAccessTokenViaRefreshTokenGrant_For_ClientApplication(t *testin
808816 },
809817 ))
810818}
819+
820+ func Test_RequestAuthorizationCodeViaAuthorizationCodeGrant (t * testing.T ) {
821+ tokenGenerator := goauth2test .NewSeededTokenGenerator (authorizationCode )
822+ options := []goauth2.Option {
823+ goauth2 .WithTokenGenerator (tokenGenerator ),
824+ goauth2 .WithClock (seededclock .New (issueTime )),
825+ }
826+
827+ t .Run ("issues authorization code to user" , goauth2TestCase (options ... ).
828+ Given (
829+ goauth2.UserWasOnBoarded {
830+ UserID : userID ,
831+ Username : email ,
832+ PasswordHash : passwordHash ,
833+ },
834+ goauth2.ClientApplicationWasOnBoarded {
835+ ClientID : clientID ,
836+ ClientSecret : clientSecret ,
837+ RedirectUri : redirectUri ,
838+ UserID : adminUserID ,
839+ },
840+ ).
841+ When (goauth2.RequestAuthorizationCodeViaAuthorizationCodeGrant {
842+ UserID : userID ,
843+ ClientID : clientID ,
844+ RedirectUri : redirectUri ,
845+ Username : email ,
846+ Password : password ,
847+ }).
848+ Then (goauth2.AuthorizationCodeWasIssuedToUserViaAuthorizationCodeGrant {
849+ UserID : userID ,
850+ AuthorizationCode : authorizationCode ,
851+ ExpiresAt : issueTimePlus10Minutes .Unix (),
852+ }))
853+
854+ t .Run ("rejected due to missing client application id" , goauth2TestCase ().
855+ Given ().
856+ When (goauth2.RequestAuthorizationCodeViaAuthorizationCodeGrant {
857+ UserID : userID ,
858+ ClientID : clientID ,
859+ RedirectUri : redirectUri ,
860+ Username : email ,
861+ Password : password ,
862+ }).
863+ Then (goauth2.RequestAuthorizationCodeViaAuthorizationCodeGrantWasRejectedDueToInvalidClientApplicationID {
864+ UserID : userID ,
865+ ClientID : clientID ,
866+ }))
867+
868+ t .Run ("rejected due to invalid client application redirect uri" , goauth2TestCase ().
869+ Given (goauth2.ClientApplicationWasOnBoarded {
870+ ClientID : clientID ,
871+ ClientSecret : clientSecret ,
872+ RedirectUri : redirectUri ,
873+ UserID : adminUserID ,
874+ }).
875+ When (goauth2.RequestAuthorizationCodeViaAuthorizationCodeGrant {
876+ UserID : userID ,
877+ ClientID : clientID ,
878+ RedirectUri : wrongRedirectUri ,
879+ Username : email ,
880+ Password : password ,
881+ }).
882+ Then (goauth2.RequestAuthorizationCodeViaAuthorizationCodeGrantWasRejectedDueToInvalidClientApplicationRedirectUri {
883+ UserID : userID ,
884+ ClientID : clientID ,
885+ RedirectUri : wrongRedirectUri ,
886+ }))
887+
888+ t .Run ("rejected due to missing user" , goauth2TestCase ().
889+ Given (goauth2.ClientApplicationWasOnBoarded {
890+ ClientID : clientID ,
891+ ClientSecret : clientSecret ,
892+ RedirectUri : redirectUri ,
893+ UserID : adminUserID ,
894+ }).
895+ When (goauth2.RequestAuthorizationCodeViaAuthorizationCodeGrant {
896+ UserID : userID ,
897+ ClientID : clientID ,
898+ RedirectUri : redirectUri ,
899+ Username : email ,
900+ Password : password ,
901+ }).
902+ Then (goauth2.RequestAuthorizationCodeViaAuthorizationCodeGrantWasRejectedDueToInvalidUser {
903+ UserID : userID ,
904+ ClientID : clientID ,
905+ }))
906+
907+ t .Run ("rejected due to invalid user password" , goauth2TestCase ().
908+ Given (
909+ goauth2.UserWasOnBoarded {
910+ UserID : userID ,
911+ Username : email ,
912+ PasswordHash : passwordHash ,
913+ },
914+ goauth2.ClientApplicationWasOnBoarded {
915+ ClientID : clientID ,
916+ ClientSecret : clientSecret ,
917+ RedirectUri : redirectUri ,
918+ UserID : adminUserID ,
919+ },
920+ ).
921+ When (goauth2.RequestAuthorizationCodeViaAuthorizationCodeGrant {
922+ UserID : userID ,
923+ ClientID : clientID ,
924+ RedirectUri : redirectUri ,
925+ Username : email ,
926+ Password : "wrong-password" ,
927+ }).
928+ Then (goauth2.RequestAuthorizationCodeViaAuthorizationCodeGrantWasRejectedDueToInvalidUserPassword {
929+ UserID : userID ,
930+ ClientID : clientID ,
931+ }))
932+ }
0 commit comments