aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/test/java/org/onap/portalng/bff/BaseIntegrationTest.java
blob: 1311ac76f1825f4b09ff4aad91e6987a3d3090dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 *
 * Copyright (c) 2022. Deutsche Telekom AG
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 *
 */

package org.onap.portalng.bff;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
import com.nimbusds.jose.jwk.JWKSet;
import io.restassured.RestAssured;
import io.restassured.filter.log.RequestLoggingFilter;
import io.restassured.filter.log.ResponseLoggingFilter;
import io.restassured.specification.RequestSpecification;
import java.net.URISyntaxException;
import java.time.Clock;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.apache.http.client.utils.URIBuilder;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.onap.portalng.bff.config.BffConfig;
import org.onap.portalng.bff.config.IdTokenExchangeFilterFunction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
import org.springframework.cloud.contract.wiremock.WireMockConfigurationCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;

/** Base class for all tests that has the common config including port, realm, logging and auth. */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWireMock(port = 0)
public abstract class BaseIntegrationTest {

  @TestConfiguration
  public static class Config {
    @Bean
    WireMockConfigurationCustomizer optionsCustomizer() {
      return options -> options.extensions(new ResponseTemplateTransformer(true));
    }
  }

  @LocalServerPort protected int port;

  @Value("${bff.realm}")
  protected String realm;

  @Autowired protected ObjectMapper objectMapper;
  @Autowired private TokenGenerator tokenGenerator;

  @Autowired protected BffConfig bffConfig;

  @BeforeAll
  public static void setup() {
    RestAssured.filters(new RequestLoggingFilter(), new ResponseLoggingFilter());
  }

  /** Mocks the OIDC auth flow. */
  @BeforeEach
  public void mockAuth() {
    WireMock.reset();

    WireMock.stubFor(
        WireMock.get(
                WireMock.urlMatching(
                    String.format("/realms/%s/protocol/openid-connect/certs", realm)))
            .willReturn(
                WireMock.aResponse()
                    .withHeader("Content-Type", JWKSet.MIME_TYPE)
                    .withBody(tokenGenerator.getJwkSet().toString())));

    final TokenGenerator.TokenGeneratorConfig config =
        TokenGenerator.TokenGeneratorConfig.builder().port(port).realm(realm).build();

    WireMock.stubFor(
        WireMock.post(
                WireMock.urlMatching(
                    String.format("/realms/%s/protocol/openid-connect/token", realm)))
            .withBasicAuth("test", "test")
            .withRequestBody(WireMock.containing("grant_type=client_credentials"))
            .willReturn(
                WireMock.aResponse()
                    .withHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
                    .withBody(
                        objectMapper
                            .createObjectNode()
                            .put("token_type", "bearer")
                            .put("access_token", tokenGenerator.generateToken(config))
                            .put("expires_in", config.getExpireIn().getSeconds())
                            .put("refresh_token", tokenGenerator.generateToken(config))
                            .put("refresh_expires_in", config.getExpireIn().getSeconds())
                            .put("not-before-policy", 0)
                            .put("session_state", UUID.randomUUID().toString())
                            .put("scope", "email profile")
                            .toString())));
  }

  /**
   * Object to store common attributes of requests that are going to be made. Adds an Identity
   * header for the <code>portal_admin</code> role to the request.
   */
  protected RequestSpecification requestSpecification() {
    final String idToken = tokenGenerator.generateToken(getTokenGeneratorConfig("portal_admin"));

    return unauthenticatedRequestSpecification()
        .auth()
        .preemptive()
        .oauth2(idToken)
        .header(IdTokenExchangeFilterFunction.X_AUTH_IDENTITY_HEADER, "Bearer " + idToken);
  }

  /**
   * Object to store common attributes of requests that are going to be made. Adds an Identity
   * header for the given role to the request.
   *
   * @param role the role used for RBAC
   * @return the templated request
   */
  protected RequestSpecification requestSpecification(String role) {
    final String idToken = tokenGenerator.generateToken(getTokenGeneratorConfig(role));

    return unauthenticatedRequestSpecification()
        .auth()
        .preemptive()
        .oauth2(idToken)
        .header(IdTokenExchangeFilterFunction.X_AUTH_IDENTITY_HEADER, "Bearer " + idToken);
  }

  /**
   * Object to store common attributes of requests that are going to be made. Adds an Identity
   * header for the given roles to the request.
   *
   * @param roles the roles used for RBAC
   * @return the templated request
   */
  protected RequestSpecification requestSpecification(List<String> roles) {
    final String idToken = tokenGenerator.generateToken(getTokenGeneratorConfig(roles));

    return unauthenticatedRequestSpecification()
        .auth()
        .preemptive()
        .oauth2(idToken)
        .header(IdTokenExchangeFilterFunction.X_AUTH_IDENTITY_HEADER, "Bearer " + idToken);
  }

  /** Get a RequestSpecification that does not have an Identity header. */
  protected RequestSpecification unauthenticatedRequestSpecification() {
    return RestAssured.given().port(port);
  }

  /**
   * Builds an OAuth2 configuration including the role, port and realm. This config can be used to
   * generate OAuth2 access tokens.
   *
   * @param role the role used for RBAC
   * @return the OAuth2 configuration
   */
  protected TokenGenerator.TokenGeneratorConfig getTokenGeneratorConfig(String role) {
    return TokenGenerator.TokenGeneratorConfig.builder()
        .port(port)
        .realm(realm)
        .roles(Collections.singletonList(role))
        .build();
  }

  /**
   * Builds an OAuth2 configuration including the roles, port and realm. This config can be used to
   * generate OAuth2 access tokens.
   *
   * @param roles the roles used for RBAC
   * @return the OAuth2 configuration
   */
  protected TokenGenerator.TokenGeneratorConfig getTokenGeneratorConfig(List<String> roles) {
    return TokenGenerator.TokenGeneratorConfig.builder()
        .port(port)
        .realm(realm)
        .roles(roles)
        .build();
  }

  public static OffsetDateTime offsetNow() {
    return OffsetDateTime.now(Clock.systemUTC());
  }

  public static String randomUUID() {
    return UUID.randomUUID().toString();
  }

  public static String adjustPath(
      String basePath, Optional<Integer> page, Optional<Integer> pageSize) {
    return adjustPath(basePath, page, pageSize, Optional.empty());
  }

  public static String adjustPath(
      String basePath,
      Optional<Integer> page,
      Optional<Integer> pageSize,
      Optional<String> filter) {
    URIBuilder builder;
    try {
      builder = new URIBuilder(basePath);
      if (page.isPresent()) {
        builder.addParameter("page", String.valueOf(page.get()));
      }
      if (pageSize.isPresent()) {
        builder.addParameter("pageSize", String.valueOf(pageSize.get()));
      }
      if (filter.isPresent()) {
        builder.addParameter("filter", filter.get());
      }
      return builder.build().toString();
    } catch (URISyntaxException e) {
      e.printStackTrace();
    }
    return basePath;
  }
}