Skip to content

Commit

Permalink
RDBQA-45 introduced Admin and User in FactIfWindowsAuthenticationIsAv…
Browse files Browse the repository at this point in the history
…ailable
  • Loading branch information
ppekrol committed Nov 19, 2015
1 parent 9a43b01 commit ab56453
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 48 deletions.
@@ -1,53 +1,47 @@
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using Xunit;

namespace Raven.Tests.Common.Attributes
{
public static class FactIfWindowsAuthenticationIsAvailable
{
private static bool triedLoading = false;
private readonly static SkipException SkipException = new SkipException("Cannot execute this test, because this test rely on actual Windows Account name / password.");

public static string Username { get; private set; }
public static string Domain { get; private set; }
public static string Password { get; private set; }
private static bool triedLoading;

public static string InvalidUsername { get; private set; }
public static string InvalidPassword { get; private set; }
public static string InvalidDomain { get; private set; }
public static NetworkCredential Admin { get; private set; }

public static NetworkCredential User { get; private set; }

public static void LoadCredentials()
{
if (Username != null)
if (Admin != null)
return;

var skipException = new SkipException("Cannot execute this test, because this test rely on actual Windows Account name / password.");
if (triedLoading)
throw skipException;
throw SkipException;

lock (typeof (FactIfWindowsAuthenticationIsAvailable))
{
triedLoading = true;
ActualLoad();
if (Username == null)
throw skipException;
if (Admin == null || string.IsNullOrEmpty(Admin.UserName))
throw SkipException;
}
}

[Conditional("DEBUG")]
private static void LoadDebugCredentials()
{
Username = "local_user_test";
Password = "local_user_test";
Domain = "local_machine_name_test";
InvalidUsername = "local_machine_invalid_user";
InvalidPassword = "local_machine_invalid_user";
InvalidDomain = "local_machine_name_test";
Admin = new NetworkCredential("local_user_test", "local_user_test", "local_machine_name_test");
User = new NetworkCredential("local_machine_invalid_user", "local_machine_invalid_user", "local_machine_name_test");

if (Username == "local_user_test")
if (Admin.UserName == "local_user_test")
{
Username = null;
Admin = null;
}
}

Expand All @@ -63,30 +57,32 @@ private static void ActualLoad()
return;
}

Admin = new NetworkCredential();
var lines = File.ReadAllLines(path, Encoding.UTF8);
var username = lines[0].Split('\\');
if (username.Length > 1)
{
Domain = username[0];
Username = username[1];
Admin.Domain = username[0];
Admin.UserName = username[1];
}
else
{
Username = username[0];
Admin.UserName = username[0];
}
Password = lines[1];
Admin.Password = lines[1];

User = new NetworkCredential();
username = lines[2].Split('\\');
if (username.Length > 1)
{
InvalidDomain = username[0];
InvalidUsername = username[1];
User.Domain = username[0];
User.UserName = username[1];
}
else
{
InvalidUsername = username[0];
User.UserName = username[0];
}
InvalidPassword = lines[3];
User.Password = lines[3];
}
}
}
6 changes: 3 additions & 3 deletions Raven.Tests.Core/Auth/Authentication.cs
Expand Up @@ -71,7 +71,7 @@ public void CanUseWindowsAuthentication()
{
new WindowsAuthData
{
Name = string.Format("{0}\\{1}", FactIfWindowsAuthenticationIsAvailable.Domain, FactIfWindowsAuthenticationIsAvailable.Username),
Name = string.Format("{0}\\{1}", FactIfWindowsAuthenticationIsAvailable.Admin.Domain, FactIfWindowsAuthenticationIsAvailable.Admin.UserName),
Enabled = true,
Databases = new List<ResourceAccess>
{
Expand All @@ -84,7 +84,7 @@ public void CanUseWindowsAuthentication()

using (var store = new DocumentStore
{
Credentials = new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.InvalidUsername, FactIfWindowsAuthenticationIsAvailable.InvalidPassword, FactIfWindowsAuthenticationIsAvailable.InvalidDomain),
Credentials = new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.User.UserName, FactIfWindowsAuthenticationIsAvailable.User.Password, FactIfWindowsAuthenticationIsAvailable.User.Domain),
Url = this.Server.SystemDatabase.ServerUrl
}.Initialize())
{
Expand All @@ -93,7 +93,7 @@ public void CanUseWindowsAuthentication()

using (var store = new DocumentStore
{
Credentials = new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Username, FactIfWindowsAuthenticationIsAvailable.Password, FactIfWindowsAuthenticationIsAvailable.Domain),
Credentials = new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Admin.UserName, FactIfWindowsAuthenticationIsAvailable.Admin.Password, FactIfWindowsAuthenticationIsAvailable.Admin.Domain),
Url = this.Server.SystemDatabase.ServerUrl
}.Initialize())
{
Expand Down
16 changes: 8 additions & 8 deletions Raven.Tests.FileSystem/Auth/ClientWindowsAuth.cs
Expand Up @@ -44,7 +44,7 @@ protected override void ConfigureServer(RavenDbServer server, string fileSystemN
{
new WindowsAuthData()
{
Name = string.Format("{0}\\{1}", FactIfWindowsAuthenticationIsAvailable.Domain, FactIfWindowsAuthenticationIsAvailable.Username),
Name = string.Format("{0}\\{1}", FactIfWindowsAuthenticationIsAvailable.Admin.Domain, FactIfWindowsAuthenticationIsAvailable.Admin.UserName),
Enabled = true,
Databases = new List<ResourceAccess>
{
Expand All @@ -59,7 +59,7 @@ protected override void ConfigureServer(RavenDbServer server, string fileSystemN
[Fact]
public async Task CanWorkWithWinAuthEnabled()
{
var client = NewAsyncClient(enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Username, FactIfWindowsAuthenticationIsAvailable.Password, FactIfWindowsAuthenticationIsAvailable.Domain));
var client = NewAsyncClient(enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Admin.UserName, FactIfWindowsAuthenticationIsAvailable.Admin.Password, FactIfWindowsAuthenticationIsAvailable.Admin.Domain));

var ms = new MemoryStream(new byte[1024 * 1024 * 10]);

Expand Down Expand Up @@ -110,12 +110,12 @@ public async Task CanWorkWithWinAuthEnabled()
[Fact]
public async Task AdminClientWorkWithWinAuthEnabled()
{
var client = (IAsyncFilesCommandsImpl)NewAsyncClient(enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Username, FactIfWindowsAuthenticationIsAvailable.Password, FactIfWindowsAuthenticationIsAvailable.Domain));
var client = (IAsyncFilesCommandsImpl)NewAsyncClient(enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Admin.UserName, FactIfWindowsAuthenticationIsAvailable.Admin.Password, FactIfWindowsAuthenticationIsAvailable.Admin.Domain));
var adminClient = client.Admin;

await adminClient.CreateFileSystemAsync(MultiDatabase.CreateFileSystemDocument("testName"), "testName");

using (var createdFsClient = new AsyncFilesServerClient(client.ServerUrl, "testName", new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Username, FactIfWindowsAuthenticationIsAvailable.Password, FactIfWindowsAuthenticationIsAvailable.Domain)))
using (var createdFsClient = new AsyncFilesServerClient(client.ServerUrl, "testName", new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Admin.UserName, FactIfWindowsAuthenticationIsAvailable.Admin.Password, FactIfWindowsAuthenticationIsAvailable.Admin.Domain)))
{
await createdFsClient.UploadAsync("foo", new MemoryStream(new byte[] {1}));
}
Expand All @@ -138,7 +138,7 @@ public async Task AdminClientWorkWithWinAuthEnabled()
[Fact]
public async Task ConfigClientCanWorkWithWinAuthEnabled()
{
var configClient = NewAsyncClient(enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Username, FactIfWindowsAuthenticationIsAvailable.Password, FactIfWindowsAuthenticationIsAvailable.Domain)).Configuration;
var configClient = NewAsyncClient(enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Admin.UserName, FactIfWindowsAuthenticationIsAvailable.Admin.Password, FactIfWindowsAuthenticationIsAvailable.Admin.Domain)).Configuration;

await configClient.SetKeyAsync("test-conf", new RavenJObject() { { "key", "value" } });

Expand All @@ -158,7 +158,7 @@ public async Task ConfigClientCanWorkWithWinAuthEnabled()
[Fact]
public async Task StorageClientCanWorkWithWinAuthEnabled()
{
var storageClient = NewAsyncClient(enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Username, FactIfWindowsAuthenticationIsAvailable.Password, FactIfWindowsAuthenticationIsAvailable.Domain)).Storage;
var storageClient = NewAsyncClient(enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Admin.UserName, FactIfWindowsAuthenticationIsAvailable.Admin.Password, FactIfWindowsAuthenticationIsAvailable.Admin.Domain)).Storage;

await storageClient.RetryRenamingAsync();

Expand All @@ -171,13 +171,13 @@ public async Task ShouldThrowWhenWindowsDocumentDoesNotContainFileSystem()
// in this test be careful if the specified credentials belong to admin user or not
// to make this test pass you need to specify the credentials of a user who isn't an admin on this machine

var client = NewAsyncClient(enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Username, FactIfWindowsAuthenticationIsAvailable.Password, FactIfWindowsAuthenticationIsAvailable.Domain));
var client = NewAsyncClient(enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Admin.UserName, FactIfWindowsAuthenticationIsAvailable.Admin.Password, FactIfWindowsAuthenticationIsAvailable.Admin.Domain));
var server = GetServer();

await client.UploadAsync("abc.bin", new RandomStream(3));

using (var anotherClient = new AsyncFilesServerClient(GetServerUrl(false, server.SystemDatabase.ServerUrl), "ShouldThrow_WindowsDocumentDoesNotContainsThisFS",
credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Username, FactIfWindowsAuthenticationIsAvailable.Password, FactIfWindowsAuthenticationIsAvailable.Domain)))
credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Admin.UserName, FactIfWindowsAuthenticationIsAvailable.Admin.Password, FactIfWindowsAuthenticationIsAvailable.Admin.Domain)))
{
await anotherClient.EnsureFileSystemExistsAsync(); // will pass because by using this api key we have access to <system> database

Expand Down
10 changes: 5 additions & 5 deletions Raven.Tests.FileSystem/Auth/SynchronizationWithWindowsAuth.cs
Expand Up @@ -37,7 +37,7 @@ protected override void ConfigureServer(RavenDbServer server, string fileSystemN
{
new WindowsAuthData()
{
Name = string.Format("{0}\\{1}", FactIfWindowsAuthenticationIsAvailable.Domain, FactIfWindowsAuthenticationIsAvailable.Username),
Name = string.Format("{0}\\{1}", FactIfWindowsAuthenticationIsAvailable.Admin.Domain, FactIfWindowsAuthenticationIsAvailable.Admin.UserName),
Enabled = true,
Databases = new List<ResourceAccess>
{
Expand All @@ -54,7 +54,7 @@ protected override void ConfigureServer(RavenDbServer server, string fileSystemN
public async Task CanSynchronizeFileContent()
{
var source = NewAsyncClient(0);
var destination = NewAsyncClient(1, enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Username, FactIfWindowsAuthenticationIsAvailable.Password, FactIfWindowsAuthenticationIsAvailable.Domain));
var destination = NewAsyncClient(1, enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Admin.UserName, FactIfWindowsAuthenticationIsAvailable.Admin.Password, FactIfWindowsAuthenticationIsAvailable.Admin.Domain));

var ms = new MemoryStream(new byte[] { 3, 2, 1 });

Expand All @@ -72,7 +72,7 @@ public async Task CanSynchronizeMetadata()
var content = new MemoryStream(new byte[] { 1, 2, 3, 4 });

var sourceClient = NewAsyncClient(0);
var destinationClient = NewAsyncClient(1, enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Username, FactIfWindowsAuthenticationIsAvailable.Password, FactIfWindowsAuthenticationIsAvailable.Domain));
var destinationClient = NewAsyncClient(1, enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Admin.UserName, FactIfWindowsAuthenticationIsAvailable.Admin.Password, FactIfWindowsAuthenticationIsAvailable.Admin.Domain));

await sourceClient.UploadAsync("test.bin", content, new RavenJObject { { "difference", "metadata" } });
content.Position = 0;
Expand All @@ -94,7 +94,7 @@ public async Task CanSynchronizeFileRename()
var content = new MemoryStream(new byte[] { 1, 2, 3, 4 });

var sourceClient = NewAsyncClient(0);
var destinationClient = NewAsyncClient(1, enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Username, FactIfWindowsAuthenticationIsAvailable.Password, FactIfWindowsAuthenticationIsAvailable.Domain));
var destinationClient = NewAsyncClient(1, enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Admin.UserName, FactIfWindowsAuthenticationIsAvailable.Admin.Password, FactIfWindowsAuthenticationIsAvailable.Admin.Domain));

await sourceClient.UploadAsync("test.bin", content);
content.Position = 0;
Expand All @@ -119,7 +119,7 @@ public async Task CanSynchronizeFileRename()
public async Task CanSynchronizeFileDelete()
{
var source = NewAsyncClient(0);
var destination = NewAsyncClient(1, enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Username, FactIfWindowsAuthenticationIsAvailable.Password, FactIfWindowsAuthenticationIsAvailable.Domain));
var destination = NewAsyncClient(1, enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Admin.UserName, FactIfWindowsAuthenticationIsAvailable.Admin.Password, FactIfWindowsAuthenticationIsAvailable.Admin.Domain));

await source.UploadAsync("test.bin", new RandomStream(1));

Expand Down
8 changes: 4 additions & 4 deletions Raven.Tests/Security/ReplicationWithMixedSecurity.cs
Expand Up @@ -45,7 +45,7 @@ protected override void ModifyStore(DocumentStore store)
}
else
{
store.Credentials = new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Username, FactIfWindowsAuthenticationIsAvailable.Password, FactIfWindowsAuthenticationIsAvailable.Domain);
store.Credentials = new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Admin.UserName, FactIfWindowsAuthenticationIsAvailable.Admin.Password, FactIfWindowsAuthenticationIsAvailable.Admin.Domain);
store.ApiKey = null;
}

Expand Down Expand Up @@ -87,7 +87,7 @@ protected override void ConfigureDatabase(Database.DocumentDatabase database, st
{
new WindowsAuthData()
{
Name = FactIfWindowsAuthenticationIsAvailable.Username,
Name = FactIfWindowsAuthenticationIsAvailable.Admin.UserName,
Enabled = true,
Databases = new List<ResourceAccess>
{
Expand All @@ -110,7 +110,7 @@ public void DocumentStoreShouldSwitchFromApiKeyToCredentials()
Authentication.EnableOnce();
var store2 = CreateStore(enableAuthorization: true, anonymousUserAccessMode: AnonymousUserAccessMode.None);

TellFirstInstanceToReplicateToSecondInstance(username: FactIfWindowsAuthenticationIsAvailable.Username, password: FactIfWindowsAuthenticationIsAvailable.Password, domain: FactIfWindowsAuthenticationIsAvailable.Domain);
TellFirstInstanceToReplicateToSecondInstance(username: FactIfWindowsAuthenticationIsAvailable.Admin.UserName, password: FactIfWindowsAuthenticationIsAvailable.Admin.Password, domain: FactIfWindowsAuthenticationIsAvailable.Admin.Domain);

using (var session = store1.OpenSession())
{
Expand Down Expand Up @@ -139,7 +139,7 @@ public async Task DocumentStoreShouldSwitchFromApiKeyToCredentialsAsync()
Authentication.EnableOnce();
var store2 = CreateStore(enableAuthorization: true, anonymousUserAccessMode: AnonymousUserAccessMode.None);

TellFirstInstanceToReplicateToSecondInstance(username: FactIfWindowsAuthenticationIsAvailable.Username, password: FactIfWindowsAuthenticationIsAvailable.Password, domain: FactIfWindowsAuthenticationIsAvailable.Domain);
TellFirstInstanceToReplicateToSecondInstance(username: FactIfWindowsAuthenticationIsAvailable.Admin.UserName, password: FactIfWindowsAuthenticationIsAvailable.Admin.Password, domain: FactIfWindowsAuthenticationIsAvailable.Admin.Domain);

using (var session = store1.OpenAsyncSession())
{
Expand Down

0 comments on commit ab56453

Please sign in to comment.