-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathResourceDefinitionTests.cs
129 lines (106 loc) · 3.82 KB
/
ResourceDefinitionTests.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using JsonApiDotNetCore.Builders;
using JsonApiDotNetCore.Internal;
using JsonApiDotNetCore.Models;
using System.Collections.Generic;
using Xunit;
namespace UnitTests.Models
{
public class ResourceDefinition_Scenario_Tests
{
private readonly IContextGraph _graph;
public ResourceDefinition_Scenario_Tests()
{
_graph = new ContextGraphBuilder()
.AddResource<Model>("models")
.Build();
}
[Fact]
public void Request_Filter_Uses_Member_Expression()
{
// arrange
var resource = new RequestFilteredResource(isAdmin: true);
// act
var attrs = resource.GetOutputAttrs(null);
// assert
Assert.Single(attrs);
Assert.Equal(nameof(Model.Password), attrs[0].InternalAttributeName);
}
[Fact]
public void Request_Filter_Uses_NewExpression()
{
// arrange
var resource = new RequestFilteredResource(isAdmin: false);
// act
var attrs = resource.GetOutputAttrs(null);
// assert
Assert.Empty(attrs);
}
[Fact]
public void Instance_Filter_Uses_Member_Expression()
{
// arrange
var model = new Model { AlwaysExcluded = "Admin" };
var resource = new InstanceFilteredResource();
// act
var attrs = resource.GetOutputAttrs(model);
// assert
Assert.Single(attrs);
Assert.Equal(nameof(Model.Password), attrs[0].InternalAttributeName);
}
[Fact]
public void Instance_Filter_Uses_NewExpression()
{
// arrange
var model = new Model { AlwaysExcluded = "Joe" };
var resource = new InstanceFilteredResource();
// act
var attrs = resource.GetOutputAttrs(model);
// assert
Assert.Empty(attrs);
}
[Fact]
public void InstanceOutputAttrsAreSpecified_Returns_True_If_Instance_Method_Is_Overriden()
{
// act
var resource = new InstanceFilteredResource();
// assert
Assert.True(resource._instanceAttrsAreSpecified);
}
[Fact]
public void InstanceOutputAttrsAreSpecified_Returns_False_If_Instance_Method_Is_Not_Overriden()
{
// act
var resource = new RequestFilteredResource(isAdmin: false);
// assert
Assert.False(resource._instanceAttrsAreSpecified);
}
}
public class Model : Identifiable
{
[Attr("name")] public string AlwaysExcluded { get; set; }
[Attr("password")] public string Password { get; set; }
}
public class RequestFilteredResource : ResourceDefinition<Model>
{
private readonly bool _isAdmin;
// this constructor will be resolved from the container
// that means you can take on any dependency that is also defined in the container
public RequestFilteredResource(bool isAdmin)
{
_isAdmin = isAdmin;
}
// Called once per filtered resource in request.
protected override List<AttrAttribute> OutputAttrs()
=> _isAdmin
? Remove(m => m.AlwaysExcluded)
: Remove(m => new { m.AlwaysExcluded, m.Password }, from: base.OutputAttrs());
}
public class InstanceFilteredResource : ResourceDefinition<Model>
{
// Called once per resource instance
protected override List<AttrAttribute> OutputAttrs(Model model)
=> model.AlwaysExcluded == "Admin"
? Remove(m => m.AlwaysExcluded, base.OutputAttrs())
: Remove(m => new { m.AlwaysExcluded, m.Password }, from: base.OutputAttrs());
}
}