add account registration

This commit is contained in:
2013-09-27 01:03:02 +02:00
parent 966a35e2c6
commit 2154740550
7 changed files with 169 additions and 14 deletions
+59
View File
@@ -0,0 +1,59 @@
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Xunit;
using PocketSharp.Models;
namespace PocketSharp.Tests
{
public class AccountTests : TestsBase
{
public AccountTests() : base() { }
[Fact]
public async Task IsUserRegistered()
{
string randomId = ((int)((DateTime)DateTime.Now - new DateTime(1970, 1, 1)).TotalSeconds).ToString();
bool success = await client.RegisterAccount("pocket-" + randomId, String.Format("pocketsharp-{0}@outlook.com", randomId), "mypassword");
Assert.True(success);
}
[Fact]
public async Task AreInvalidRegistrationsBlocked()
{
await ThrowsAsync<FormatException>(async () =>
{
await client.RegisterAccount("abcdefghijklmnopqrstuvwxyz", "pocketsharp@outlook.com", "mypassword");
});
await ThrowsAsync<FormatException>(async () =>
{
await client.RegisterAccount("oiu_my:o;", "pocketsharp@outlook.com", "mypassword");
});
await ThrowsAsync<FormatException>(async () =>
{
await client.RegisterAccount("myusername", "pocketsharpoutlook.com", "mypassword");
});
await ThrowsAsync<FormatException>(async () =>
{
await client.RegisterAccount("myusername", "pocketsharp@outlook", "mypassword");
});
await ThrowsAsync<FormatException>(async () =>
{
await client.RegisterAccount("myusername", "pocketsharp@outlook,com", "mypassword");
});
await ThrowsAsync<ArgumentNullException>(async () =>
{
await client.RegisterAccount("myusername", null, "mypassword");
});
}
}
}
-13
View File
@@ -1,13 +0,0 @@
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Xunit;
using PocketSharp.Models;
namespace PocketSharp.Tests
{
class AuthenticationTests : TestsBase
{
public AuthenticationTests() : base() { }
}
}
+1 -1
View File
@@ -67,7 +67,7 @@
</Choose>
<ItemGroup>
<Compile Include="AddTests.cs" />
<Compile Include="AuthenticationTests.cs" />
<Compile Include="AccountTests.cs" />
<Compile Include="ModifyTagsTests.cs" />
<Compile Include="ModifyTests.cs" />
<Compile Include="RetrieveTests.cs" />
+17
View File
@@ -33,5 +33,22 @@ namespace PocketSharp.Tests
await client.Delete(id);
});
}
// async throws
public static async Task ThrowsAsync<TException>(Func<Task> func)
{
var expected = typeof(TException);
Type actual = null;
try
{
await func();
}
catch (Exception e)
{
actual = e.GetType();
}
Assert.Equal(expected, actual);
}
}
}
+48
View File
@@ -1,6 +1,7 @@
using PocketSharp.Models;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace PocketSharp
@@ -95,5 +96,52 @@ namespace PocketSharp
return AccessCode;
}
/// <summary>
/// Registers a new account.
/// Account has to be activated via a activation email sent by Pocket.
/// </summary>
/// <param name="username">The username.</param>
/// <param name="email">The email.</param>
/// <param name="password">The password.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">All parameters are required</exception>
/// <exception cref="System.FormatException">
/// Invalid email address.
/// or
/// Invalid username. Please only use letters, numbers, and/or dashes and between 1-20 characters.
/// </exception>
public async Task<bool> RegisterAccount(string username, string email, string password)
{
if (username == null || email == null || password == null)
{
throw new ArgumentNullException("All parameters are required");
}
Match matchEmail = Regex.Match(email, @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,10}))$");
Match matchUsername = Regex.Match(username, @"^([\w\-]{1,20})$");
if (!matchEmail.Success)
{
throw new FormatException("Invalid email address.");
}
if (!matchUsername.Success)
{
throw new FormatException("Invalid username. Please only use letters, numbers, and/or dashes and between 1-20 characters.");
}
RegisterParameters parameters = new RegisterParameters()
{
Username = username,
Email = email,
Password = password
};
ResponseBase response = await Request<ResponseBase>("signup", parameters.Convert(), false);
return response.Status;
}
}
}
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
using System.Linq;
namespace PocketSharp.Models
{
/// <summary>
/// All parameters which can be passed to register a user
/// </summary>
[DataContract]
internal class RegisterParameters : Parameters
{
/// <summary>
/// Gets or sets the username.
/// </summary>
/// <value>
/// The username.
/// </value>
[DataMember(Name = "username")]
public string Username { get; set; }
/// <summary>
/// Gets or sets the E-Mail.
/// </summary>
/// <value>
/// The E-Mail.
/// </value>
[DataMember(Name = "email")]
public string Email { get; set; }
/// <summary>
/// Gets or sets the password.
/// </summary>
/// <value>
/// The password.
/// </value>
[DataMember(Name = "password")]
public string Password { get; set; }
}
}
+1
View File
@@ -40,6 +40,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Components\Statistics.cs" />
<Compile Include="Models\Parameters\RegisterParameters.cs" />
<Compile Include="Models\PocketStatistics.cs" />
<Compile Include="PocketException.cs" />
<Compile Include="Components\Add.cs" />