Skip to content

Commit

Permalink
#4, Updating DocumentDB client ref. Minor refactor context constructo…
Browse files Browse the repository at this point in the history
…r with added tests. Rev version to 1.3
  • Loading branch information
dlmelendez committed Apr 18, 2017
1 parent 86e675b commit b8d9138
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
<HintPath>..\..\packages\Microsoft.AspNet.Identity.Core.2.2.1\lib\net45\Microsoft.AspNet.Identity.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Documents.Client, Version=1.12.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.DocumentDB.1.12.0\lib\net45\Microsoft.Azure.Documents.Client.dll</HintPath>
<Reference Include="Microsoft.Azure.Documents.Client, Version=1.13.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.DocumentDB.1.13.1\lib\net45\Microsoft.Azure.Documents.Client.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
Expand Down Expand Up @@ -108,7 +108,9 @@
<None Include="ElCamino.AspNet.Identity.DocumentDB.nuspec">
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="IdentityResources.resx">
Expand Down Expand Up @@ -143,11 +145,11 @@
<!-- 7-zip dlls-->
<Exec Command="$(SolutionDir)vendor\7-zip\7z.exe a $(TargetDir)$(TargetName)_$(Configuration).zip $(TargetPath) $(TargetDir)$(TargetName).pdb" />
</Target>
<Import Project="..\..\packages\Microsoft.Azure.DocumentDB.1.12.0\build\Microsoft.Azure.DocumentDB.targets" Condition="Exists('..\..\packages\Microsoft.Azure.DocumentDB.1.12.0\build\Microsoft.Azure.DocumentDB.targets')" />
<Import Project="..\..\packages\Microsoft.Azure.DocumentDB.1.13.1\build\Microsoft.Azure.DocumentDB.targets" Condition="Exists('..\..\packages\Microsoft.Azure.DocumentDB.1.13.1\build\Microsoft.Azure.DocumentDB.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\Microsoft.Azure.DocumentDB.1.12.0\build\Microsoft.Azure.DocumentDB.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Azure.DocumentDB.1.12.0\build\Microsoft.Azure.DocumentDB.targets'))" />
<Error Condition="!Exists('..\..\packages\Microsoft.Azure.DocumentDB.1.13.1\build\Microsoft.Azure.DocumentDB.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Azure.DocumentDB.1.13.1\build\Microsoft.Azure.DocumentDB.targets'))" />
</Target>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<description>$description$</description>
<releaseNotes>
Release $version$ This project is an open source plugin to ASP.NET Identity framework using Azure DocumentDB.
* Updating references.
* Adding configurable collection names.
* Updating DocumentDB client reference.
</releaseNotes>
<copyright>Copyright 2017</copyright>
<tags>ASP.NET Identity 2.0 Azure DocumentDB Storage MVC UserStore RoleStore UserManager</tags>
Expand Down
2 changes: 1 addition & 1 deletion src/identity.documentdb/Helpers/UriEncodeKeyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public override string GenerateRowKeyIdentityUserLogin(string loginProvider, str

public override double KeyVersion
{
get { return 1.2; }
get { return 1.3; }
}
}
}
32 changes: 13 additions & 19 deletions src/identity.documentdb/IdentityCloudContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@ public IdentityCloudContext()
: base()
{ }

public IdentityCloudContext(string uri, string authKey, string database, ConnectionPolicy policy = null)
: base(uri, authKey, database, policy)
public IdentityCloudContext(string uri, string authKey, string database, ConnectionPolicy policy = null,
string usersCollectionName = null, string rolesCollectionName = null)
: base(uri, authKey, database, policy, usersCollectionName, rolesCollectionName)
{ }

public IdentityCloudContext(string usersCollectionName, string rolesCollectionName)
: base(usersCollectionName, rolesCollectionName)
{ }

public IdentityCloudContext(string uri, string authKey, string database, ConnectionPolicy policy, string usersCollectionName, string rolesCollectionName)
: base(uri, authKey, database, policy, usersCollectionName, rolesCollectionName)
{ }
}

public class IdentityCloudContext<TUser> : IdentityCloudContext<TUser, IdentityRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim> where TUser : IdentityUser
Expand All @@ -38,17 +36,15 @@ public IdentityCloudContext()
: base()
{ }

public IdentityCloudContext(string uri, string authKey, string database, ConnectionPolicy policy = null)
: base(uri, authKey, database, policy)
public IdentityCloudContext(string uri, string authKey, string database, ConnectionPolicy policy = null,
string usersCollectionName = null, string rolesCollectionName = null)
: base(uri, authKey, database, policy, usersCollectionName, rolesCollectionName)
{ }

public IdentityCloudContext(string usersCollectionName, string rolesCollectionName)
: base(usersCollectionName, rolesCollectionName)
{ }

public IdentityCloudContext(string uri, string authKey, string database, ConnectionPolicy policy, string usersCollectionName, string rolesCollectionName)
: base(uri, authKey, database, policy, usersCollectionName, rolesCollectionName)
{ }

}

Expand Down Expand Up @@ -108,14 +104,12 @@ public IdentityCloudContext(string usersCollectionName, string rolesCollectionNa

}

public IdentityCloudContext(string uri, string authKey, string database, ConnectionPolicy policy = null) :
this(uri, authKey, database, policy, Constants.DocumentCollectionIds.UsersCollection, Constants.DocumentCollectionIds.RolesCollection)
{ }

public IdentityCloudContext(string uri, string authKey, string database, ConnectionPolicy policy, string usersCollection, string rolesCollection)
public IdentityCloudContext(string uri, string authKey, string database, ConnectionPolicy policy = null,
string usersCollection = null,
string rolesCollection = null)
{
_userDocumentCollection = new DocumentCollection { Id = usersCollection };
_roleDocumentCollection = new DocumentCollection { Id = rolesCollection };
_userDocumentCollection = new DocumentCollection { Id = usersCollection?? Constants.DocumentCollectionIds.UsersCollection };
_roleDocumentCollection = new DocumentCollection { Id = rolesCollection?? Constants.DocumentCollectionIds.RolesCollection };

_client = new DocumentClient(new Uri(uri), authKey, policy, ConsistencyLevel.Session);
InitDatabase(database);
Expand Down Expand Up @@ -177,14 +171,14 @@ private void InitCollections()
var ucTask = _client.CreateDocumentCollectionIfNotExistsAsync(_db.SelfLink, _userDocumentCollection);
ucTask.Wait();
var uc = ucTask.Result;
UserDocumentCollection = uc;
UserDocumentCollection = uc;
}),
new TaskFactory().StartNew(() =>
{
var rcTask = _client.CreateDocumentCollectionIfNotExistsAsync(_db.SelfLink, _roleDocumentCollection);
rcTask.Wait();
var rc = rcTask.Result;
RoleDocumentCollection = rc;
RoleDocumentCollection = rc;
})
};
Task.WaitAll(tasks);
Expand Down
6 changes: 3 additions & 3 deletions src/identity.documentdb/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
[assembly: Guid("29B22384-59C4-40E9-B59F-346666629742")]

//http://semver.org/
[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: AssemblyInformationalVersion("1.2.0.0")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
[assembly: AssemblyInformationalVersion("1.3.0.0")]

44 changes: 18 additions & 26 deletions src/identity.documentdb/UserStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,10 @@ public async virtual Task DeleteAsync(TUser user)
throw new ArgumentNullException("user");
}

await Context.Client.DeleteDocumentAsync(user.SelfLink,
var result = await Context.Client.DeleteDocumentAsync(user.SelfLink,
Context.RequestOptions);
Context.SetSessionTokenIfEmpty(result.SessionToken);

}

public void Dispose()
Expand Down Expand Up @@ -205,27 +207,21 @@ public async virtual Task<TUser> FindAsync(UserLoginInfo login)
string loginId = login.GenerateRowKeyUserLoginInfo();

var result = await Context.Client.ExecuteStoredProcedureAsync<IEnumerable<dynamic>>(Context.GetUserByLoginSproc.SelfLink,
Context.RequestOptions,
new dynamic[] { loginId });
if (result.Response != null)
{
Context.SetSessionTokenIfEmpty(result.SessionToken);
return GetUserAggregate(result.Response.ToList());
}
return null;
Context.SetSessionTokenIfEmpty(result.SessionToken);
return GetUserAggregate(result.Response.ToList());

}

public async Task<TUser> FindByEmailAsync(string plainEmail)
{
var result = await Context.Client.ExecuteStoredProcedureAsync<IEnumerable<dynamic>>(Context.GetUserByEmailSproc.SelfLink,
Context.RequestOptions,
new dynamic[] { plainEmail });

if (result.Response != null)
{
Context.SetSessionTokenIfEmpty(result.SessionToken);
return GetUserAggregate(result.Response.ToList());
}
return null;
}

public virtual async Task<TUser> FindByIdAsync(TKey userId)
Expand All @@ -238,13 +234,10 @@ public async virtual Task<TUser> FindByNameAsync(string userName)
{
this.ThrowIfDisposed();
var result = await Context.Client.ExecuteStoredProcedureAsync<IEnumerable<dynamic>>(Context.GetUserByUserNameSproc.SelfLink,
Context.RequestOptions,
new dynamic[] { userName });
if (result.Response != null)
{
Context.SetSessionTokenIfEmpty(result.SessionToken);
return await Task.FromResult<TUser>(GetUserAggregate(result.Response.ToList()));
}
return null;
Context.SetSessionTokenIfEmpty(result.SessionToken);
return await Task.FromResult<TUser>(GetUserAggregate(result.Response.ToList()));
}

public Task<int> GetAccessFailedCountAsync(TUser user)
Expand Down Expand Up @@ -380,15 +373,14 @@ public Task<bool> GetTwoFactorEnabledAsync(TUser user)

private async Task<TUser> GetUserAggregateAsync(string userId)
{
var task = Context.Client.ExecuteStoredProcedureAsync<IEnumerable<dynamic>>(Context.GetUserByIdSproc.SelfLink,
new dynamic[] { userId });
task.Wait();
if (task.Result.Response != null)
{
return await Task.FromResult<TUser>(GetUserAggregate(task.Result.Response.ToList()));
}
return null;

return await Context.Client.ExecuteStoredProcedureAsync<IEnumerable<dynamic>>(Context.GetUserByIdSproc.SelfLink,
Context.RequestOptions,
new dynamic[] { userId })
.ContinueWith((storedProcTask) => {
Context.SetSessionTokenIfEmpty(storedProcTask.Result.SessionToken);
return GetUserAggregate(storedProcTask.Result.Response.ToList());
});

}

private TUser GetUserAggregate(List<dynamic> userResults)
Expand Down
2 changes: 1 addition & 1 deletion src/identity.documentdb/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net45" />
<package id="Microsoft.Azure.DocumentDB" version="1.12.0" targetFramework="net45" />
<package id="Microsoft.Azure.DocumentDB" version="1.13.1" targetFramework="net45" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
<HintPath>..\..\packages\Microsoft.AspNet.Identity.Core.2.2.1\lib\net45\Microsoft.AspNet.Identity.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Documents.Client, Version=1.12.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.DocumentDB.1.12.0\lib\net45\Microsoft.Azure.Documents.Client.dll</HintPath>
<Reference Include="Microsoft.Azure.Documents.Client, Version=1.13.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.DocumentDB.1.13.1\lib\net45\Microsoft.Azure.Documents.Client.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
Expand Down Expand Up @@ -127,12 +127,12 @@
</Choose>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\..\packages\Microsoft.Azure.DocumentDB.1.12.0\build\Microsoft.Azure.DocumentDB.targets" Condition="Exists('..\..\packages\Microsoft.Azure.DocumentDB.1.12.0\build\Microsoft.Azure.DocumentDB.targets')" />
<Import Project="..\..\packages\Microsoft.Azure.DocumentDB.1.13.1\build\Microsoft.Azure.DocumentDB.targets" Condition="Exists('..\..\packages\Microsoft.Azure.DocumentDB.1.13.1\build\Microsoft.Azure.DocumentDB.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\Microsoft.Azure.DocumentDB.1.12.0\build\Microsoft.Azure.DocumentDB.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Azure.DocumentDB.1.12.0\build\Microsoft.Azure.DocumentDB.targets'))" />
<Error Condition="!Exists('..\..\packages\Microsoft.Azure.DocumentDB.1.13.1\build\Microsoft.Azure.DocumentDB.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Azure.DocumentDB.1.13.1\build\Microsoft.Azure.DocumentDB.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
6 changes: 3 additions & 3 deletions tests/identity.documentdb.tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
[assembly: Guid("B50C24F2-2BD4-42EC-87C8-D8BDEBB1097E")]

//http://semver.org/
[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: AssemblyInformationalVersion("1.2.0.0")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
[assembly: AssemblyInformationalVersion("1.3.0.0")]
42 changes: 42 additions & 0 deletions tests/identity.documentdb.tests/RoleStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,48 @@ public void CreateRoleScratch()
}
}

[TestMethod]
[TestCategory("Identity.Azure.RoleStore")]
public void CreateRoleSameCollection()
{
const string sameCol = "same";
//var ic = new IdentityCloudContext<IdentityUser>(sameCol, sameCol);

using (RoleStore<IdentityRole> store = new RoleStore<IdentityRole>(new IdentityCloudContext(sameCol, sameCol)))
{
using (RoleManager<IdentityRole> manager = new RoleManager<IdentityRole>(store))
{
Assert.IsNotNull(store.Context.Database);
var role = CreateRoleHelper(manager);
WriteLineObject<IdentityRole>(role);

var result = store.Context.Client.DeleteDocumentCollectionAsync(store.Context.UserDocumentCollection.SelfLink).Result;

}
}

}

[TestMethod]
[TestCategory("Identity.Azure.RoleStore")]
public void CreateQueryRoles()
{

using (RoleStore<IdentityRole> store = new RoleStore<IdentityRole>())
{
using (RoleManager<IdentityRole> manager = new RoleManager<IdentityRole>(store))
{
var role = CreateRoleHelper(manager);
WriteLineObject<IdentityRole>(role);

var r = manager.Roles.AsQueryable().Where(o => o.Name == role.Name).ToList().FirstOrDefault();
Assert.IsNotNull(r);

}
}

}

[TestMethod]
[TestCategory("Identity.Azure.RoleStore")]
public void CreateRole()
Expand Down
6 changes: 4 additions & 2 deletions tests/identity.documentdb.tests/UserStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public partial class UserStoreTests
"PhoneNumber",
"TwoFactorEnabled",
"PasswordHash",
"Email",
"EmailNone"
"EmailNone",
"PhoneNumberConfirmed"
};

#endregion
Expand Down Expand Up @@ -157,6 +157,8 @@ private IdentityUser CreateTestUser(bool createPassword = true, bool createEmail

using (UserStore<IdentityUser> store = new UserStore<IdentityUser>())
{
store.Context.Client.ConnectionPolicy.RetryOptions.MaxRetryAttemptsOnThrottledRequests = 30;

using (UserManager<IdentityUser> manager = new UserManager<IdentityUser>(store))
{
var user = GenTestUser();
Expand Down
2 changes: 1 addition & 1 deletion tests/identity.documentdb.tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net45" />
<package id="Microsoft.Azure.DocumentDB" version="1.12.0" targetFramework="net45" />
<package id="Microsoft.Azure.DocumentDB" version="1.13.1" targetFramework="net45" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
</packages>

0 comments on commit b8d9138

Please sign in to comment.