Introduce Instrumentation
[cps.git] / cps-application / src / test / groovy / org / onap / cps / rest / controller / ControllerSecuritySpec.groovy
1 /*
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
9  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.rest.controller
22
23 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
24
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.Specification
30
31 @WebMvcTest(TestController)
32 class ControllerSecuritySpec extends Specification {
33
34     @Autowired
35     MockMvc mvc
36
37     def testEndpoint = '/test'
38
39     def 'Get request with authentication'() {
40         when: 'request is sent with authentication'
41             def response = mvc.perform(
42                     get(testEndpoint).header("Authorization", 'Basic Y3BzdXNlcjpjcHNyMGNrcyE=')
43             ).andReturn().response
44         then: 'HTTP OK status code is returned'
45             assert response.status == HttpStatus.OK.value()
46     }
47
48     def 'Get request without authentication is not authorized'() {
49         when: 'request is sent without authentication'
50             def response = mvc.perform(get(testEndpoint)).andReturn().response
51         then: 'HTTP Unauthorized status code is returned'
52             assert response.status == HttpStatus.UNAUTHORIZED.value()
53     }
54
55     def 'Get request with invalid authentication is not authorized'() {
56         when: 'request is sent with invalid authentication'
57             def response = mvc.perform(
58                     get(testEndpoint).header("Authorization", 'Basic invalid auth')
59             ).andReturn().response
60         then: 'HTTP Unauthorized status code is returned'
61             assert response.status == HttpStatus.UNAUTHORIZED.value()
62     }
63 }