Skip to content

Commit 0a9f4f9

Browse files
committed
Added some API unit tests for ProfilesController and fixed bugs that came up.
1 parent 0a074f5 commit 0a9f4f9

15 files changed

+680
-37
lines changed
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Guncho.Services;
5+
6+
namespace Guncho.Api.Tests
7+
{
8+
internal class FakePlayersService : IPlayersService
9+
{
10+
private readonly List<Player> players = new List<Player>();
11+
12+
public IEnumerable<Player> GetAllPlayers()
13+
{
14+
return players;
15+
}
16+
17+
public Player GetPlayerById(int id)
18+
{
19+
return players.SingleOrDefault(p => p.ID == id);
20+
}
21+
22+
public void Add(string name, bool isAdmin = false, bool isGuest = false)
23+
{
24+
var id = players.Count + 1;
25+
players.Add(new Player(id, name, isAdmin, isGuest));
26+
}
27+
28+
public Player GetPlayerByName(string name)
29+
{
30+
return players.SingleOrDefault(p => p.Name == name);
31+
}
32+
33+
public bool IsValidNameChange(string oldName, string newName)
34+
{
35+
return false;
36+
}
37+
38+
public bool TransactionalUpdate(Player player, Func<Player, bool> transaction)
39+
{
40+
return transaction(player);
41+
}
42+
}
43+
}

Guncho.Api.Tests/FakeRealmsService.cs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Threading.Tasks;
5+
using Guncho.Services;
6+
7+
namespace Guncho.Api.Tests
8+
{
9+
internal class FakeRealmsService : IRealmsService
10+
{
11+
private readonly List<Realm> realms = new List<Realm>();
12+
13+
public void Add(string realmName, string ownerName)
14+
{
15+
//XXX
16+
}
17+
18+
public Realm CreateRealm(Player owner, string name, RealmFactory factory)
19+
{
20+
throw new NotImplementedException();
21+
}
22+
23+
public IEnumerable<Realm> GetAllRealms()
24+
{
25+
throw new NotImplementedException();
26+
}
27+
28+
public Realm GetRealmByName(string name)
29+
{
30+
throw new NotImplementedException();
31+
}
32+
33+
public IEnumerable<RealmFactory> GetRealmFactories()
34+
{
35+
throw new NotImplementedException();
36+
}
37+
38+
public bool IsValidNameChange(string oldName, string newName)
39+
{
40+
throw new NotImplementedException();
41+
}
42+
43+
public bool TransactionalUpdate(Realm realm, Func<Realm, bool> transaction)
44+
{
45+
throw new NotImplementedException();
46+
}
47+
48+
public Task<RealmEditingOutcome> UpdateRealmSourceAsync(Realm realm, Stream bodyStream)
49+
{
50+
throw new NotImplementedException();
51+
}
52+
}
53+
}
+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProjectGuid>{420029C3-557E-42DA-965B-4E4AD3D97E9C}</ProjectGuid>
7+
<OutputType>Library</OutputType>
8+
<AppDesignerFolder>Properties</AppDesignerFolder>
9+
<RootNamespace>Guncho.Api.Tests</RootNamespace>
10+
<AssemblyName>Guncho.Api.Tests</AssemblyName>
11+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
14+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
15+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
16+
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
17+
<IsCodedUITest>False</IsCodedUITest>
18+
<TestProjectType>UnitTest</TestProjectType>
19+
</PropertyGroup>
20+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
21+
<DebugSymbols>true</DebugSymbols>
22+
<DebugType>full</DebugType>
23+
<Optimize>false</Optimize>
24+
<OutputPath>bin\Debug\</OutputPath>
25+
<DefineConstants>DEBUG;TRACE</DefineConstants>
26+
<ErrorReport>prompt</ErrorReport>
27+
<WarningLevel>4</WarningLevel>
28+
</PropertyGroup>
29+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
30+
<DebugType>pdbonly</DebugType>
31+
<Optimize>true</Optimize>
32+
<OutputPath>bin\Release\</OutputPath>
33+
<DefineConstants>TRACE</DefineConstants>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
</PropertyGroup>
37+
<ItemGroup>
38+
<Reference Include="Microsoft.Owin, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
39+
<HintPath>..\packages\Microsoft.Owin.3.0.1\lib\net45\Microsoft.Owin.dll</HintPath>
40+
<Private>True</Private>
41+
</Reference>
42+
<Reference Include="Microsoft.Owin.Hosting, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
43+
<HintPath>..\packages\Microsoft.Owin.Hosting.3.0.1\lib\net45\Microsoft.Owin.Hosting.dll</HintPath>
44+
<Private>True</Private>
45+
</Reference>
46+
<Reference Include="Microsoft.Owin.Testing, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
47+
<HintPath>..\packages\Microsoft.Owin.Testing.3.0.1\lib\net45\Microsoft.Owin.Testing.dll</HintPath>
48+
<Private>True</Private>
49+
</Reference>
50+
<Reference Include="MyTested.WebApi, Version=1.2.0.0, Culture=neutral, processorArchitecture=MSIL">
51+
<HintPath>..\packages\MyTested.WebApi.1.2.1\lib\net45\MyTested.WebApi.dll</HintPath>
52+
<Private>True</Private>
53+
</Reference>
54+
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
55+
<HintPath>..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
56+
<Private>True</Private>
57+
</Reference>
58+
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
59+
<HintPath>..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
60+
<Private>True</Private>
61+
</Reference>
62+
<Reference Include="System" />
63+
<Reference Include="System.IdentityModel" />
64+
<Reference Include="System.Net.Http" />
65+
<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
66+
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
67+
<Private>True</Private>
68+
</Reference>
69+
<Reference Include="System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
70+
<HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll</HintPath>
71+
<Private>True</Private>
72+
</Reference>
73+
<Reference Include="System.Web.Http.Owin, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
74+
<HintPath>..\packages\Microsoft.AspNet.WebApi.Owin.5.2.3\lib\net45\System.Web.Http.Owin.dll</HintPath>
75+
<Private>True</Private>
76+
</Reference>
77+
<Reference Include="Thinktecture.IdentityModel.Core, Version=1.2.0.0, Culture=neutral, processorArchitecture=MSIL">
78+
<HintPath>..\packages\Thinktecture.IdentityModel.Core.1.2.0\lib\net45\Thinktecture.IdentityModel.Core.dll</HintPath>
79+
<Private>True</Private>
80+
</Reference>
81+
<Reference Include="Thinktecture.IdentityModel.Owin.ResourceAuthorization, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
82+
<HintPath>..\packages\Thinktecture.IdentityModel.Owin.ResourceAuthorization.1.0.1\lib\net45\Thinktecture.IdentityModel.Owin.ResourceAuthorization.dll</HintPath>
83+
<Private>True</Private>
84+
</Reference>
85+
<Reference Include="Thinktecture.IdentityModel.Owin.ResourceAuthorization.WebApi, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
86+
<HintPath>..\packages\Thinktecture.IdentityModel.Owin.ResourceAuthorization.WebApi.3.0.0\lib\net45\Thinktecture.IdentityModel.Owin.ResourceAuthorization.WebApi.dll</HintPath>
87+
<Private>True</Private>
88+
</Reference>
89+
</ItemGroup>
90+
<Choose>
91+
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
92+
<ItemGroup>
93+
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
94+
</ItemGroup>
95+
</When>
96+
<Otherwise>
97+
<ItemGroup>
98+
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
99+
</ItemGroup>
100+
</Otherwise>
101+
</Choose>
102+
<ItemGroup>
103+
<Compile Include="FakePlayersService.cs" />
104+
<Compile Include="FakeRealmsService.cs" />
105+
<Compile Include="MyWebApiExtensions.cs" />
106+
<Compile Include="ProfilesTests.cs" />
107+
<Compile Include="Properties\AssemblyInfo.cs" />
108+
<Compile Include="TestRig.cs" />
109+
</ItemGroup>
110+
<ItemGroup>
111+
<ProjectReference Include="..\Guncho.Core\Guncho.Core.csproj">
112+
<Project>{358595ad-6d80-4bc2-90da-6390cafbe0aa}</Project>
113+
<Name>Guncho.Core</Name>
114+
</ProjectReference>
115+
</ItemGroup>
116+
<ItemGroup>
117+
<None Include="app.config" />
118+
<None Include="packages.config" />
119+
</ItemGroup>
120+
<Choose>
121+
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
122+
<ItemGroup>
123+
<Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
124+
<Private>False</Private>
125+
</Reference>
126+
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
127+
<Private>False</Private>
128+
</Reference>
129+
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
130+
<Private>False</Private>
131+
</Reference>
132+
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
133+
<Private>False</Private>
134+
</Reference>
135+
</ItemGroup>
136+
</When>
137+
</Choose>
138+
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
139+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
140+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
141+
Other similar extension points exist, see Microsoft.Common.targets.
142+
<Target Name="BeforeBuild">
143+
</Target>
144+
<Target Name="AfterBuild">
145+
</Target>
146+
-->
147+
</Project>
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Guncho.Api.Security;
2+
using Microsoft.Owin;
3+
using MyTested.WebApi;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Net.Http;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using System.Web.Http;
11+
using Thinktecture.IdentityModel.Owin.ResourceAuthorization;
12+
13+
namespace Guncho.Api.Tests
14+
{
15+
static class MyWebApiExtensions
16+
{
17+
public static IControllerBuilder<T> WithTestRig<T>(this IControllerBuilder<T> obj, TestRig rig)
18+
where T : ApiController
19+
{
20+
return obj
21+
.WithResolvedDependencies(rig.PlayersService)
22+
.WithSetup(c =>
23+
{
24+
var oc = new OwinContext();
25+
var auth = new GunchoResourceAuthorization(rig.PlayersService, rig.RealmsService);
26+
oc.Set(ResourceAuthorizationManagerMiddleware.Key, auth);
27+
c.Request.SetOwinContext(oc);
28+
});
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)