2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Pantheon.tech
4 * Modifications Copyright (C) 2023 Nordix Foundation
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.rest.controller
23 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
25 import org.springframework.beans.factory.annotation.Autowired
26 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
27 import org.springframework.http.HttpStatus
28 import org.springframework.test.web.servlet.MockMvc
29 import spock.lang.Ignore
30 import spock.lang.Specification
32 @WebMvcTest(TestController)
33 class ControllerSecuritySpec extends Specification {
38 def testEndpoint = '/test'
40 def 'Get request with authentication'() {
41 when: 'request is sent with authentication'
42 def response = mvc.perform(
43 get(testEndpoint).header("Authorization", 'Basic Y3BzdXNlcjpjcHNyMGNrcyE=')
44 ).andReturn().response
45 then: 'HTTP OK status code is returned'
46 assert response.status == HttpStatus.OK.value()
50 def 'Get request without authentication is not authorized'() {
51 when: 'request is sent without authentication'
52 def response = mvc.perform(get(testEndpoint)).andReturn().response
53 then: 'HTTP Unauthorized status code is returned'
54 assert response.status == HttpStatus.UNAUTHORIZED.value()
58 def 'Get request with invalid authentication is not authorized'() {
59 when: 'request is sent with invalid authentication'
60 def response = mvc.perform(
61 get(testEndpoint).header("Authorization", 'Basic invalid auth')
62 ).andReturn().response
63 then: 'HTTP Unauthorized status code is returned'
64 assert response.status == HttpStatus.UNAUTHORIZED.value()