2  *  ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2024 Nordix Foundation
 
   4  *  ================================================================================
 
   5  *  Licensed under the Apache License, Version 2.0 (the 'License');
 
   6  *  you may not use this file except in compliance with the License.
 
   7  *  You may obtain a copy of the License at
 
   9  *        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.integration.functional.ncmp
 
  23 import org.onap.cps.integration.base.CpsIntegrationSpecBase
 
  24 import org.springframework.http.HttpHeaders
 
  25 import org.springframework.http.MediaType
 
  26 import spock.util.concurrent.PollingConditions
 
  28 import static org.springframework.http.HttpMethod.DELETE
 
  29 import static org.springframework.http.HttpMethod.GET
 
  30 import static org.springframework.http.HttpMethod.PATCH
 
  31 import static org.springframework.http.HttpMethod.POST
 
  32 import static org.springframework.http.HttpMethod.PUT
 
  33 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.request
 
  34 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
 
  36 class BearerTokenPassthroughSpec extends CpsIntegrationSpecBase {
 
  39         dmiDispatcher1.moduleNamesPerCmHandleId['ch-1'] = ['M1', 'M2']
 
  40         registerCmHandle(DMI1_URL, 'ch-1', NO_MODULE_SET_TAG)
 
  44         deregisterCmHandle(DMI1_URL, 'ch-1')
 
  47     def 'Bearer token is passed from NCMP to DMI in pass-through data operations.'() {
 
  48         when: 'a pass-through data request is sent to NCMP with a bearer token'
 
  49             mvc.perform(request(httpMethod, '/ncmp/v1/ch/ch-1/data/ds/ncmp-datastore:passthrough-running')
 
  50                     .queryParam('resourceIdentifier', 'my-resource-id')
 
  51                     .contentType(MediaType.APPLICATION_JSON)
 
  52                     .content('{ "some-json": "data" }')
 
  53                     .header(HttpHeaders.AUTHORIZATION, 'Bearer some-bearer-token'))
 
  54                     .andExpect(status().is2xxSuccessful())
 
  56         then: 'DMI has received request with bearer token'
 
  57             assert dmiDispatcher1.lastAuthHeaderReceived == 'Bearer some-bearer-token'
 
  59         where: 'all HTTP operations are applied'
 
  60             httpMethod << [GET, POST, PUT, PATCH, DELETE]
 
  63     def 'Basic auth header is NOT passed from NCMP to DMI in pass-through data operations.'() {
 
  64         when: 'a pass-through data request is sent to NCMP with basic authentication'
 
  65             mvc.perform(request(httpMethod, '/ncmp/v1/ch/ch-1/data/ds/ncmp-datastore:passthrough-running')
 
  66                     .queryParam('resourceIdentifier', 'my-resource-id')
 
  67                     .contentType(MediaType.APPLICATION_JSON)
 
  68                     .content('{ "some-json": "data" }')
 
  69                     .header(HttpHeaders.AUTHORIZATION, 'Basic Y3BzdXNlcjpjcHNyMGNrcyE='))
 
  70                     .andExpect(status().is2xxSuccessful())
 
  72         then: 'DMI has received request with no authorization header'
 
  73             assert dmiDispatcher1.lastAuthHeaderReceived == null
 
  75         where: 'all HTTP operations are applied'
 
  76             httpMethod << [GET, POST, PUT, PATCH, DELETE]
 
  79     def 'Bearer token is passed from NCMP to DMI in async batch pass-through data operation.'() {
 
  80         when: 'a pass-through async data request is sent to NCMP with a bearer token'
 
  81             def requestBody = """{"operations": [{
 
  83                 "operationId": "operational-1",
 
  84                 "datastore": "ncmp-datastore:passthrough-running",
 
  85                 "resourceIdentifier": "my-resource-id",
 
  88         mvc.perform(request(POST, '/ncmp/v1/data')
 
  89                     .queryParam('topic', 'my-topic')
 
  90                     .contentType(MediaType.APPLICATION_JSON)
 
  92                     .header(HttpHeaders.AUTHORIZATION, 'Bearer some-bearer-token'))
 
  93                     .andExpect(status().is2xxSuccessful())
 
  95         then: 'DMI will receive the async request with bearer token'
 
  96             new PollingConditions().within(3, () -> {
 
  97                 assert dmiDispatcher1.lastAuthHeaderReceived == 'Bearer some-bearer-token'