Skip to content

Commit

Permalink
Add unit test for oneOf
Browse files Browse the repository at this point in the history
KevinJoiner committed Nov 22, 2024
1 parent c2c844f commit 8c886e5
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions internal/auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit 8c886e5

Please sign in to comment.