Skip to content

Commit

Permalink
Change phones and register route (#170)
Browse files Browse the repository at this point in the history
* change phoneNumber to phones and change register route to profiles/register

* Change phone types to strings instead of codes.

* change phone type
  • Loading branch information
Jonathan Raymer authored and Tim Michalski committed May 12, 2018
1 parent 2cb7a19 commit 2f4e983
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,35 @@
using System.Web.Http;
using System.Web.Http.Routing;
using FluentAssertions;
using Microsoft.Owin.Security;
using Moq;
using Ploeh.AutoFixture;
using Voyage.Api.API.V1;
using Voyage.Api.UserManager;
using Voyage.Api.UserManager.API.V1;
using Voyage.Api.UnitTests.Common;
using Voyage.Api.UnitTests.Common.AutoMapperFixture;
using Voyage.Core.Exceptions;
using Voyage.Models;
using Voyage.Services.Profile;
using Voyage.Services.User;
using Xunit;

namespace Voyage.Api.UnitTests.API.V1
{
public class AccountControllerTests : BaseUnitTest
public class ProfilesControllerTests : BaseUnitTest
{
private readonly AccountController _accountController;
private readonly ProfilesController _profilesController;
private readonly Mock<IUserService> _mockUserService;
private readonly Mock<IProfileService> _mockProfileService;
private readonly Mock<IAuthenticationManager> _mockAuthenticationManager;
private readonly Mock<UrlHelper> _mockUrlHelper;

public AccountControllerTests()
public ProfilesControllerTests()
{

_mockUserService = Mock.Create<IUserService>();
_mockUrlHelper = Mock.Create<UrlHelper>();
_mockProfileService = Mock.Create<IProfileService>();
_accountController = new AccountController(_mockUserService.Object, _mockProfileService.Object)
_mockAuthenticationManager = Mock.Create<IAuthenticationManager>();
_profilesController = new ProfilesController(_mockAuthenticationManager.Object, _mockProfileService.Object, _mockUserService.Object)
{
Request = new HttpRequestMessage(),
Configuration = new HttpConfiguration(),
Expand All @@ -60,7 +62,7 @@ public async Task Register_Should_Call_UserManager()
.Returns(Task.FromResult(0));

// ACT
var result = await _accountController.Register(model);
var result = await _profilesController.Register(model);

var message = await result.ExecuteAsync(new CancellationToken());

Expand All @@ -70,7 +72,7 @@ public async Task Register_Should_Call_UserManager()
[Fact]
public void Ctor_Should_Throw_ArgumentNullException_When_UserService_IsNull()
{
Action throwAction = () => new AccountController(null, null);
Action throwAction = () => new ProfilesController(_mockAuthenticationManager.Object, _mockProfileService.Object, null);

throwAction.ShouldThrow<ArgumentNullException>()
.And
Expand All @@ -82,7 +84,7 @@ public void Ctor_Should_Throw_ArgumentNullException_When_UserService_IsNull()
[Fact]
public void Class_Should_Have_RoutePrefix_Attribute()
{
typeof(AccountController).Should()
typeof(ProfilesController).Should()
.BeDecoratedWith<RoutePrefixAttribute>(
_ => _.Prefix.Equals(RoutePrefixConstants.RoutePrefixes.V1));
}
Expand All @@ -91,10 +93,10 @@ public void Class_Should_Have_RoutePrefix_Attribute()
public void Register_Should_Have_Route_Attribute()
{
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
ReflectionHelper.GetMethod<AccountController>(_ => _.Register(new RegistrationModel()))
ReflectionHelper.GetMethod<ProfilesController>(_ => _.Register(new RegistrationModel()))
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
.Should()
.BeDecoratedWith<RouteAttribute>(_ => _.Template.Equals("accounts"));
.BeDecoratedWith<RouteAttribute>(_ => _.Template.Equals("profiles/register"));
}
}
}
13 changes: 0 additions & 13 deletions Voyage.Api.UnitTests/API/V1/UserControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Web.Http.Routing;
using FluentAssertions;
using Microsoft.AspNet.Identity;
using Microsoft.Owin.Security;
using Moq;
using Ploeh.AutoFixture;
using Voyage.Api.UserManager;
Expand Down Expand Up @@ -401,18 +400,6 @@ public void AssignRole_Should_Be_Decorated_With_RouteAttribute()
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
}

[Fact]
public void Ctor_Should_Throw_ArgumentNullException_When_UserService_IsNull()
{
Action throwAction = () => new AccountController(null, null);

throwAction.ShouldThrow<ArgumentNullException>()
.And
.ParamName
.Should()
.Be("userService");
}

[Fact]
public void Class_Should_Have_RoutePrefix_Attribute()
{
Expand Down
2 changes: 1 addition & 1 deletion Voyage.Api.UnitTests/Voyage.Api.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="API\V1\AccountControllerTests.cs" />
<Compile Include="API\V1\ProfilesControllerTests.cs" />
<Compile Include="API\V1\AdminControllerTests.cs" />
<Compile Include="API\V1\NotificationsControllerTests.cs" />
<Compile Include="API\V1\ApplicationInfoControllerTests.cs" />
Expand Down
43 changes: 0 additions & 43 deletions Voyage.Api.UserManager/API/v1/AccountController.cs

This file was deleted.

1 change: 0 additions & 1 deletion Voyage.Api.UserManager/Voyage.Api.UserManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="API\v1\AccountController.cs" />
<Compile Include="API\v1\RoleController.cs" />
<Compile Include="API\v1\UserController.cs" />
<Compile Include="API\v1\VerifyController.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,31 @@
using Voyage.Models;
using Voyage.Services.Profile;
using Swashbuckle.Swagger.Annotations;
using Voyage.Services.User;

namespace Voyage.Api.API.V1
{
/// <summary>
/// Controller that handles user session actions.
/// </summary>
[RoutePrefix(Constants.RoutePrefixes.V1)]
public class ProfileController : ApiController
public class ProfilesController : ApiController
{
private readonly IAuthenticationManager _authenticationManager;
private readonly IProfileService _profileService;
private readonly IUserService _userService;

/// <summary>
/// Constructor for the Profile Controller.
/// </summary>
public ProfileController(IAuthenticationManager authenticationManager, IProfileService profileService)
public ProfilesController(
IAuthenticationManager authenticationManager,
IProfileService profileService,
IUserService userService)
{
_authenticationManager = authenticationManager.ThrowIfNull(nameof(authenticationManager));
_profileService = profileService.ThrowIfNull(nameof(profileService));
_userService = userService.ThrowIfNull(nameof(userService));
}

/// <summary>
Expand Down Expand Up @@ -56,5 +62,19 @@ public async Task<IHttpActionResult> UpdateProfile(ProfileModel model)
var result = await _profileService.UpdateProfileAsync(userId, model);
return Ok(result);
}

/// <summary>
/// Registers a new user.
/// </summary>
[HttpPost]
[Route("profiles/register")]
[SwaggerResponse(201, "UserModel", typeof(UserModel))]
[SwaggerResponse(400, "BadRequestException")]
public async Task<IHttpActionResult> Register(RegistrationModel model)
{
var result = await _userService.RegisterAsync(model);
await _profileService.GetInitialProfileImageAsync(result.Id, result.Email);
return CreatedAtRoute("GetUserAsync", new { userId = result.Id }, result);
}
}
}
2 changes: 1 addition & 1 deletion Voyage.Api/Voyage.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="API\v1\ChatsController.cs" />
<Compile Include="API\v1\ProfileController.cs" />
<Compile Include="API\v1\ProfilesController.cs" />
<Compile Include="API\v1\AdminController.cs" />
<Compile Include="API\v1\ApplicationInfoController.cs" />
<Compile Include="API\v1\NotificationsController.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void UserPhoneEntity_Should_MapTo_UserPhoneModel()
model.Id.Should().Be(userPhone.Id);
model.UserId.Should().Be(userPhone.UserId);
model.PhoneNumber.Should().Be(userPhone.PhoneNumber);
model.PhoneType.Should().Be(userPhone.PhoneType);
model.PhoneType.Should().Be(userPhone.PhoneType.ToString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void Should_Have_Error_When_ConfirmPassword_Does_Not_Match_Password()
Email = "first.last@firstlast.com",
Password = "password!!!",
ConfirmPassword = "notpassword!!",
PhoneNumbers = new List<UserPhoneModel>
Phones = new List<UserPhoneModel>
{
new UserPhoneModel
{
Expand All @@ -98,7 +98,7 @@ public void Should_Not_Have_Error_When_ConfirmPassword_Matches_Password()
Email = "first.last@firstlast.com",
Password = "password!!!",
ConfirmPassword = "password!!!",
PhoneNumbers = new List<UserPhoneModel>
Phones = new List<UserPhoneModel>
{
new UserPhoneModel
{
Expand Down Expand Up @@ -129,7 +129,7 @@ public void Should_Have_Error_When_Email_Is_Invalid()
[Fact]
public void Should_Have_Error_when_PhoneNumber_Is_Null()
{
_validator.ShouldHaveValidationErrorFor(model => model.PhoneNumbers, new List<UserPhoneModel>());
_validator.ShouldHaveValidationErrorFor(model => model.Phones, new List<UserPhoneModel>());
}

[Fact]
Expand Down
5 changes: 3 additions & 2 deletions Voyage.Models/Map/Profiles/UserPhoneProfile.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AutoMapper;
using Voyage.Models.Entities;
using Voyage.Models.Enum;

namespace Voyage.Models.Map.Profiles
{
Expand All @@ -10,14 +11,14 @@ public UserPhoneProfile()
CreateMap<UserPhone, UserPhoneModel>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
.ForMember(dest => dest.PhoneNumber, opt => opt.MapFrom(src => src.PhoneNumber))
.ForMember(dest => dest.PhoneType, opt => opt.MapFrom(src => src.PhoneType))
.ForMember(dest => dest.PhoneType, opt => opt.MapFrom(src => src.PhoneType.ToString()))
.ForMember(dest => dest.UserId, opt => opt.MapFrom(src => src.UserId))
.ForMember(dest => dest.VerificationCode, opt => opt.MapFrom(src => src.VerificationCode));

CreateMap<UserPhoneModel, UserPhone>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
.ForMember(dest => dest.PhoneNumber, opt => opt.MapFrom(src => src.PhoneNumber))
.ForMember(dest => dest.PhoneType, opt => opt.MapFrom(src => src.PhoneType))
.ForMember(dest => dest.PhoneType, opt => opt.MapFrom(src => (PhoneType)System.Enum.Parse(typeof(PhoneType), src.PhoneType)))
.ForMember(dest => dest.UserId, opt => opt.MapFrom(src => src.UserId))
.ForMember(dest => dest.VerificationCode, opt => opt.MapFrom(src => src.VerificationCode))
.ForAllOtherMembers(opt => opt.Ignore());
Expand Down
2 changes: 1 addition & 1 deletion Voyage.Models/RegistrationModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public class RegistrationModel
public string LastName { get; set; }

[AntiXss]
public List<UserPhoneModel> PhoneNumbers { get; set; }
public List<UserPhoneModel> Phones { get; set; }
}
}
7 changes: 2 additions & 5 deletions Voyage.Models/UserPhoneModel.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using FluentValidation.Attributes;
using Voyage.Models.Enum;
using Voyage.Models.Validators;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Embarr.WebAPI.AntiXss;

namespace Voyage.Models
Expand All @@ -18,8 +15,8 @@ public class UserPhoneModel
[AntiXss]
public string PhoneNumber { get; set; }

[JsonConverter(typeof(StringEnumConverter))]
public PhoneType PhoneType { get; set; }
[AntiXss]
public string PhoneType { get; set; }

[AntiXss]
public string VerificationCode { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Voyage.Models/Validators/RegistrationModelValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public RegistrationModelValidator()
.NotEmpty()
.WithErrorCodeMessage(Constants.ErrorCodes.MissingField, "Last name is a required field");

RuleFor(_ => _.PhoneNumbers)
RuleFor(_ => _.Phones)
.NotEmpty()
.WithErrorCodeMessage(Constants.ErrorCodes.MissingField, "Phone Number is a required field");
}
Expand Down
9 changes: 8 additions & 1 deletion Voyage.Models/Validators/UserPhoneModelValidator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using FluentValidation;
using System.Collections.Generic;
using FluentValidation;
using Voyage.Core;
using Voyage.Models.Enum;

namespace Voyage.Models.Validators
{
Expand All @@ -10,6 +12,11 @@ public UserPhoneModelValidator()
RuleFor(_ => _.PhoneNumber)
.NotEmpty()
.WithErrorCodeMessage(Constants.ErrorCodes.MissingField, "Phone number is a required field");

RuleFor(_ => _.PhoneType)
.NotEmpty()
.Must(type => new List<string> { PhoneType.Mobile.ToString(), PhoneType.Office.ToString(), PhoneType.Home.ToString(), PhoneType.Other.ToString() }.Contains(type))
.WithErrorCodeMessage(Constants.ErrorCodes.InvalidPhoneNumber, "Invalid phone type. Must be one of 'Mobile', 'Office', 'Home', 'Other'.");
}
}
}
8 changes: 4 additions & 4 deletions Voyage.Services.UnitTests/UserServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public async void UpdateUser_Should_Remove_Phone_Numbers_When_Not_In_Source()
public async void UpdateUser_Should_Update_Existing_Phone_Numbers()
{
var id = Fixture.Create<string>();
var phoneModel = Fixture.Create<UserPhoneModel>();
var phoneModel = new UserPhoneModel { PhoneType = "Mobile" };

var phone = Fixture.Build<UserPhone>()
.With(_ => _.User, null)
Expand Down Expand Up @@ -269,7 +269,7 @@ public async void UpdateUser_Should_Update_Existing_Phone_Numbers()
public async void UpdateUser_Should_Add_New_Phone_Numbers()
{
var id = Fixture.Create<string>();
var phone = Fixture.Create<UserPhoneModel>();
var phone = new UserPhoneModel { PhoneType = "Mobile" };

var userModel = new UserModel
{
Expand Down Expand Up @@ -843,12 +843,12 @@ public void Register_Should_Call_FindByNameAsync_And_Throw_Exception_For_Duplica
.With(_ => _.UserName, "testUserName")
.With(_ => _.Email, "test@test.com")
.With(_ => _.Password, "cool1Password!!")
.With(_ => _.PhoneNumbers, new List<UserPhoneModel>
.With(_ => _.Phones, new List<UserPhoneModel>
{
new UserPhoneModel
{
PhoneNumber = "(202) 555-0100",
PhoneType = Voyage.Models.Enum.PhoneType.Mobile
PhoneType = Voyage.Models.Enum.PhoneType.Mobile.ToString()
}
})
.Create();
Expand Down
Loading

0 comments on commit 2f4e983

Please sign in to comment.