Skip to content

Commit

Permalink
corrected issue #3
Browse files Browse the repository at this point in the history
  • Loading branch information
gauffininteractive committed Sep 28, 2016
1 parent e444cc8 commit 7c0102e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 11 deletions.
7 changes: 3 additions & 4 deletions src/Server/OneTrueError.Api.Client.Tests/TryTheClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ public class TryTheClient
[Fact]
public async Task Test()
{
OneTrueClient client = new OneTrueClient();
client.Credentials = new NetworkCredential("jonas", "123456");
client.Open(new Uri("http://localhost/onetrueerror/"));
OneTrueApiClient client = new OneTrueApiClient();
client.Open(new Uri("http://localhost/onetrueerror/"), "", "");
FindAccountByUserNameResult result = null;
try
{
result = await client.QueryAsync(new FindAccountByUserName("admin"));
}
catch (WebException ex)
catch (WebException ex)
{

}
Expand Down
27 changes: 27 additions & 0 deletions src/Server/OneTrueError.Api/Core/ApiKeys/Commands/CreateApiKey.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel.Design;
using DotNetCqs;

namespace OneTrueError.Api.Core.ApiKeys.Commands
Expand Down Expand Up @@ -29,6 +30,32 @@ public CreateApiKey(string applicationName, string apiKey, string sharedSecret,
ApplicationIds = applicationIds;
}

/// <summary>
/// Creates a new instance of <see cref="CreateApiKey"/>.
/// </summary>
/// <param name="applicationName"><see cref="ApplicationName"/></param>
/// <param name="apiKey"><see cref="ApiKey"/></param>
/// <param name="sharedSecret"><see cref="SharedSecret"/></param>
public CreateApiKey(string applicationName, string apiKey, string sharedSecret)
{
if (applicationName == null) throw new ArgumentNullException("applicationName");
if (apiKey == null) throw new ArgumentNullException("apiKey");
if (sharedSecret == null) throw new ArgumentNullException("sharedSecret");

ApplicationName = applicationName;
ApiKey = apiKey;
SharedSecret = sharedSecret;
ApplicationIds = new int[0];
}

/// <summary>
/// Serialization constructor
/// </summary>
protected CreateApiKey()
{

}


/// <summary>
/// Must always be the one that creates the key (will be assigned by the CommandBus per convention)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ public class ActivateAccountHandler : IRequestHandler<ActivateAccount, ActivateA
{
private readonly IEventBus _eventBus;
private readonly IAccountRepository _repository;
private IQueryBus _queryBus;
private readonly IQueryBus _queryBus;

/// <summary>
/// Creates a new instance of <see cref="ActivateAccountHandler" />.
/// </summary>
/// <param name="repository">repos</param>
/// <param name="eventBus">used to publish <see cref="AccountActivated" />.</param>
/// <param name="queryBus"> </param>
public ActivateAccountHandler(IAccountRepository repository, IEventBus eventBus, IQueryBus queryBus)
{
_repository = repository;
Expand Down Expand Up @@ -51,7 +53,7 @@ public async Task<ActivateAccountReply> ExecuteAsync(ActivateAccount request)
var query = new GetApplicationList();
var apps = await _queryBus.QueryAsync(query);
var roles = apps.Select(x => "Member_" + x.Id).ToArray();

Thread.CurrentPrincipal = new OneTruePrincipal(account.Id, account.UserName, roles);
var evt = new AccountActivated(account.Id, account.UserName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ public async Task should_be_able_to_Create_key()
generated.AllowedApplications.Should().BeEquivalentTo(new[] {_applicationId});
}

[Fact]
public async Task should_be_able_to_Create_a_key_without_applications_mapped()
{
var cmd = new CreateApiKey("Mofo", Guid.NewGuid().ToString("N"), Guid.NewGuid().ToString("N"));

var sut = new CreateApiKeyHandler(_uow);
await sut.ExecuteAsync(cmd);

var repos = new ApiKeyRepository(_uow);
var generated = await repos.GetByKeyAsync(cmd.ApiKey);
generated.Should().NotBeNull();
generated.AllowedApplications.Should().BeEmpty();
}

public void Dispose()
{
_uow.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public async Task ExecuteAsync(CreateApiKey command)
id = (int) await cmd.ExecuteScalarAsync();
}


foreach (var applicationId in command.ApplicationIds)
{
using (var cmd = _unitOfWork.CreateDbCommand())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public ApiKeysController(IQueryBus queryBus, ICommandBus commandBus)

public async Task<ActionResult> Index()
{
var query= new ListApiKeys();
var query = new ListApiKeys();
var result = await _queryBus.QueryAsync(query);
var vms = result.Keys.Select(x => new ListViewModelItem {Id = x.Id, Name = x.ApplicationName}).ToArray();
var model = new ListViewModel {Keys = vms};
var vms = result.Keys.Select(x => new ListViewModelItem { Id = x.Id, Name = x.ApplicationName }).ToArray();
var model = new ListViewModel { Keys = vms };
return View(model);
}

Expand Down Expand Up @@ -81,11 +81,13 @@ public async Task<ActionResult> Create(CreateViewModel model)

var apiKey = Guid.NewGuid().ToString("N");
var sharedSecret = Guid.NewGuid().ToString("N");
var apps = model.SelectedApplications.Select(int.Parse).ToArray();
var apps = model.SelectedApplications == null
? new int[0]
: model.SelectedApplications.Select(int.Parse).ToArray();
var cmd = new CreateApiKey(model.ApplicationName, apiKey, sharedSecret, apps);
await _commandBus.ExecuteAsync(cmd);

return RedirectToAction("Created", new {apiKey, sharedSecret});
return RedirectToAction("Created", new { apiKey, sharedSecret });
}

public ActionResult Created(string apiKey, string sharedSecret)
Expand Down

0 comments on commit 7c0102e

Please sign in to comment.