From 8c886e5a8471e81d659362abac4f498344510eef Mon Sep 17 00:00:00 2001 From: Kevin Joiner <10265309+KevinJoiner@users.noreply.github.com> Date: Fri, 22 Nov 2024 13:26:31 -0500 Subject: [PATCH] Add unit test for oneOf --- internal/auth/auth_test.go | 105 +++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go index a0b58dc..b9e9927 100644 --- a/internal/auth/auth_test.go +++ b/internal/auth/auth_test.go @@ -339,3 +339,108 @@ func TestRequiresPrivilegeCheck(t *testing.T) { }) } } + +func TestRequiresOneOfPrivilegeCheck(t *testing.T) { + t.Parallel() + vehicleNFTAddr := common.BigToAddress(big.NewInt(10)) + manufNFTAddr := common.BigToAddress(big.NewInt(11)) + + privMaps := map[common.Address]map[privileges.Privilege]model.Privilege{ + vehicleNFTAddr: vehiclePrivToAPI, + manufNFTAddr: manufacturerPrivToAPI, + } + + testCases := []struct { + name string + privs []model.Privilege + telemetryClaim *TelemetryClaim + expectedError bool + }{ + { + name: "has_one_of_required_privileges", + privs: []model.Privilege{ + model.PrivilegeVehicleAllTimeLocation, + model.PrivilegeVehicleNonLocationData, + }, + telemetryClaim: &TelemetryClaim{ + CustomClaims: privilegetoken.CustomClaims{ + PrivilegeIDs: []privileges.Privilege{ + privileges.VehicleAllTimeLocation, + }, + ContractAddress: vehicleNFTAddr, + }, + }, + expectedError: false, + }, + { + name: "has_all_required_privileges", + privs: []model.Privilege{ + model.PrivilegeVehicleAllTimeLocation, + model.PrivilegeVehicleNonLocationData, + }, + telemetryClaim: &TelemetryClaim{ + CustomClaims: privilegetoken.CustomClaims{ + PrivilegeIDs: []privileges.Privilege{ + privileges.VehicleAllTimeLocation, + privileges.VehicleNonLocationData, + }, + ContractAddress: vehicleNFTAddr, + }, + }, + expectedError: false, + }, + { + name: "missing_all_privileges", + privs: []model.Privilege{ + model.PrivilegeVehicleAllTimeLocation, + model.PrivilegeVehicleNonLocationData, + }, + telemetryClaim: &TelemetryClaim{ + CustomClaims: privilegetoken.CustomClaims{ + PrivilegeIDs: nil, + ContractAddress: vehicleNFTAddr, + }, + }, + expectedError: true, + }, + { + name: "missing_claim", + privs: []model.Privilege{}, + telemetryClaim: nil, + expectedError: true, + }, + { + name: "wrong_contract_for_privilege", + privs: []model.Privilege{ + model.PrivilegeVehicleAllTimeLocation, + }, + telemetryClaim: &TelemetryClaim{ + CustomClaims: privilegetoken.CustomClaims{ + PrivilegeIDs: []privileges.Privilege{ + privileges.ManufacturerDeviceDefinitionInsert, + }, + ContractAddress: manufNFTAddr, + }, + }, + expectedError: true, + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + if tc.telemetryClaim != nil { + tc.telemetryClaim.SetPrivileges(privMaps) + } + testCtx := context.WithValue(context.Background(), TelemetryClaimContextKey{}, tc.telemetryClaim) + next, err := OneOfPrivilegeCheck(testCtx, nil, emptyResolver, tc.privs) + if tc.expectedError { + require.Error(t, err) + } else { + require.NoError(t, err) + require.Equal(t, expectedReturn, next) + } + }) + } +}