fixes #18 - new registration via redirect

This commit is contained in:
2014-02-07 14:12:09 +01:00
parent 13c98e2d59
commit 13664c4201
3 changed files with 24 additions and 105 deletions
+5 -46
View File
@@ -1,8 +1,6 @@
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Xunit;
using PocketSharp.Models;
namespace PocketSharp.Tests
{
@@ -12,53 +10,14 @@ namespace PocketSharp.Tests
[Fact]
public async Task IsUserRegistered()
public async Task IsRegistrationURLSuccessfullyCreated()
{
string randomId = ((int)((DateTime)DateTime.Now - new DateTime(1970, 1, 1)).TotalSeconds).ToString();
string requestCode = await client.GetRequestCode();
bool success = await client.RegisterAccount("pocket-" + randomId, String.Format("pocketsharp-{0}@outlook.com", randomId), "mypassword");
Uri uri = client.GenerateRegistrationUri(requestCode);
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<FormatException>(async () =>
{
await client.RegisterAccount("myusername", "pocketsharp@outlook.com", "my");
});
await ThrowsAsync<ArgumentNullException>(async () =>
{
await client.RegisterAccount("myusername", null, "mypassword");
});
Assert.True(uri.OriginalString.Contains(requestCode));
Assert.True(uri.OriginalString.Contains("force=signup"));
}
}
}
+14 -44
View File
@@ -1,7 +1,6 @@
using PocketSharp.Models;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
@@ -61,7 +60,7 @@ namespace PocketSharp
RequestCode = requestCode;
}
return new Uri(string.Format(authentificationUri, RequestCode, CallbackUri));
return new Uri(String.Format(authentificationUri, RequestCode, CallbackUri));
}
@@ -103,56 +102,27 @@ namespace PocketSharp
/// <summary>
/// Registers a new account.
/// Account has to be activated via a activation email sent by Pocket.
/// Generate registration URI from requestCode
/// Follow the steps as with GenerateAuthenticationUri, but for unregistered users
/// </summary>
/// <param name="username">The username.</param>
/// <param name="email">The email.</param>
/// <param name="password">The password.</param>
/// <param name="cancellationToken">The cancellation token.</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.
/// or
/// Invalid password.</exception>
/// <exception cref="PocketException"></exception>
public async Task<bool> RegisterAccount(string username, string email, string password, CancellationToken cancellationToken = default(CancellationToken))
/// <param name="requestCode">The requestCode. If no requestCode is supplied, the property from the PocketClient intialization is used.</param>
/// <returns>A valid URI to redirect the user to.</returns>
/// <exception cref="System.NullReferenceException">Call GetRequestCode() first to receive a request_code</exception>
public Uri GenerateRegistrationUri(string requestCode = null)
{
if (username == null || email == null || password == null)
// check if request code is available
if (RequestCode == null && requestCode == null)
{
throw new ArgumentNullException("All parameters are required");
throw new NullReferenceException("Call GetRequestCode() first to receive a request_code");
}
Match matchEmail = Regex.Match(email, @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,10}))$");
Match matchUsername = Regex.Match(username, @"^([\w\-_]{1,20})$");
if (!matchEmail.Success)
// override property with given param if available
if (requestCode != null)
{
throw new FormatException("(1) Invalid email address.");
RequestCode = requestCode;
}
if (!matchUsername.Success)
{
throw new FormatException("(2) Invalid username. Please only use letters, numbers, and/or dashes and between 1-20 characters.");
}
if (password.Length < 6)
{
throw new FormatException("(3) Invalid password.");
}
RegisterParameters parameters = new RegisterParameters()
{
Username = username,
Email = email,
Password = password
};
ResponseBase response = await Request<ResponseBase>("signup", cancellationToken, parameters.Convert(), false);
return response.Status;
return new Uri(String.Format("{0}&force=signup", String.Format(authentificationUri, RequestCode, CallbackUri)));
}
}
}
+5 -15
View File
@@ -84,22 +84,12 @@ namespace PocketSharp
Task<PocketUser> GetUser(string requestCode = null, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Registers a new account.
/// Account has to be activated via a activation email sent by Pocket.
/// Generate registration URI from requestCode
/// </summary>
/// <param name="username">The username.</param>
/// <param name="email">The email.</param>
/// <param name="password">The password.</param>
/// <param name="cancellationToken">The cancellation token.</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.
/// or
/// Invalid password.</exception>
/// <exception cref="PocketException"></exception>
Task<bool> RegisterAccount(string username, string email, string password, CancellationToken cancellationToken = default(CancellationToken));
/// <param name="requestCode">The requestCode. If no requestCode is supplied, the property from the PocketClient intialization is used.</param>
/// <returns>A valid URI to redirect the user to.</returns>
/// <exception cref="System.NullReferenceException">Call GetRequestCode() first to receive a request_code</exception>
Uri GenerateRegistrationUri(string requestCode = null);
#endregion
#region add methods