Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api keys interceptor example test updates #1423

Merged
merged 11 commits into from
Dec 18, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
// Security
JsonProtectionTest.class,
APIKeyTest.class,
APIKeyRBACTest.class,
APIKeyWithOpenAPITest.class,
XMLTemplateTest.class,
//DefaultConfigAdminConsoleTest.class*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Copyright 2024 predic8 GmbH, www.predic8.com

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. */
package com.predic8.membrane.examples.tests;

import com.predic8.membrane.examples.util.AbstractSampleMembraneStartStopTestcase;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.equalTo;

public class APIKeyRBACTest extends AbstractSampleMembraneStartStopTestcase {

@Override
protected String getExampleDirName() {
return "security/api-key/rbac";
}

@Test
public void normalScope() {
given()
.header("X-Key", "123456789")
.when()
.get("http://localhost:3000")
.then().assertThat()
.statusCode(200)
.body(equalTo("Only for finance or accounting!"));
}

@Test
public void conditionalScope() {
given()
.header("X-Key", "key_321_abc")
.when()
.get("http://localhost:3000")
.then().assertThat()
.statusCode(200)
.body(equalTo("Only for admins!"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void notAuthorized() {
@Test
public void successKeyHeader() {
given()
.header("X-Api-Key", "P8MBR")
.header("X-Api-Key", "demokey")
.when()
.get("http://localhost:2000")
.then().assertThat()
Expand All @@ -58,32 +58,10 @@ public void successKeyHeader() {
@Test
public void successQueryKey() {
given()
.queryParam("api-key", "P8MBR")
.queryParam("api-key", "demokey")
.when()
.get("http://localhost:2000")
.then().assertThat()
.statusCode(200);
}

@Test
public void normalScope() {
given()
.header("X-Key", "123456789")
.when()
.get("http://localhost:3000")
.then().assertThat()
.statusCode(200)
.body(equalTo("Only for finance or accounting!"));
}

@Test
public void conditionalScope() {
given()
.header("X-Key", "key_321_abc")
.when()
.get("http://localhost:3000")
.then().assertThat()
.statusCode(200)
.body(equalTo("Only for admins!"));
}
}
Loading