forked from PegaSysEng/pantheon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NC-2044] jsonrpc authentication cli options & acceptance tests (Pega…
- Loading branch information
Showing
19 changed files
with
438 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...t/java/tech/pegasys/pantheon/tests/acceptance/dsl/httptransaction/HttpRequestFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2019 ConsenSys 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. | ||
*/ | ||
package tech.pegasys.pantheon.tests.acceptance.dsl.httptransaction; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.IOException; | ||
|
||
import io.vertx.core.json.JsonObject; | ||
import okhttp3.MediaType; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.RequestBody; | ||
import okhttp3.Response; | ||
import okhttp3.ResponseBody; | ||
|
||
public class HttpRequestFactory { | ||
private final String uri; | ||
private final OkHttpClient client; | ||
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); | ||
|
||
public HttpRequestFactory(final String uri) { | ||
this.uri = uri; | ||
client = new OkHttpClient(); | ||
} | ||
|
||
public JsonObject loginSuccessful(final String username, final String password) | ||
throws IOException { | ||
final RequestBody requestBody = | ||
RequestBody.create( | ||
JSON, "{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}"); | ||
final Request request = new Request.Builder().post(requestBody).url(uri + "/login").build(); | ||
try (final Response response = client.newCall(request).execute()) { | ||
|
||
assertThat(response.code()).isEqualTo(200); | ||
assertThat(response.message()).isEqualTo("OK"); | ||
final ResponseBody responseBody = response.body(); | ||
assertThat(responseBody.contentType()).isNotNull(); | ||
assertThat(responseBody.contentType().type()).isEqualTo("application"); | ||
assertThat(responseBody.contentType().subtype()).isEqualTo("json"); | ||
final String bodyString = responseBody.string(); | ||
assertThat(bodyString).isNotNull(); | ||
assertThat(bodyString).isNotBlank(); | ||
|
||
return new JsonObject(bodyString); | ||
} | ||
} | ||
|
||
public void loginUnauthorized(final String username, final String password) throws IOException { | ||
final RequestBody requestBody = | ||
RequestBody.create( | ||
JSON, "{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}"); | ||
final Request request = new Request.Builder().post(requestBody).url(uri + "/login").build(); | ||
try (final Response response = client.newCall(request).execute()) { | ||
assertThat(response.code()).isEqualTo(401); | ||
assertThat(response.message()).isEqualTo("Unauthorized"); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...test/java/tech/pegasys/pantheon/tests/acceptance/dsl/httptransaction/HttpTransaction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright 2019 ConsenSys 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. | ||
*/ | ||
package tech.pegasys.pantheon.tests.acceptance.dsl.httptransaction; | ||
|
||
@FunctionalInterface | ||
public interface HttpTransaction<T> { | ||
|
||
T execute(final HttpRequestFactory httpFactory); | ||
} |
41 changes: 41 additions & 0 deletions
41
...est/java/tech/pegasys/pantheon/tests/acceptance/dsl/httptransaction/LoginTransaction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2019 ConsenSys 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. | ||
*/ | ||
package tech.pegasys.pantheon.tests.acceptance.dsl.httptransaction; | ||
|
||
import static org.junit.Assert.fail; | ||
|
||
import java.io.IOException; | ||
|
||
import io.vertx.core.json.JsonObject; | ||
|
||
public class LoginTransaction implements HttpTransaction<String> { | ||
private final String username; | ||
private final String password; | ||
|
||
public LoginTransaction(final String username, final String password) { | ||
this.username = username; | ||
this.password = password; | ||
} | ||
|
||
@Override | ||
public String execute(final HttpRequestFactory httpFactory) { | ||
try { | ||
final JsonObject response = httpFactory.loginSuccessful(username, password); | ||
final String token = response.getString("token"); | ||
return token; | ||
} catch (IOException e) { | ||
fail("Login request failed with exception: " + e.toString()); | ||
return null; | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...h/pegasys/pantheon/tests/acceptance/dsl/httptransaction/LoginUnauthorizedTransaction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2019 ConsenSys 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. | ||
*/ | ||
package tech.pegasys.pantheon.tests.acceptance.dsl.httptransaction; | ||
|
||
import static org.junit.Assert.fail; | ||
|
||
import java.io.IOException; | ||
|
||
public class LoginUnauthorizedTransaction implements HttpTransaction<Void> { | ||
private final String username; | ||
private final String password; | ||
|
||
public LoginUnauthorizedTransaction(final String username, final String password) { | ||
this.username = username; | ||
this.password = password; | ||
} | ||
|
||
@Override | ||
public Void execute(final HttpRequestFactory httpFactory) { | ||
try { | ||
httpFactory.loginUnauthorized(username, password); | ||
return null; | ||
} catch (IOException e) { | ||
fail("Login request failed with exception: " + e.toString()); | ||
return null; | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Login.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2019 ConsenSys 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. | ||
*/ | ||
package tech.pegasys.pantheon.tests.acceptance.dsl.jsonrpc; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition; | ||
import tech.pegasys.pantheon.tests.acceptance.dsl.httptransaction.LoginTransaction; | ||
import tech.pegasys.pantheon.tests.acceptance.dsl.httptransaction.LoginUnauthorizedTransaction; | ||
|
||
public class Login { | ||
|
||
public Condition loginSucceeds(final String username, final String password) { | ||
return (n) -> { | ||
final String token = n.executeHttpTransaction(new LoginTransaction(username, password)); | ||
assertThat(token).isNotBlank(); | ||
}; | ||
} | ||
|
||
public Condition loginFails(final String username, final String password) { | ||
return (n) -> { | ||
n.executeHttpTransaction(new LoginUnauthorizedTransaction(username, password)); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.