Replace deprecated WebSecurityConfigurerAdapter
[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 org.onap.cps.config.WebSecurityConfig
24 import org.springframework.context.annotation.Import
25
26 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
27
28 import org.springframework.beans.factory.annotation.Autowired
29 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
30 import org.springframework.http.HttpStatus
31 import org.springframework.test.web.servlet.MockMvc
32 import spock.lang.Specification
33
34 @WebMvcTest(TestController)
35 @Import(WebSecurityConfig)
36 class ControllerSecuritySpec extends Specification {
37
38     @Autowired
39     MockMvc mvc
40
41     def testEndpoint = '/test'
42
43     def 'Get request with authentication'() {
44         when: 'request is sent with authentication'
45             def response = mvc.perform(
46                     get(testEndpoint).header("Authorization", 'Basic Y3BzdXNlcjpjcHNyMGNrcyE=')
47             ).andReturn().response
48         then: 'HTTP OK status code is returned'
49             assert response.status == HttpStatus.OK.value()
50     }
51
52     def 'Get request without authentication is not authorized'() {
53         when: 'request is sent without authentication'
54             def response = mvc.perform(get(testEndpoint)).andReturn().response
55         then: 'HTTP Unauthorized status code is returned'
56             assert response.status == HttpStatus.UNAUTHORIZED.value()
57     }
58
59     def 'Get request with invalid authentication is not authorized'() {
60         when: 'request is sent with invalid authentication'
61             def response = mvc.perform(
62                     get(testEndpoint).header("Authorization", 'Basic invalid auth')
63             ).andReturn().response
64         then: 'HTTP Unauthorized status code is returned'
65             assert response.status == HttpStatus.UNAUTHORIZED.value()
66     }
67 }