1
+ using System . Threading . Tasks ;
2
+ using FluentAssertions ;
3
+ using Microsoft . AspNetCore . Authorization ;
4
+ using Microsoft . AspNetCore . Http ;
5
+ using Microsoft . AspNetCore . Mvc ;
6
+ using Microsoft . Azure . WebJobs ;
7
+ using Microsoft . Azure . WebJobs . Extensions . Http ;
8
+ using Microsoft . Extensions . Logging ;
9
+ using NUnit . Framework ;
10
+
11
+ namespace AzureFunctions . Extensions . OpenIDConnect . Tests
12
+ {
13
+ using System ;
14
+ using System . Collections . Generic ;
15
+
16
+ [ TestFixture ]
17
+ public class RouteGuardianShould
18
+ {
19
+ [ Test ]
20
+ public async Task Not_Authorize_When_Not_HttpTrigger ( )
21
+ {
22
+ // Arrange
23
+ var guardian = new RouteGuardian ( ( ) => new List < Type > { typeof ( Not_HttpTrigger ) } ) ;
24
+
25
+ // Act
26
+ var result = await guardian . ShouldAuthorize ( "Not_HttpTrigger" ) ;
27
+
28
+ // Assert
29
+ result . Should ( ) . Be ( false ) ;
30
+ }
31
+
32
+ [ Test ]
33
+ public async Task Not_Authorize_When_No_Authorize_Attribute_On_Method_And_Type ( )
34
+ {
35
+ // Arrange
36
+ var guardian = new RouteGuardian ( ( ) => new List < Type > { typeof ( No_Authorize_Attribute_On_Method_And_Type ) } ) ;
37
+
38
+ // Act
39
+ var result = await guardian . ShouldAuthorize ( "No_Authorize_Attribute_On_Method_And_Type" ) ;
40
+
41
+ // Assert
42
+ result . Should ( ) . Be ( false ) ;
43
+ }
44
+
45
+ [ Test ]
46
+ public async Task Authorize_When_Authorize_Attribute_Is_On_Method ( )
47
+ {
48
+ // Arrange
49
+ var guardian = new RouteGuardian ( ( ) => new List < Type > { typeof ( Authorize_Attribute_Is_On_Method ) } ) ;
50
+
51
+ // Act
52
+ var result = await guardian . ShouldAuthorize ( "Authorize_Attribute_Is_On_Method" ) ;
53
+
54
+ // Assert
55
+ result . Should ( ) . Be ( true ) ;
56
+ }
57
+
58
+ [ Test ]
59
+ public async Task Authorize_When_Authorize_Attribute_Is_On_Class ( )
60
+ {
61
+ // Arrange
62
+ var guardian = new RouteGuardian ( ( ) => new List < Type > { typeof ( Authorize_Attribute_Is_On_Class ) } ) ;
63
+
64
+ // Act
65
+ var result = await guardian . ShouldAuthorize ( "Authorize_Attribute_Is_On_Class" ) ;
66
+
67
+ // Assert
68
+ result . Should ( ) . Be ( true ) ;
69
+ }
70
+
71
+ [ Test ]
72
+ public async Task NotAuthorize_When_Authorize_Attribute_Is_On_Class_But_AllowAnonimous_On_Method ( )
73
+ {
74
+ // Arrange
75
+ var guardian = new RouteGuardian ( ( ) => new List < Type > { typeof ( Attribute_Is_On_Class_But_AllowAnonimous_On_Method ) } ) ;
76
+
77
+ // Act
78
+ var result = await guardian . ShouldAuthorize ( "Attribute_Is_On_Class_But_AllowAnonimous_On_Method" ) ;
79
+
80
+ // Assert
81
+ result . Should ( ) . Be ( false ) ;
82
+ }
83
+
84
+
85
+
86
+ internal class Not_HttpTrigger
87
+ {
88
+ [ Authorize ]
89
+ [ FunctionName ( "Not_HttpTrigger" ) ]
90
+ public IActionResult Run ( HttpRequest req , ILogger log )
91
+ {
92
+ var responseMessage = "Hello. This HTTP triggered function is protected." ;
93
+
94
+ return new OkObjectResult ( responseMessage ) ;
95
+ }
96
+ }
97
+
98
+ internal class No_Authorize_Attribute_On_Method_And_Type
99
+ {
100
+ [ FunctionName ( "No_Authorize_Attribute_On_Method_And_Type" ) ]
101
+ public IActionResult Run (
102
+ [ HttpTrigger ( AuthorizationLevel . Anonymous , "get" , "post" , Route = null ) ] HttpRequest req , ILogger log )
103
+ {
104
+ var responseMessage = "Hello. This HTTP triggered function is protected." ;
105
+
106
+ return new OkObjectResult ( responseMessage ) ;
107
+ }
108
+ }
109
+
110
+ internal class Authorize_Attribute_Is_On_Method
111
+ {
112
+ [ Authorize ]
113
+ [ FunctionName ( "Authorize_Attribute_Is_On_Method" ) ]
114
+ public IActionResult Run (
115
+ [ HttpTrigger ( AuthorizationLevel . Anonymous , "get" , "post" , Route = null ) ] HttpRequest req , ILogger log )
116
+ {
117
+ var responseMessage = "Hello. This HTTP triggered function is protected." ;
118
+
119
+ return new OkObjectResult ( responseMessage ) ;
120
+ }
121
+ }
122
+
123
+ [ Authorize ]
124
+ internal class Authorize_Attribute_Is_On_Class
125
+ {
126
+ [ FunctionName ( "Authorize_Attribute_Is_On_Class" ) ]
127
+ public IActionResult Run (
128
+ [ HttpTrigger ( AuthorizationLevel . Anonymous , "get" , "post" , Route = null ) ] HttpRequest req , ILogger log )
129
+ {
130
+ var responseMessage = "Hello. This HTTP triggered function is protected." ;
131
+
132
+ return new OkObjectResult ( responseMessage ) ;
133
+ }
134
+ }
135
+
136
+ [ Authorize ]
137
+ internal class Attribute_Is_On_Class_But_AllowAnonimous_On_Method
138
+ {
139
+ [ AllowAnonymous ]
140
+ [ FunctionName ( "Attribute_Is_On_Class_But_AllowAnonimous_On_Method" ) ]
141
+ public IActionResult Run (
142
+ [ HttpTrigger ( AuthorizationLevel . Anonymous , "get" , "post" , Route = null ) ] HttpRequest req , ILogger log )
143
+ {
144
+ var responseMessage = "Hello. This HTTP triggered function is protected." ;
145
+
146
+ return new OkObjectResult ( responseMessage ) ;
147
+ }
148
+ }
149
+ }
150
+ }
0 commit comments