Skip to content
This repository has been archived by the owner on Sep 20, 2019. It is now read-only.

Commit

Permalink
Add integration tests for authentication
Browse files Browse the repository at this point in the history
This also changes the exception thrown for missing credentials when authentication is required to InvalidOperationException.
  • Loading branch information
FlorianHockmann committed May 21, 2017
1 parent f324f7c commit 29d783c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Start-Gremlin-Server.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ Set-Location $gremlinServerDirectory
Start-Process $gremlinServerStartFile
Write-Output "Gremlin Server should now be running"

$gremlinServerSecureConf = "conf/gremlin-server-secure.yaml"
(Get-Content $gremlinServerSecureConf).Replace("8182", "8183").Replace("enabled: true}","enabled: false}") | Set-Content $gremlinServerSecureConf
Write-Output "Starting Secure Gremlin Server on Port 8183"
Start-Process $gremlinServerStartFile $gremlinServerSecureConf
Write-Output "Secure Gremlin Server should now be running"

Set-Location $currentDirectory
3 changes: 2 additions & 1 deletion src/Gremlin.Net/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ private async Task<IEnumerable<T>> ReceiveAsync<T>()
if (status.Code == ResponseStatusCode.Authenticate)
{
if (string.IsNullOrEmpty(_username) || string.IsNullOrEmpty(_password))
throw new ResponseException($"{status.Code}: {status.Message}");
throw new InvalidOperationException(
$"The Gremlin Server requires authentication, but no credentials are specified - username: {_username}, password: {_password}.");

var message = new AuthenticationRequestMessage
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Threading.Tasks;
using Gremlin.Net.Exceptions;
using Gremlin.Net.IntegrationTest.Util;
using Xunit;

namespace Gremlin.Net.IntegrationTest
{
public class GremlinClientAuthenticationTests
{
private static readonly string TestHost = ConfigProvider.Configuration["TestServerIpAddress"];
private static readonly int TestPort = Convert.ToInt32(ConfigProvider.Configuration["TestSecureServerPort"]);
private readonly ScriptRequestMessageProvider _requestMessageProvider = new ScriptRequestMessageProvider();

[Fact]
public async Task ShouldThrowForMissingCredentials()
{
var gremlinServer = new GremlinServer(TestHost, TestPort);
using (var gremlinClient = new GremlinClient(gremlinServer))
{
var exception = await Assert.ThrowsAsync<InvalidOperationException>(
async () => await gremlinClient.SubmitWithSingleResultAsync<string>(_requestMessageProvider
.GetDummyMessage()));

Assert.Contains("authentication", exception.Message);
Assert.Contains("credentials", exception.Message);
}
}

[Theory]
[InlineData("unknownUser", "passwordDoesntMatter")]
[InlineData("stephen", "wrongPassword")]
public async Task ShouldThrowForWrongCredentials(string username, string password)
{
var gremlinServer = new GremlinServer(TestHost, TestPort, Username: username, Password: password);
using (var gremlinClient = new GremlinClient(gremlinServer))
{
var exception = await Assert.ThrowsAsync<ResponseException>(
async () => await gremlinClient.SubmitWithSingleResultAsync<string>(_requestMessageProvider
.GetDummyMessage()));

Assert.Contains("Unauthorized", exception.Message);
}
}

[Theory]
[InlineData("1+1", "2")]
[InlineData("'Hello' + 'World'", "HelloWorld")]
public async Task ScriptShouldBeEvaluatedAndResultReturnedForCorrectCredentials(string requestMsg,
string expectedResponse)
{
const string username = "stephen";
const string password = "password";
var gremlinServer = new GremlinServer(TestHost, TestPort, Username: username, Password: password);
using (var gremlinClient = new GremlinClient(gremlinServer))
{
var response = await gremlinClient.SubmitWithSingleResultAsync<string>(requestMsg);

Assert.Equal(expectedResponse, response);
}
}
}
}
3 changes: 2 additions & 1 deletion test/Gremlin.Net.IntegrationTest/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"TestServerIpAddress": "localhost",
"TestServerPort": 8182
"TestServerPort": 8182,
"TestSecureServerPort": 8183
}

0 comments on commit 29d783c

Please sign in to comment.