diff --git a/PocketSharp.Tests/AccountTests.cs b/PocketSharp.Tests/AccountTests.cs index 876f1e4..7733159 100644 --- a/PocketSharp.Tests/AccountTests.cs +++ b/PocketSharp.Tests/AccountTests.cs @@ -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(async () => - { - await client.RegisterAccount("abcdefghijklmnopqrstuvwxyz", "pocketsharp@outlook.com", "mypassword"); - }); - - await ThrowsAsync(async () => - { - await client.RegisterAccount("oiu_my:o;", "pocketsharp@outlook.com", "mypassword"); - }); - - await ThrowsAsync(async () => - { - await client.RegisterAccount("myusername", "pocketsharpoutlook.com", "mypassword"); - }); - - await ThrowsAsync(async () => - { - await client.RegisterAccount("myusername", "pocketsharp@outlook", "mypassword"); - }); - - await ThrowsAsync(async () => - { - await client.RegisterAccount("myusername", "pocketsharp@outlook,com", "mypassword"); - }); - - await ThrowsAsync(async () => - { - await client.RegisterAccount("myusername", "pocketsharp@outlook.com", "my"); - }); - - await ThrowsAsync(async () => - { - await client.RegisterAccount("myusername", null, "mypassword"); - }); + Assert.True(uri.OriginalString.Contains(requestCode)); + Assert.True(uri.OriginalString.Contains("force=signup")); } } } diff --git a/PocketSharp/Components/Account.cs b/PocketSharp/Components/Account.cs index 9946f7b..226bed4 100644 --- a/PocketSharp/Components/Account.cs +++ b/PocketSharp/Components/Account.cs @@ -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 /// - /// 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 /// - /// The username. - /// The email. - /// The password. - /// The cancellation token. - /// - /// All parameters are required - /// Invalid email address. - /// or - /// Invalid username. Please only use letters, numbers, and/or dashes and between 1-20 characters. - /// or - /// Invalid password. - /// - public async Task RegisterAccount(string username, string email, string password, CancellationToken cancellationToken = default(CancellationToken)) + /// The requestCode. If no requestCode is supplied, the property from the PocketClient intialization is used. + /// A valid URI to redirect the user to. + /// Call GetRequestCode() first to receive a request_code + 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("signup", cancellationToken, parameters.Convert(), false); - - return response.Status; + return new Uri(String.Format("{0}&force=signup", String.Format(authentificationUri, RequestCode, CallbackUri))); } } } diff --git a/PocketSharp/IPocketClient.cs b/PocketSharp/IPocketClient.cs index f462865..420bacf 100644 --- a/PocketSharp/IPocketClient.cs +++ b/PocketSharp/IPocketClient.cs @@ -84,22 +84,12 @@ namespace PocketSharp Task GetUser(string requestCode = null, CancellationToken cancellationToken = default(CancellationToken)); /// - /// Registers a new account. - /// Account has to be activated via a activation email sent by Pocket. + /// Generate registration URI from requestCode /// - /// The username. - /// The email. - /// The password. - /// The cancellation token. - /// - /// All parameters are required - /// Invalid email address. - /// or - /// Invalid username. Please only use letters, numbers, and/or dashes and between 1-20 characters. - /// or - /// Invalid password. - /// - Task RegisterAccount(string username, string email, string password, CancellationToken cancellationToken = default(CancellationToken)); + /// The requestCode. If no requestCode is supplied, the property from the PocketClient intialization is used. + /// A valid URI to redirect the user to. + /// Call GetRequestCode() first to receive a request_code + Uri GenerateRegistrationUri(string requestCode = null); #endregion #region add methods