|
| 1 | +/* |
| 2 | + * Copyright 2016 Google, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License") |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.netflix.spinnaker.fiat.shared |
| 18 | + |
| 19 | +import com.netflix.spinnaker.fiat.model.resources.ResourceType |
| 20 | +import org.springframework.security.core.Authentication |
| 21 | +import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken |
| 22 | +import retrofit.RetrofitError |
| 23 | +import retrofit.client.Response |
| 24 | +import spock.lang.Specification |
| 25 | +import spock.lang.Unroll |
| 26 | + |
| 27 | +class FiatPermissionEvaluatorSpec extends Specification { |
| 28 | + |
| 29 | + @Unroll |
| 30 | + def "should parse application name"() { |
| 31 | + setup: |
| 32 | + FiatService fiatService = Mock(FiatService) |
| 33 | + FiatPermissionEvaluator evaluator = new FiatPermissionEvaluator(fiatEnabled: true, |
| 34 | + fiatService: fiatService); |
| 35 | + |
| 36 | + Authentication authentication = new PreAuthenticatedAuthenticationToken("testUser", |
| 37 | + null, |
| 38 | + new ArrayList<>()); |
| 39 | + |
| 40 | + when: |
| 41 | + def result = evaluator.hasPermission(authentication, |
| 42 | + resource, |
| 43 | + resourceType.name(), |
| 44 | + authorization) |
| 45 | + |
| 46 | + then: |
| 47 | + 1 * fiatService.hasAuthorization("testUser", resourceType.name(), resourceName, authorization) >> { |
| 48 | + throw RetrofitError.httpError( |
| 49 | + "/", |
| 50 | + new Response("/", 404, "not found", Collections.emptyList(), null), |
| 51 | + null, |
| 52 | + null) |
| 53 | + } |
| 54 | + !result |
| 55 | + |
| 56 | + when: |
| 57 | + result = evaluator.hasPermission(authentication, |
| 58 | + resource, |
| 59 | + resourceType.name(), |
| 60 | + authorization) |
| 61 | + |
| 62 | + then: |
| 63 | + 1 * fiatService.hasAuthorization("testUser", resourceType.name(), resourceName, authorization) |
| 64 | + result |
| 65 | + |
| 66 | + where: |
| 67 | + resource | resourceName | resourceType |
| 68 | + "abc" | "abc" | ResourceType.APPLICATION |
| 69 | + "abc-def" | "abc" | ResourceType.APPLICATION |
| 70 | + "abc-def-ghi" | "abc" | ResourceType.APPLICATION |
| 71 | + "abc-def-ghi-1234" | "abc" | ResourceType.APPLICATION |
| 72 | + |
| 73 | + authorization = 'READ' |
| 74 | + } |
| 75 | +} |
0 commit comments