@@ -1170,4 +1170,103 @@ func TestExtractCredentialTypes(t *testing.T) {
11701170 }
11711171 })
11721172 }
1173+
1174+ }
1175+ func TestGenerateToken (t * testing.T ) {
1176+
1177+ logging .Configure (true , "DEBUG" , true , []string {})
1178+
1179+ type test struct {
1180+ testName string
1181+ clientId string
1182+ subject string
1183+ audience string
1184+ scopes []string
1185+ presentation * verifiable.Presentation
1186+ credentialScopes map [string ]map [string ]configModel.ScopeEntry
1187+ configError error
1188+ mockTokenSignError error
1189+ expectedError error
1190+ }
1191+
1192+ testKey := getECDSAKey ()
1193+ emptyPresentation , _ := verifiable .NewPresentation ()
1194+ vc1 , _ := verifiable .CreateCredential (verifiable.CredentialContents {
1195+ ID : "vc1" ,
1196+ Types : []string {"type1" , "typeA" },
1197+ }, verifiable.CustomFields {})
1198+ invalidPresentation , _ := verifiable .NewPresentation (verifiable .WithCredentials (vc1 ))
1199+
1200+ tests := []test {
1201+ {
1202+ testName : "When presentation is empty, ErrorNoValidCredentialTypeProvided should be returned" ,
1203+ clientId : "test-client" ,
1204+ subject : "subject-id" ,
1205+ audience : "audience-id" ,
1206+ scopes : []string {"test-scope" },
1207+ presentation : emptyPresentation ,
1208+ credentialScopes : map [string ]map [string ]configModel.ScopeEntry {},
1209+ configError : nil ,
1210+ mockTokenSignError : nil ,
1211+ expectedError : ErrorNoValidCredentialTypeProvided ,
1212+ },
1213+ {
1214+ testName : "When presentation has not valid types, ErrorNoValidCredentialTypeProvided should be returned" ,
1215+ clientId : "test-client" ,
1216+ subject : "subject-id" ,
1217+ audience : "audience-id" ,
1218+ scopes : []string {"test-scope" },
1219+ presentation : invalidPresentation ,
1220+ credentialScopes : map [string ]map [string ]configModel.ScopeEntry {},
1221+ configError : nil ,
1222+ mockTokenSignError : nil ,
1223+ expectedError : ErrorNoValidCredentialTypeProvided ,
1224+ },
1225+ }
1226+
1227+ for _ , tc := range tests {
1228+ t .Run (tc .testName , func (t * testing.T ) {
1229+ mockConfig := mockCredentialConfig {mockScopes : tc .credentialScopes , mockError : tc .configError }
1230+
1231+ verifier := CredentialVerifier {
1232+ credentialsConfig : & mockConfig ,
1233+ validationServices : []ValidationService {},
1234+ signingKey : testKey ,
1235+ clock : mockClock {},
1236+ tokenSigner : mockTokenSigner {tc .mockTokenSignError },
1237+ signingAlgorithm : "ES256" ,
1238+ host : "https://verifier.example.com" ,
1239+ jwtExpiration : time .Hour ,
1240+ }
1241+
1242+ expiration , tokenString , err := verifier .GenerateToken (tc .clientId , tc .subject , tc .audience , tc .scopes , tc .presentation )
1243+
1244+ if tc .expectedError != nil {
1245+ if err == nil {
1246+ t .Errorf ("%s - Expected error %v but got none." , tc .testName , tc .expectedError )
1247+ return
1248+ }
1249+ if err .Error () != tc .expectedError .Error () {
1250+ t .Errorf ("%s - Expected error %v but was %v." , tc .testName , tc .expectedError , err )
1251+ return
1252+ }
1253+ return
1254+ }
1255+
1256+ if err != nil {
1257+ t .Errorf ("%s - Expected no error but got %v." , tc .testName , err )
1258+ return
1259+ }
1260+
1261+ if tokenString == "" {
1262+ t .Errorf ("%s - Expected token string but got empty." , tc .testName )
1263+ return
1264+ }
1265+
1266+ if expiration == 0 {
1267+ t .Errorf ("%s - Expected expiration but got 0." , tc .testName )
1268+ return
1269+ }
1270+ })
1271+ }
11731272}
0 commit comments