-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntegrationTests.cs
110 lines (94 loc) · 4.4 KB
/
IntegrationTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Json;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Principal;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Microsoft.ApplicationServer.Http;
using Microsoft.ApplicationServer.Http.Activation;
using Microsoft.ApplicationServer.Http.Description;
using Microsoft.ApplicationServer.Http.Dispatcher;
using NUnit.Framework;
namespace Waaz.Tests
{
class GivenTheTestPolicy1
{
[ServiceContract]
public class TheService
{
[WebGet(UriTemplate = "/{user}/{id}")]
public string GetOper(string user, int id){ return ""; }
[WebInvoke(Method = "POST", UriTemplate = "/{user}")]
public string PostOper(string user, JsonValue v) { return ""; }
[WebInvoke(Method = "PUT",UriTemplate = "/{user}/{id}")]
public string PutOper(string user, int id, JsonValue v) { return ""; }
[WebInvoke(Method = "DELETE", UriTemplate = "/{user}/{id}")]
public string DeleteOper(string user, int id) { return ""; }
}
private class TestPolicy1 : AuthorizationPolicyFor<TheService>
{
public TestPolicy1()
{
var admin = new Role("admin");
var root = new Role("root");
ForAll.Deny.IfAnonymous();
ForAll.Allow.IfInRoles(admin || root);
For(Get).Allow.IfAuthenticated();
// alt: For(OperationNamed("GetOper")).Allow.IfAuthenticated();
// alt: ForServiceMethod(s => s.GetOper(default(string), default(int))).Allow.IfAuthenticated();
For(Post || Put).Allow.If<string, IPrincipal>(
(user, principal) => user == principal.Identity.Name);
For(Delete).Allow.When<string,IPrincipal>(CheckUserName);
}
static bool CheckUserName(string user, IPrincipal principal)
{
return user == principal.Identity.Name;
}
}
readonly IPrincipal Anonymous = new GenericPrincipal(new GenericIdentity(""), new string[0]);
[Test]
public void When_Get_by_Anonymous_then_access_should_be_denied()
{
var ctx = new IntegrationContext<TheService>(Anonymous, new TestPolicy1());
Assert.AreEqual(HttpStatusCode.Forbidden, ctx.Send(HttpMethod.Get,"/SomeUser/1"));
}
readonly IPrincipal Alice = new GenericPrincipal(new GenericIdentity("Alice", "type"), new string[] { "user" });
[Test]
public void When_Get_by_Alice_then_access_should_be_granted()
{
var ctx = new IntegrationContext<TheService>(Alice, new TestPolicy1());
Assert.AreEqual(HttpStatusCode.OK, ctx.Send(HttpMethod.Get,"/SomeUser/1"));
}
[Test]
public void When_Delete_by_Alice_to_another_user_then_access_should_be_denied()
{
var ctx = new IntegrationContext<TheService>(Alice, new TestPolicy1());
Assert.AreEqual(HttpStatusCode.Forbidden, ctx.Send(HttpMethod.Delete,"/SomeUser/1"));
}
[Test]
public void When_Delete_by_Alice_to_its_user_then_access_should_be_granted()
{
var ctx = new IntegrationContext<TheService>(Alice, new TestPolicy1());
Assert.AreEqual(HttpStatusCode.OK, ctx.Send(HttpMethod.Delete,"/Alice/1"));
}
readonly IPrincipal Admin = new GenericPrincipal(new GenericIdentity("Alice", "type"), new string[] { "admin" });
[Test]
public void When_Delete_by_Admin_to_another_user_then_access_should_be_granted()
{
var ctx = new IntegrationContext<TheService>(Admin, new TestPolicy1());
Assert.AreEqual(HttpStatusCode.OK, ctx.Send(HttpMethod.Delete, "/Alice/1"));
}
readonly IPrincipal Root = new GenericPrincipal(new GenericIdentity("Alice", "type"), new string[] { "root" });
[Test]
public void When_Delete_by_Root_to_another_user_then_access_should_be_granted()
{
var ctx = new IntegrationContext<TheService>(Root, new TestPolicy1());
Assert.AreEqual(HttpStatusCode.OK, ctx.Send(HttpMethod.Delete, "/Alice/1"));
}
}
}