Skip to content
This repository was archived by the owner on Nov 11, 2025. It is now read-only.

Commit 1542284

Browse files
committed
test: adds missing unit test definitions for security schemes in OAI 3.1 & 3.2
Signed-off-by: Vincent Biret <[email protected]>
1 parent 224648b commit 1542284

17 files changed

+318
-79
lines changed

test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiOAuthFlowTests.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using System.IO;
6+
using System.Threading.Tasks;
7+
using Microsoft.OpenApi.Reader;
8+
using Xunit;
9+
10+
namespace Microsoft.OpenApi.Readers.Tests.V31Tests
11+
{
12+
[Collection("DefaultSettings")]
13+
public class OpenApiSecuritySchemeTests
14+
{
15+
private const string SampleFolderPath = "V31Tests/Samples/OpenApiSecurityScheme/";
16+
[Fact]
17+
public async Task ParseHttpSecuritySchemeShouldSucceed()
18+
{
19+
// Act
20+
var securityScheme = await OpenApiModelFactory.LoadAsync<OpenApiSecurityScheme>(Path.Combine(SampleFolderPath, "httpSecurityScheme.yaml"), OpenApiSpecVersion.OpenApi3_1, new(), SettingsFixture.ReaderSettings);
21+
22+
// Assert
23+
Assert.Equivalent(
24+
new OpenApiSecurityScheme
25+
{
26+
Type = SecuritySchemeType.Http,
27+
Scheme = OpenApiConstants.Basic
28+
}, securityScheme);
29+
}
30+
31+
[Fact]
32+
public async Task ParseApiKeySecuritySchemeShouldSucceed()
33+
{
34+
// Act
35+
var securityScheme = await OpenApiModelFactory.LoadAsync<OpenApiSecurityScheme>(Path.Combine(SampleFolderPath, "apiKeySecurityScheme.yaml"), OpenApiSpecVersion.OpenApi3_1, new(), SettingsFixture.ReaderSettings);
36+
37+
// Assert
38+
Assert.Equivalent(
39+
new OpenApiSecurityScheme
40+
{
41+
Type = SecuritySchemeType.ApiKey,
42+
Name = "api_key",
43+
In = ParameterLocation.Header
44+
}, securityScheme);
45+
}
46+
47+
[Fact]
48+
public async Task ParseBearerSecuritySchemeShouldSucceed()
49+
{
50+
// Act
51+
var securityScheme = await OpenApiModelFactory.LoadAsync<OpenApiSecurityScheme>(Path.Combine(SampleFolderPath, "bearerSecurityScheme.yaml"), OpenApiSpecVersion.OpenApi3_1, new(), SettingsFixture.ReaderSettings);
52+
53+
// Assert
54+
Assert.Equivalent(
55+
new OpenApiSecurityScheme
56+
{
57+
Type = SecuritySchemeType.Http,
58+
Scheme = OpenApiConstants.Bearer,
59+
BearerFormat = OpenApiConstants.Jwt
60+
}, securityScheme);
61+
}
62+
63+
[Fact]
64+
public async Task ParseOAuth2SecuritySchemeShouldSucceed()
65+
{
66+
// Act
67+
var securityScheme = await OpenApiModelFactory.LoadAsync<OpenApiSecurityScheme>(Path.Combine(SampleFolderPath, "oauth2SecurityScheme.yaml"), OpenApiSpecVersion.OpenApi3_1, new(), SettingsFixture.ReaderSettings);
68+
69+
// Assert
70+
Assert.Equivalent(
71+
new OpenApiSecurityScheme
72+
{
73+
Type = SecuritySchemeType.OAuth2,
74+
Flows = new OpenApiOAuthFlows
75+
{
76+
Implicit = new OpenApiOAuthFlow
77+
{
78+
AuthorizationUrl = new Uri("https://example.com/api/oauth/dialog"),
79+
Scopes = new System.Collections.Generic.Dictionary<string, string>
80+
{
81+
["write:pets"] = "modify pets in your account",
82+
["read:pets"] = "read your pets"
83+
}
84+
}
85+
}
86+
}, securityScheme);
87+
}
88+
89+
[Fact]
90+
public async Task ParseOpenIdConnectSecuritySchemeShouldSucceed()
91+
{
92+
// Act
93+
var securityScheme = await OpenApiModelFactory.LoadAsync<OpenApiSecurityScheme>(Path.Combine(SampleFolderPath, "openIdConnectSecurityScheme.yaml"), OpenApiSpecVersion.OpenApi3_1, new(), SettingsFixture.ReaderSettings);
94+
95+
// Assert
96+
Assert.Equivalent(
97+
new OpenApiSecurityScheme
98+
{
99+
Type = SecuritySchemeType.OpenIdConnect,
100+
Description = "Sample Description",
101+
OpenIdConnectUrl = new Uri("http://www.example.com")
102+
}, securityScheme);
103+
}
104+
105+
[Fact]
106+
public async Task ParseOAuth2SecuritySchemeWithDeviceAuthorizationUrlShouldSucceed()
107+
{
108+
// Act
109+
var securityScheme = await OpenApiModelFactory.LoadAsync<OpenApiSecurityScheme>(
110+
Path.Combine(SampleFolderPath, "oauth2SecuritySchemeWithDeviceUrl.yaml"),
111+
OpenApiSpecVersion.OpenApi3_1,
112+
new(),
113+
SettingsFixture.ReaderSettings);
114+
115+
// Assert
116+
Assert.NotNull(securityScheme);
117+
Assert.Equal(SecuritySchemeType.OAuth2, securityScheme.Type);
118+
Assert.NotNull(securityScheme.Flows?.AuthorizationCode);
119+
Assert.Equal(new Uri("https://example.com/api/oauth/dialog"), securityScheme.Flows.AuthorizationCode.AuthorizationUrl);
120+
Assert.Equal(new Uri("https://example.com/api/oauth/token"), securityScheme.Flows.AuthorizationCode.TokenUrl);
121+
Assert.Equal(new Uri("https://example.com/api/oauth/device"), securityScheme.Flows.AuthorizationCode.DeviceAuthorizationUrl);
122+
Assert.NotNull(securityScheme.Flows.AuthorizationCode.Scopes);
123+
Assert.Equal(2, securityScheme.Flows.AuthorizationCode.Scopes.Count);
124+
}
125+
}
126+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#securitySchemeObject
2+
type: apiKey
3+
name: api_key
4+
in: header
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#securitySchemeObject
2+
type: http
3+
scheme: bearer
4+
bearerFormat: JWT
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#securitySchemeObject
2+
type: http
3+
scheme: basic
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#securitySchemeObject
2+
type: oauth2
3+
flows:
4+
implicit:
5+
authorizationUrl: https://example.com/api/oauth/dialog
6+
scopes:
7+
write:pets: modify pets in your account
8+
read:pets: read your pets
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type: oauth2
2+
flows:
3+
authorizationCode:
4+
authorizationUrl: https://example.com/api/oauth/dialog
5+
tokenUrl: https://example.com/api/oauth/token
6+
x-oai-deviceAuthorizationUrl: https://example.com/api/oauth/device
7+
scopes:
8+
write:pets: modify pets in your account
9+
read:pets: read your pets
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type: openIdConnect
2+
description: Sample Description
3+
openIdConnectUrl: http://www.example.com

test/Microsoft.OpenApi.Readers.Tests/V32Tests/OpenApiOAuthFlowTests.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using System.IO;
6+
using System.Threading.Tasks;
7+
using Microsoft.OpenApi.Reader;
8+
using Xunit;
9+
10+
namespace Microsoft.OpenApi.Readers.Tests.V32Tests
11+
{
12+
[Collection("DefaultSettings")]
13+
public class OpenApiSecuritySchemeTests
14+
{
15+
private const string SampleFolderPath = "V32Tests/Samples/OpenApiSecurityScheme/";
16+
[Fact]
17+
public async Task ParseHttpSecuritySchemeShouldSucceed()
18+
{
19+
// Act
20+
var securityScheme = await OpenApiModelFactory.LoadAsync<OpenApiSecurityScheme>(Path.Combine(SampleFolderPath, "httpSecurityScheme.yaml"), OpenApiSpecVersion.OpenApi3_2, new(), SettingsFixture.ReaderSettings);
21+
22+
// Assert
23+
Assert.Equivalent(
24+
new OpenApiSecurityScheme
25+
{
26+
Type = SecuritySchemeType.Http,
27+
Scheme = OpenApiConstants.Basic
28+
}, securityScheme);
29+
}
30+
31+
[Fact]
32+
public async Task ParseApiKeySecuritySchemeShouldSucceed()
33+
{
34+
// Act
35+
var securityScheme = await OpenApiModelFactory.LoadAsync<OpenApiSecurityScheme>(Path.Combine(SampleFolderPath, "apiKeySecurityScheme.yaml"), OpenApiSpecVersion.OpenApi3_2, new(), SettingsFixture.ReaderSettings);
36+
37+
// Assert
38+
Assert.Equivalent(
39+
new OpenApiSecurityScheme
40+
{
41+
Type = SecuritySchemeType.ApiKey,
42+
Name = "api_key",
43+
In = ParameterLocation.Header
44+
}, securityScheme);
45+
}
46+
47+
[Fact]
48+
public async Task ParseBearerSecuritySchemeShouldSucceed()
49+
{
50+
// Act
51+
var securityScheme = await OpenApiModelFactory.LoadAsync<OpenApiSecurityScheme>(Path.Combine(SampleFolderPath, "bearerSecurityScheme.yaml"), OpenApiSpecVersion.OpenApi3_2, new(), SettingsFixture.ReaderSettings);
52+
53+
// Assert
54+
Assert.Equivalent(
55+
new OpenApiSecurityScheme
56+
{
57+
Type = SecuritySchemeType.Http,
58+
Scheme = OpenApiConstants.Bearer,
59+
BearerFormat = OpenApiConstants.Jwt
60+
}, securityScheme);
61+
}
62+
63+
[Fact]
64+
public async Task ParseOAuth2SecuritySchemeShouldSucceed()
65+
{
66+
// Act
67+
var securityScheme = await OpenApiModelFactory.LoadAsync<OpenApiSecurityScheme>(Path.Combine(SampleFolderPath, "oauth2SecurityScheme.yaml"), OpenApiSpecVersion.OpenApi3_2, new(), SettingsFixture.ReaderSettings);
68+
69+
// Assert
70+
Assert.Equivalent(
71+
new OpenApiSecurityScheme
72+
{
73+
Type = SecuritySchemeType.OAuth2,
74+
Flows = new OpenApiOAuthFlows
75+
{
76+
Implicit = new OpenApiOAuthFlow
77+
{
78+
AuthorizationUrl = new Uri("https://example.com/api/oauth/dialog"),
79+
Scopes = new System.Collections.Generic.Dictionary<string, string>
80+
{
81+
["write:pets"] = "modify pets in your account",
82+
["read:pets"] = "read your pets"
83+
}
84+
}
85+
}
86+
}, securityScheme);
87+
}
88+
89+
[Fact]
90+
public async Task ParseOpenIdConnectSecuritySchemeShouldSucceed()
91+
{
92+
// Act
93+
var securityScheme = await OpenApiModelFactory.LoadAsync<OpenApiSecurityScheme>(Path.Combine(SampleFolderPath, "openIdConnectSecurityScheme.yaml"), OpenApiSpecVersion.OpenApi3_2, new(), SettingsFixture.ReaderSettings);
94+
95+
// Assert
96+
Assert.Equivalent(
97+
new OpenApiSecurityScheme
98+
{
99+
Type = SecuritySchemeType.OpenIdConnect,
100+
Description = "Sample Description",
101+
OpenIdConnectUrl = new Uri("http://www.example.com")
102+
}, securityScheme);
103+
}
104+
105+
[Fact]
106+
public async Task ParseOAuth2SecuritySchemeWithDeviceAuthorizationUrlShouldSucceed()
107+
{
108+
// Act
109+
var securityScheme = await OpenApiModelFactory.LoadAsync<OpenApiSecurityScheme>(
110+
Path.Combine(SampleFolderPath, "oauth2SecuritySchemeWithDeviceUrl.yaml"),
111+
OpenApiSpecVersion.OpenApi3_2,
112+
new(),
113+
SettingsFixture.ReaderSettings);
114+
115+
// Assert
116+
Assert.NotNull(securityScheme);
117+
Assert.Equal(SecuritySchemeType.OAuth2, securityScheme.Type);
118+
Assert.NotNull(securityScheme.Flows?.AuthorizationCode);
119+
Assert.Equal(new Uri("https://example.com/api/oauth/dialog"), securityScheme.Flows.AuthorizationCode.AuthorizationUrl);
120+
Assert.Equal(new Uri("https://example.com/api/oauth/token"), securityScheme.Flows.AuthorizationCode.TokenUrl);
121+
Assert.Equal(new Uri("https://example.com/api/oauth/device"), securityScheme.Flows.AuthorizationCode.DeviceAuthorizationUrl);
122+
Assert.NotNull(securityScheme.Flows.AuthorizationCode.Scopes);
123+
Assert.Equal(2, securityScheme.Flows.AuthorizationCode.Scopes.Count);
124+
}
125+
126+
}
127+
}

0 commit comments

Comments
 (0)