|
| 1 | +/* |
| 2 | + * Copyright 2021 LINE Corporation |
| 3 | + * |
| 4 | + * LINE Corporation licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.linecorp.centraldogma.it; |
| 18 | + |
| 19 | +import static com.linecorp.centraldogma.testing.internal.auth.TestAuthMessageUtil.login; |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 24 | + |
| 25 | +import com.linecorp.armeria.client.WebClient; |
| 26 | +import com.linecorp.armeria.common.AggregatedHttpResponse; |
| 27 | +import com.linecorp.armeria.common.HttpRequest; |
| 28 | +import com.linecorp.armeria.common.HttpStatus; |
| 29 | +import com.linecorp.armeria.common.MediaType; |
| 30 | +import com.linecorp.armeria.common.auth.OAuth2Token; |
| 31 | +import com.linecorp.centraldogma.internal.Jackson; |
| 32 | +import com.linecorp.centraldogma.internal.api.v1.AccessToken; |
| 33 | +import com.linecorp.centraldogma.server.CentralDogmaBuilder; |
| 34 | +import com.linecorp.centraldogma.testing.internal.auth.TestAuthMessageUtil; |
| 35 | +import com.linecorp.centraldogma.testing.internal.auth.TestAuthProviderFactory; |
| 36 | +import com.linecorp.centraldogma.testing.junit.CentralDogmaExtension; |
| 37 | + |
| 38 | +class NonRandomTokenTest { |
| 39 | + |
| 40 | + @RegisterExtension |
| 41 | + static final CentralDogmaExtension dogma = new CentralDogmaExtension() { |
| 42 | + @Override |
| 43 | + protected void configure(CentralDogmaBuilder builder) { |
| 44 | + builder.administrators(TestAuthMessageUtil.USERNAME); |
| 45 | + builder.authProviderFactory(new TestAuthProviderFactory()); |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + @Test |
| 50 | + void createNonRandomToken() throws Exception { |
| 51 | + final WebClient client = dogma.httpClient(); |
| 52 | + final AggregatedHttpResponse response = login(client, |
| 53 | + TestAuthMessageUtil.USERNAME, |
| 54 | + TestAuthMessageUtil.PASSWORD); |
| 55 | + |
| 56 | + assertThat(response.status()).isEqualTo(HttpStatus.OK); |
| 57 | + final String sessionId = Jackson.readValue(response.content().array(), AccessToken.class) |
| 58 | + .accessToken(); |
| 59 | + final WebClient adminClient = WebClient.builder(client.uri()) |
| 60 | + .auth(OAuth2Token.of(sessionId)).build(); |
| 61 | + |
| 62 | + final HttpRequest request = HttpRequest.builder() |
| 63 | + .post("/api/v1/tokens") |
| 64 | + .content(MediaType.FORM_DATA, |
| 65 | + "secret=appToken-secret&isAdmin=true&appId=foo") |
| 66 | + .build(); |
| 67 | + AggregatedHttpResponse res = adminClient.execute(request).aggregate().join(); |
| 68 | + assertThat(res.status()).isEqualTo(HttpStatus.CREATED); |
| 69 | + res = adminClient.get("/api/v1/tokens").aggregate().join(); |
| 70 | + assertThat(res.contentUtf8()).contains("\"secret\":\"appToken-secret\""); |
| 71 | + } |
| 72 | +} |
0 commit comments