Browse Source

Port the AspNet.Security.OpenIdConnect.Primitives tests

pull/847/head
IgorHrabrov 6 years ago
committed by Kévin Chalet
parent
commit
ee2e139ae5
  1. 322
      test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictConverterTests.cs
  2. 915
      test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictExtensionsTests.cs
  3. 450
      test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictMessageTests.cs
  4. 1026
      test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictParameterTests.cs
  5. 334
      test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictRequestTests.cs
  6. 137
      test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictResponseTests.cs

322
test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictConverterTests.cs

@ -0,0 +1,322 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/openiddict/openiddict-core for more information concerning
* the license and the contributors participating to this project.
*/
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Xunit;
namespace OpenIddict.Abstractions.Tests.Primitives
{
public class OpenIddictConverterTests
{
[Fact]
public void CanConvert_ThrowsAnExceptionForNullType()
{
// Arrange
var converter = new OpenIddictConverter();
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() => converter.CanConvert(type: null));
Assert.Equal("type", exception.ParamName);
}
[Theory]
[InlineData(typeof(OpenIddictMessage), true)]
[InlineData(typeof(OpenIddictRequest), true)]
[InlineData(typeof(OpenIddictResponse), true)]
[InlineData(typeof(OpenIddictMessage[]), false)]
[InlineData(typeof(OpenIddictRequest[]), false)]
[InlineData(typeof(OpenIddictResponse[]), false)]
[InlineData(typeof(OpenIddictParameter), false)]
[InlineData(typeof(OpenIddictParameter?), false)]
[InlineData(typeof(OpenIddictParameter[]), false)]
[InlineData(typeof(OpenIddictParameter?[]), false)]
[InlineData(typeof(object), false)]
[InlineData(typeof(long), false)]
public void CanConvert_ReturnsExpectedResult(Type type, bool result)
{
// Arrange
var converter = new OpenIddictConverter();
// Act and assert
Assert.Equal(result, converter.CanConvert(type));
}
[Fact]
public void ReadJson_ThrowsAnExceptionForNullReader()
{
// Arrange
var converter = new OpenIddictConverter();
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() =>
{
converter.ReadJson(reader: null, type: null, value: null, serializer: null);
});
Assert.Equal("reader", exception.ParamName);
}
[Fact]
public void ReadJson_ThrowsAnExceptionForNullType()
{
// Arrange
var converter = new OpenIddictConverter();
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() =>
{
converter.ReadJson(reader: new JsonTextReader(TextReader.Null), type: null, value: null, serializer: null);
});
Assert.Equal("type", exception.ParamName);
}
[Fact]
public void ReadJson_ThrowsAnExceptionForUnexpectedJsonToken()
{
// Arrange
var converter = new OpenIddictConverter();
using var reader = new JsonTextReader(new StringReader("[0,1,2,3]"));
// Act and assert
var exception = Assert.Throws<JsonSerializationException>(() =>
{
converter.ReadJson(reader: reader, type: typeof(OpenIddictRequest), value: null, serializer: null);
});
Assert.Equal("An error occurred while reading the JSON payload.", exception.Message);
}
[Theory]
[InlineData(typeof(OpenIddictMessage[]))]
[InlineData(typeof(OpenIddictRequest[]))]
[InlineData(typeof(OpenIddictResponse[]))]
[InlineData(typeof(OpenIddictParameter))]
[InlineData(typeof(OpenIddictParameter?))]
[InlineData(typeof(OpenIddictParameter[]))]
[InlineData(typeof(OpenIddictParameter?[]))]
[InlineData(typeof(object))]
[InlineData(typeof(long))]
public void ReadJson_ThrowsAnExceptionForUnsupportedType(Type type)
{
// Arrange
var converter = new OpenIddictConverter();
using var reader = new JsonTextReader(new StringReader(@"{""name"":""value""}"));
// Act and assert
var exception = Assert.Throws<ArgumentException>(() =>
{
converter.ReadJson(reader, type, value: null, serializer: null);
});
Assert.StartsWith("The specified type is not supported.", exception.Message);
Assert.Equal("type", exception.ParamName);
}
[Fact]
public void ReadJson_PopulatesExistingInstance()
{
// Arrange
var message = new OpenIddictMessage();
var converter = new OpenIddictConverter();
var reader = new JsonTextReader(new StringReader(@"{""name"":""value""}"));
// Act
var result = converter.ReadJson(reader: reader, type: typeof(OpenIddictMessage), value: message, serializer: null);
// Assert
Assert.Same(message, result);
Assert.Equal("value", message.GetParameter("name"));
}
[Theory]
[InlineData(typeof(OpenIddictMessage))]
[InlineData(typeof(OpenIddictRequest))]
[InlineData(typeof(OpenIddictResponse))]
public void ReadJson_ReturnsRequestedType(Type type)
{
// Arrange
var converter = new OpenIddictConverter();
var reader = new JsonTextReader(new StringReader(@"{""name"":""value""}"));
// Act
var result = (OpenIddictMessage)converter.ReadJson(reader, type, value: null, serializer: null);
// Assert
Assert.IsType(type, result);
Assert.Equal("value", result.GetParameter("name"));
}
[Fact]
public void ReadJson_PreservesNullParameters()
{
// Arrange
var converter = new OpenIddictConverter();
var reader = new JsonTextReader(
new StringReader(@"{""string"":null,""bool"":null,""long"":null,""array"":null,""object"":null}"));
// Act
var result = (OpenIddictMessage)converter.ReadJson(reader, typeof(OpenIddictMessage), value: null, serializer: null);
// Assert
Assert.Equal(5, result.GetParameters().Count());
Assert.NotNull(result.GetParameter("string"));
Assert.NotNull(result.GetParameter("bool"));
Assert.NotNull(result.GetParameter("long"));
Assert.NotNull(result.GetParameter("array"));
Assert.NotNull(result.GetParameter("object"));
Assert.Null((string)result.GetParameter("string"));
Assert.Null((bool?)result.GetParameter("bool"));
Assert.Null((long?)result.GetParameter("long"));
Assert.Null((JArray)result.GetParameter("array"));
Assert.Null((JObject)result.GetParameter("object"));
}
[Fact]
public void ReadJson_PreservesEmptyParameters()
{
// Arrange
var converter = new OpenIddictConverter();
var reader = new JsonTextReader(new StringReader(@"{""string"":"""",""array"":[],""object"":{}}"));
// Act
var result = (OpenIddictMessage)converter.ReadJson(reader, typeof(OpenIddictMessage), value: null, serializer: null);
// Assert
Assert.Equal(3, result.GetParameters().Count());
Assert.NotNull(result.GetParameter("string"));
Assert.NotNull(result.GetParameter("array"));
Assert.NotNull(result.GetParameter("object"));
Assert.Empty((string)result.GetParameter("string"));
Assert.Empty((JArray)result.GetParameter("array"));
Assert.Empty((JObject)result.GetParameter("object"));
}
[Fact]
public void WriteJson_ThrowsAnExceptionForNullWriter()
{
// Arrange
var converter = new OpenIddictConverter();
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() =>
{
converter.WriteJson(writer: null, value: null, serializer: null);
});
Assert.Equal("writer", exception.ParamName);
}
[Fact]
public void WriteJson_ThrowsAnExceptionForNullValue()
{
// Arrange
var converter = new OpenIddictConverter();
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() =>
{
converter.WriteJson(writer: new JsonTextWriter(TextWriter.Null), value: null, serializer: null);
});
Assert.Equal("value", exception.ParamName);
}
[Fact]
public void WriteJson_ThrowsAnExceptionForInvalidValue()
{
// Arrange
var converter = new OpenIddictConverter();
// Act and assert
var exception = Assert.Throws<ArgumentException>(() =>
{
converter.WriteJson(writer: new JsonTextWriter(TextWriter.Null), value: new object(), serializer: null);
});
Assert.StartsWith("The specified object is not supported.", exception.Message);
Assert.Equal("value", exception.ParamName);
}
[Fact]
public void WriteJson_WritesEmptyPayloadForEmptyMessages()
{
// Arrange
var message = new OpenIddictMessage();
var converter = new OpenIddictConverter();
var writer = new StringWriter(CultureInfo.InvariantCulture);
// Act
converter.WriteJson(writer: new JsonTextWriter(writer), value: message, serializer: null);
Assert.Equal("{}", writer.ToString());
}
[Fact]
public void WriteJson_PreservesNullParameters()
{
// Arrange
var converter = new OpenIddictConverter();
var writer = new StringWriter(CultureInfo.InvariantCulture);
var message = new OpenIddictMessage();
message.AddParameter("string", new OpenIddictParameter((string)null));
message.AddParameter("bool", new OpenIddictParameter((bool?)null));
message.AddParameter("long", new OpenIddictParameter((long?)null));
message.AddParameter("array", new OpenIddictParameter((JArray)null));
message.AddParameter("object", new OpenIddictParameter((JObject)null));
// Act
converter.WriteJson(writer: new JsonTextWriter(writer), value: message, serializer: null);
// Assert
Assert.Equal(@"{""string"":null,""bool"":null,""long"":null,""array"":null,""object"":null}", writer.ToString());
}
[Fact]
public void WriteJson_PreservesEmptyParameters()
{
// Arrange
var converter = new OpenIddictConverter();
var writer = new StringWriter(CultureInfo.InvariantCulture);
var message = new OpenIddictMessage();
message.AddParameter("string", new OpenIddictParameter(string.Empty));
message.AddParameter("array", new OpenIddictParameter(new JArray()));
message.AddParameter("object", new OpenIddictParameter(new JObject()));
// Act
converter.WriteJson(writer: new JsonTextWriter(writer), value: message, serializer: null);
// Assert
Assert.Equal(@"{""string"":"""",""array"":[],""object"":{}}", writer.ToString());
}
[Fact]
public void WriteJson_WritesExpectedPayload()
{
// Arrange
var converter = new OpenIddictConverter();
var writer = new StringWriter(CultureInfo.InvariantCulture);
var message = new OpenIddictMessage();
message.AddParameter("string", "value");
message.AddParameter("array", new JArray("value"));
// Act
converter.WriteJson(writer: new JsonTextWriter(writer), value: message, serializer: null);
// Assert
Assert.Equal(@"{""string"":""value"",""array"":[""value""]}", writer.ToString());
}
}
}

915
test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictExtensionsTests.cs

@ -0,0 +1,915 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/openiddict/openiddict-core for more information concerning
* the license and the contributors participating to this project.
*/
using System;
using Xunit;
namespace OpenIddict.Abstractions.Tests.Primitives
{
public class OpenIdConnectExtensionsTests
{
[Fact]
public void GetAcrValues_ThrowsAnExceptionForNullRequest()
{
// Arrange
var request = (OpenIddictRequest) null;
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() => request.GetAcrValues());
Assert.Equal("request", exception.ParamName);
}
[Theory]
[InlineData(null, new string[0])]
[InlineData("mod-pr", new[] { "mod-pr" })]
[InlineData("mod-pr ", new[] { "mod-pr" })]
[InlineData(" mod-pr ", new[] { "mod-pr" })]
[InlineData("mod-pr mod-mf", new[] { "mod-pr", "mod-mf" })]
[InlineData("mod-pr mod-mf", new[] { "mod-pr", "mod-mf" })]
[InlineData("mod-pr mod-mf ", new[] { "mod-pr", "mod-mf" })]
[InlineData(" mod-pr mod-mf", new[] { "mod-pr", "mod-mf" })]
[InlineData("mod-pr mod-pr mod-mf", new[] { "mod-pr", "mod-mf" })]
[InlineData("mod-pr MOD-PR mod-mf", new[] { "mod-pr", "MOD-PR", "mod-mf" })]
public void GetAcrValues_ReturnsExpectedAcrValues(string value, string[] values)
{
// Arrange
var request = new OpenIddictRequest
{
AcrValues = value
};
// Act
var actualValues = request.GetAcrValues();
// Assert
Assert.Equal(values.Length, actualValues.Count);
foreach (var val in actualValues)
{
Assert.Contains(val, values);
}
}
[Fact]
public void GetResponseTypes_ThrowsAnExceptionForNullRequest()
{
// Arrange
var request = (OpenIddictRequest)null;
// Act
var exception = Assert.Throws<ArgumentNullException>(() => request.GetResponseTypes());
// Assert
Assert.Equal("request", exception.ParamName);
}
[Theory]
[InlineData(null, new string[0])]
[InlineData("code", new[] { "code" })]
[InlineData("code ", new[] { "code" })]
[InlineData(" code ", new[] { "code" })]
[InlineData("code id_token", new[] { "code", "id_token" })]
[InlineData("code id_token", new[] { "code", "id_token" })]
[InlineData("code id_token ", new[] { "code", "id_token" })]
[InlineData(" code id_token", new[] { "code", "id_token" })]
[InlineData("code code id_token", new[] { "code", "id_token" })]
[InlineData("code CODE id_token", new[] { "code", "CODE", "id_token" })]
public void GetResponseTypes_ReturnsExpectedResponseTypes(string value, string[] responseTypes)
{
// Arrange
var request = new OpenIddictRequest
{
ResponseType = value
};
// Act
var actualResponseTypes = request.GetResponseTypes();
// Assert
Assert.Equal(responseTypes.Length, actualResponseTypes.Count);
foreach (var rt in actualResponseTypes)
{
Assert.Contains(rt, responseTypes);
}
}
[Fact]
public void GetScopes_ThrowsAnExceptionForNullRequest()
{
// Arrange
var request = (OpenIddictRequest) null;
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() => request.GetScopes());
Assert.Equal("request", exception.ParamName);
}
[Theory]
[InlineData(null, new string[0])]
[InlineData("openid", new[] { "openid" })]
[InlineData("openid ", new[] { "openid" })]
[InlineData(" openid ", new[] { "openid" })]
[InlineData("openid profile", new[] { "openid", "profile" })]
[InlineData("openid profile", new[] { "openid", "profile" })]
[InlineData("openid profile ", new[] { "openid", "profile" })]
[InlineData(" openid profile", new[] { "openid", "profile" })]
[InlineData("openid openid profile", new[] { "openid", "profile" })]
[InlineData("openid OPENID profile", new[] { "openid", "OPENID", "profile" })]
public void GetScopes_ReturnsExpectedScopes(string scope, string[] scopes)
{
// Arrange
var request = new OpenIddictRequest
{
Scope = scope
};
// Act
var actualScopes = request.GetScopes();
// Assert
Assert.Equal(scopes.Length, actualScopes.Count);
foreach (var scp in actualScopes)
{
Assert.Contains(scp, scopes);
}
}
[Fact]
public void HasAcrValue_ThrowsAnExceptionForNullRequest()
{
// Arrange
var request = (OpenIddictRequest) null;
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() => request.HasAcrValue("mod-mf"));
Assert.Equal("request", exception.ParamName);
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void HasAcrValue_ThrowsAnExceptionForNullOrEmptyAcrValue(string value)
{
// Arrange
var request = new OpenIddictRequest();
// Act and assert
var exception = Assert.Throws<ArgumentException>(() => request.HasAcrValue(value));
Assert.Equal("value", exception.ParamName);
Assert.StartsWith("The value cannot be null or empty.", exception.Message);
}
[Theory]
[InlineData(null, false)]
[InlineData("mod-mf", true)]
[InlineData("mod-mf mod-pr", true)]
[InlineData(" mod-mf mod-pr", true)]
[InlineData("mod-pr mod-mf", true)]
[InlineData("mod-pr mod-mf ", true)]
[InlineData("mod-pr mod-mf mod-cstm", true)]
[InlineData("mod-pr mod-mf mod-cstm ", true)]
[InlineData("mod-pr mod-mf mod-cstm ", true)]
[InlineData("mod-pr", false)]
[InlineData("mod-pr mod-cstm", false)]
[InlineData("MOD-MF", false)]
[InlineData("MOD-MF MOD-PR", false)]
[InlineData(" MOD-MF MOD-PR", false)]
[InlineData("MOD-PR MOD-MF", false)]
[InlineData("MOD-PR MOD-MF ", false)]
[InlineData("MOD-PR MOD-MF MOD-CSTM", false)]
[InlineData("MOD-PR MOD-MF MOD-CSTM ", false)]
[InlineData("MOD-PR MOD-MF MOD-CSTM ", false)]
[InlineData("MOD-PR", false)]
[InlineData("MOD-PR MOD-CSTM", false)]
public void HasAcrValue_ReturnsExpectedResult(string value, bool result)
{
// Arrange
var request = new OpenIddictRequest
{
AcrValues = value
};
// Act and assert
Assert.Equal(result, request.HasAcrValue("mod-mf"));
}
[Fact]
public void HasPrompt_ThrowsAnExceptionForNullRequest()
{
// Arrange
var request = (OpenIddictRequest) null;
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() =>
{
request.HasPrompt(OpenIddictConstants.Prompts.Consent);
});
Assert.Equal("request", exception.ParamName);
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void HasPrompt_ThrowsAnExceptionForNullOrEmptyPrompt(string prompt)
{
// Arrange
var request = new OpenIddictRequest();
// Act and assert
var exception = Assert.Throws<ArgumentException>(() => request.HasPrompt(prompt));
Assert.Equal("prompt", exception.ParamName);
Assert.StartsWith("The prompt cannot be null or empty.", exception.Message);
}
[Theory]
[InlineData(null, false)]
[InlineData("consent", true)]
[InlineData("consent login", true)]
[InlineData(" consent login", true)]
[InlineData("login consent", true)]
[InlineData("login consent ", true)]
[InlineData("login consent select_account", true)]
[InlineData("login consent select_account ", true)]
[InlineData("login consent select_account ", true)]
[InlineData("login", false)]
[InlineData("login select_account", false)]
[InlineData("CONSENT", false)]
[InlineData("CONSENT LOGIN", false)]
[InlineData(" CONSENT LOGIN", false)]
[InlineData("LOGIN CONSENT", false)]
[InlineData("LOGIN CONSENT ", false)]
[InlineData("LOGIN CONSENT SELECT_ACCOUNT", false)]
[InlineData("LOGIN CONSENT SELECT_ACCOUNT ", false)]
[InlineData("LOGIN CONSENT SELECT_ACCOUNT ", false)]
[InlineData("LOGIN", false)]
[InlineData("LOGIN SELECT_ACCOUNT", false)]
public void HasPrompt_ReturnsExpectedResult(string prompt, bool result)
{
// Arrange
var request = new OpenIddictRequest
{
Prompt = prompt
};
// Act and assert
Assert.Equal(result, request.HasPrompt(OpenIddictConstants.Prompts.Consent));
}
[Fact]
public void HasResponseType_ThrowsAnExceptionForNullRequest()
{
// Arrange
var request = (OpenIddictRequest) null;
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() =>
{
request.HasResponseType(OpenIddictConstants.ResponseTypes.Code);
});
Assert.Equal("request", exception.ParamName);
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void HasResponseType_ThrowsAnExceptionForNullOrEmptyResponseType(string type)
{
// Arrange
var request = new OpenIddictRequest();
// Act and assert
var exception = Assert.Throws<ArgumentException>(() => request.HasResponseType(type));
Assert.Equal("type", exception.ParamName);
Assert.StartsWith("The response type cannot be null or empty.", exception.Message);
}
[Theory]
[InlineData(null, false)]
[InlineData("code", true)]
[InlineData("code id_token", true)]
[InlineData(" code id_token", true)]
[InlineData("id_token code", true)]
[InlineData("id_token code ", true)]
[InlineData("id_token code token", true)]
[InlineData("id_token code token ", true)]
[InlineData("id_token code token ", true)]
[InlineData("id_token", false)]
[InlineData("id_token token", false)]
[InlineData("CODE", false)]
[InlineData("CODE ID_TOKEN", false)]
[InlineData(" CODE ID_TOKEN", false)]
[InlineData("ID_TOKEN CODE", false)]
[InlineData("ID_TOKEN CODE ", false)]
[InlineData("ID_TOKEN CODE TOKEN", false)]
[InlineData("ID_TOKEN CODE TOKEN ", false)]
[InlineData("ID_TOKEN CODE TOKEN ", false)]
[InlineData("ID_TOKEN", false)]
[InlineData("ID_TOKEN TOKEN", false)]
public void HasResponseType_ReturnsExpectedResult(string type, bool result)
{
// Arrange
var request = new OpenIddictRequest
{
ResponseType = type
};
// Act and assert
Assert.Equal(result, request.HasResponseType(OpenIddictConstants.ResponseTypes.Code));
}
[Fact]
public void HasScope_ThrowsAnExceptionForNullRequest()
{
// Arrange
var request = (OpenIddictRequest) null;
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() =>
{
request.HasScope(OpenIddictConstants.Scopes.OpenId);
});
Assert.Equal("request", exception.ParamName);
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void HasScope_ThrowsAnExceptionForNullOrEmptyScope(string scope)
{
// Arrange
var request = new OpenIddictRequest();
// Act and assert
var exception = Assert.Throws<ArgumentException>(() => request.HasScope(scope));
Assert.Equal("scope", exception.ParamName);
Assert.StartsWith("The scope cannot be null or empty.", exception.Message);
}
[Theory]
[InlineData(null, false)]
[InlineData("openid", true)]
[InlineData("openid ", true)]
[InlineData(" openid ", true)]
[InlineData("openid profile", true)]
[InlineData("openid profile", true)]
[InlineData("openid profile ", true)]
[InlineData(" openid profile", true)]
[InlineData("profile", false)]
[InlineData("profile email", false)]
[InlineData("OPENID", false)]
[InlineData("OPENID ", false)]
[InlineData(" OPENID ", false)]
[InlineData("OPENID PROFILE", false)]
[InlineData("OPENID PROFILE", false)]
[InlineData("OPENID PROFILE ", false)]
[InlineData(" OPENID PROFILE", false)]
[InlineData("PROFILE", false)]
[InlineData("PROFILE EMAIL", false)]
public void HasScope_ReturnsExpectedResult(string scope, bool result)
{
// Arrange
var request = new OpenIddictRequest
{
Scope = scope
};
// Act and assert
Assert.Equal(result, request.HasScope(OpenIddictConstants.Scopes.OpenId));
}
[Fact]
public void IsNoneFlow_ThrowsAnExceptionForNullRequest()
{
// Arrange
var request = (OpenIddictRequest) null;
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() => request.IsNoneFlow());
Assert.Equal("request", exception.ParamName);
}
[Theory]
[InlineData(null, false)]
[InlineData("unknown", false)]
[InlineData("none", true)]
[InlineData("none ", true)]
[InlineData(" none", true)]
[InlineData("none id_token", false)]
[InlineData(" none id_token", false)]
[InlineData("none id_token ", false)]
[InlineData(" none id_token ", false)]
[InlineData("NONE", false)]
[InlineData("NONE ", false)]
[InlineData(" NONE", false)]
[InlineData("NONE ID_TOKEN", false)]
[InlineData(" NONE ID_TOKEN", false)]
[InlineData("NONE ID_TOKEN ", false)]
[InlineData(" NONE ID_TOKEN ", false)]
public void IsNoneFlow_ReturnsExpectedResult(string type, bool result)
{
// Arrange
var request = new OpenIddictRequest
{
ResponseType = type
};
// Act and assert
Assert.Equal(result, request.IsNoneFlow());
}
[Fact]
public void IsAuthorizationCodeFlow_ThrowsAnExceptionForNullRequest()
{
// Arrange
var request = (OpenIddictRequest) null;
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() => request.IsAuthorizationCodeFlow());
Assert.Equal("request", exception.ParamName);
}
[Theory]
[InlineData(null, false)]
[InlineData("unknown", false)]
[InlineData("code", true)]
[InlineData("code ", true)]
[InlineData(" code", true)]
[InlineData("code id_token", false)]
[InlineData(" code id_token", false)]
[InlineData("code id_token ", false)]
[InlineData(" code id_token ", false)]
[InlineData("CODE", false)]
[InlineData("CODE ", false)]
[InlineData(" CODE", false)]
[InlineData("CODE ID_TOKEN", false)]
[InlineData(" CODE ID_TOKEN", false)]
[InlineData("CODE ID_TOKEN ", false)]
[InlineData(" CODE ID_TOKEN ", false)]
public void IsAuthorizationCodeFlow_ReturnsExpectedResult(string type, bool result)
{
// Arrange
var request = new OpenIddictRequest
{
ResponseType = type
};
// Act and assert
Assert.Equal(result, request.IsAuthorizationCodeFlow());
}
[Fact]
public void IsImplicitFlow_ThrowsAnExceptionForNullRequest()
{
// Arrange
var request = (OpenIddictRequest) null;
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() => request.IsImplicitFlow());
Assert.Equal("request", exception.ParamName);
}
[Theory]
[InlineData(null, false)]
[InlineData("unknown", false)]
[InlineData("id_token", true)]
[InlineData("id_token ", true)]
[InlineData(" id_token", true)]
[InlineData("id_token token", true)]
[InlineData(" id_token token", true)]
[InlineData("id_token token ", true)]
[InlineData(" id_token token ", true)]
[InlineData("token", true)]
[InlineData("token ", true)]
[InlineData(" token", true)]
[InlineData("code id_token", false)]
[InlineData("code id_token token", false)]
[InlineData("code token", false)]
[InlineData("ID_TOKEN", false)]
[InlineData("ID_TOKEN ", false)]
[InlineData(" ID_TOKEN", false)]
[InlineData("ID_TOKEN TOKEN", false)]
[InlineData(" ID_TOKEN TOKEN", false)]
[InlineData("ID_TOKEN TOKEN ", false)]
[InlineData(" ID_TOKEN TOKEN ", false)]
[InlineData("TOKEN", false)]
[InlineData("TOKEN ", false)]
[InlineData(" TOKEN", false)]
[InlineData("CODE ID_TOKEN", false)]
[InlineData("CODE ID_TOKEN TOKEN", false)]
[InlineData("CODE TOKEN", false)]
public void IsImplicitFlow_ReturnsExpectedResult(string type, bool result)
{
// Arrange
var request = new OpenIddictRequest
{
ResponseType = type
};
// Act and assert
Assert.Equal(result, request.IsImplicitFlow());
}
[Fact]
public void IsHybridFlow_ThrowsAnExceptionForNullRequest()
{
// Arrange
var request = (OpenIddictRequest) null;
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() => request.IsHybridFlow());
Assert.Equal("request", exception.ParamName);
}
[Theory]
[InlineData(null, false)]
[InlineData("unknown", false)]
[InlineData("code id_token", true)]
[InlineData("code id_token ", true)]
[InlineData(" code id_token", true)]
[InlineData("code id_token token", true)]
[InlineData(" code id_token token", true)]
[InlineData("code id_token token ", true)]
[InlineData(" code id_token token ", true)]
[InlineData(" code id_token token ", true)]
[InlineData("code token", true)]
[InlineData("code token ", true)]
[InlineData(" code token", true)]
[InlineData("id_token", false)]
[InlineData("id_token token", false)]
[InlineData("token", false)]
[InlineData("CODE ID_TOKEN", false)]
[InlineData("CODE ID_TOKEN ", false)]
[InlineData(" CODE ID_TOKEN", false)]
[InlineData("CODE ID_TOKEN TOKEN", false)]
[InlineData(" CODE ID_TOKEN TOKEN", false)]
[InlineData("CODE ID_TOKEN TOKEN ", false)]
[InlineData(" CODE ID_TOKEN TOKEN ", false)]
[InlineData(" CODE ID_TOKEN TOKEN ", false)]
[InlineData("CODE TOKEN", false)]
[InlineData("CODE TOKEN ", false)]
[InlineData(" CODE TOKEN", false)]
[InlineData("ID_TOKEN", false)]
[InlineData("ID_TOKEN TOKEN", false)]
[InlineData("TOKEN", false)]
public void IsHybridFlow_ReturnsExpectedResult(string type, bool result)
{
// Arrange
var request = new OpenIddictRequest
{
ResponseType = type
};
// Act and assert
Assert.Equal(result, request.IsHybridFlow());
}
[Fact]
public void IsFragmentResponseMode_ThrowsAnExceptionForNullRequest()
{
// Arrange
var request = (OpenIddictRequest) null;
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() => request.IsFragmentResponseMode());
Assert.Equal("request", exception.ParamName);
}
[Theory]
[InlineData(null, null, false)]
[InlineData("unknown", null, false)]
[InlineData("query", null, false)]
[InlineData("form_post", null, false)]
[InlineData("fragment", null, true)]
[InlineData("fragment ", null, false)]
[InlineData(" fragment", null, false)]
[InlineData(" fragment ", null, false)]
[InlineData(null, "code", false)]
[InlineData(null, "code id_token", true)]
[InlineData(null, "code id_token token", true)]
[InlineData(null, "code token", true)]
[InlineData(null, "id_token", true)]
[InlineData(null, "id_token token", true)]
[InlineData(null, "token", true)]
[InlineData("QUERY", null, false)]
[InlineData("FRAGMENT", null, false)]
[InlineData("FORM_POST", null, false)]
[InlineData(null, "CODE", false)]
[InlineData(null, "CODE ID_TOKEN", false)]
[InlineData(null, "CODE ID_TOKEN TOKEN", false)]
[InlineData(null, "CODE TOKEN", false)]
[InlineData(null, "ID_TOKEN", false)]
[InlineData(null, "ID_TOKEN TOKEN", false)]
[InlineData(null, "TOKEN", false)]
public void IsFragmentResponseMode_ReturnsExpectedResult(string mode, string type, bool result)
{
// Arrange
var request = new OpenIddictRequest
{
ResponseMode = mode,
ResponseType = type
};
// Act and assert
Assert.Equal(result, request.IsFragmentResponseMode());
}
[Fact]
public void IsQueryResponseMode_ThrowsAnExceptionForNullRequest()
{
// Arrange
var request = (OpenIddictRequest) null;
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() => request.IsQueryResponseMode());
Assert.Equal("request", exception.ParamName);
}
[Theory]
[InlineData(null, null, false)]
[InlineData("unknown", null, false)]
[InlineData("query", null, true)]
[InlineData("query ", null, false)]
[InlineData(" query", null, false)]
[InlineData(" query ", null, false)]
[InlineData("fragment", null, false)]
[InlineData("form_post", null, false)]
[InlineData(null, "none", true)]
[InlineData(null, "code", true)]
[InlineData(null, "code id_token token", false)]
[InlineData(null, "code token", false)]
[InlineData(null, "id_token", false)]
[InlineData(null, "id_token token", false)]
[InlineData(null, "token", false)]
[InlineData("QUERY", null, false)]
[InlineData("FRAGMENT", null, false)]
[InlineData("FORM_POST", null, false)]
[InlineData(null, "CODE", false)]
[InlineData(null, "CODE ID_TOKEN", false)]
[InlineData(null, "CODE ID_TOKEN TOKEN", false)]
[InlineData(null, "CODE TOKEN", false)]
[InlineData(null, "ID_TOKEN", false)]
[InlineData(null, "ID_TOKEN TOKEN", false)]
[InlineData(null, "TOKEN", false)]
public void IsQueryResponseMode_ReturnsExpectedResult(string mode, string type, bool result)
{
// Arrange
var request = new OpenIddictRequest
{
ResponseMode = mode,
ResponseType = type
};
// Act and assert
Assert.Equal(result, request.IsQueryResponseMode());
}
[Fact]
public void IsFormPostResponseMode_ThrowsAnExceptionForNullRequest()
{
// Arrange
var request = (OpenIddictRequest) null;
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() => request.IsFormPostResponseMode());
Assert.Equal("request", exception.ParamName);
}
[Theory]
[InlineData(null, false)]
[InlineData("unknown", false)]
[InlineData("query", false)]
[InlineData("fragment", false)]
[InlineData("form_post", true)]
[InlineData("form_post ", false)]
[InlineData(" form_post", false)]
[InlineData(" form_post ", false)]
[InlineData("QUERY", false)]
[InlineData("FRAGMENT", false)]
[InlineData("FORM_POST", false)]
public void IsFormPostResponseMode_ReturnsExpectedResult(string mode, bool result)
{
// Arrange
var request = new OpenIddictRequest
{
ResponseMode = mode
};
// Act and assert
Assert.Equal(result, request.IsFormPostResponseMode());
}
[Fact]
public void IsAuthorizationCodeGrantType_ThrowsAnExceptionForNullRequest()
{
// Arrange
var request = (OpenIddictRequest) null;
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() => request.IsAuthorizationCodeGrantType());
Assert.Equal("request", exception.ParamName);
}
[Theory]
[InlineData(null, false)]
[InlineData("unknown", false)]
[InlineData("authorization_code", true)]
[InlineData("authorization_code ", false)]
[InlineData(" authorization_code", false)]
[InlineData(" authorization_code ", false)]
[InlineData("client_credentials", false)]
[InlineData("password", false)]
[InlineData("refresh_token", false)]
[InlineData("AUTHORIZATION_CODE", false)]
[InlineData("CLIENT_CREDENTIALS", false)]
[InlineData("PASSWORD", false)]
[InlineData("REFRESH_TOKEN", false)]
[InlineData("urn:ietf:params:oauth:grant-type:device_code", false)]
public void IsAuthorizationCodeGrantType_ReturnsExpectedResult(string type, bool result)
{
// Arrange
var request = new OpenIddictRequest
{
GrantType = type
};
// Act and assert
Assert.Equal(result, request.IsAuthorizationCodeGrantType());
}
[Fact]
public void IsClientCredentialsGrantType_ThrowsAnExceptionForNullRequest()
{
// Arrange
var request = (OpenIddictRequest) null;
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() => request.IsClientCredentialsGrantType());
Assert.Equal("request", exception.ParamName);
}
[Theory]
[InlineData(null, false)]
[InlineData("unknown", false)]
[InlineData("authorization_code", false)]
[InlineData("client_credentials", true)]
[InlineData("client_credentials ", false)]
[InlineData(" client_credentials", false)]
[InlineData(" client_credentials ", false)]
[InlineData("password", false)]
[InlineData("refresh_token", false)]
[InlineData("AUTHORIZATION_CODE", false)]
[InlineData("CLIENT_CREDENTIALS", false)]
[InlineData("PASSWORD", false)]
[InlineData("REFRESH_TOKEN", false)]
[InlineData("urn:ietf:params:oauth:grant-type:device_code", false)]
public void IsClientCredentialsGrantType_ReturnsExpectedResult(string type, bool result)
{
// Arrange
var request = new OpenIddictRequest
{
GrantType = type
};
// Act and assert
Assert.Equal(result, request.IsClientCredentialsGrantType());
}
[Fact]
public void IsDeviceCodeGrantType_ThrowsAnExceptionForNullRequest()
{
// Arrange
var request = (OpenIddictRequest)null;
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() => request.IsDeviceCodeGrantType());
Assert.Equal("request", exception.ParamName);
}
[Theory]
[InlineData(null, false)]
[InlineData("unknown", false)]
[InlineData("authorization_code", false)]
[InlineData("client_credentials", false)]
[InlineData("password", false)]
[InlineData("refresh_token", false)]
[InlineData("AUTHORIZATION_CODE", false)]
[InlineData("CLIENT_CREDENTIALS", false)]
[InlineData("PASSWORD", false)]
[InlineData("REFRESH_TOKEN", false)]
[InlineData("urn:ietf:params:oauth:grant-type:device_code", true)]
[InlineData("urn:ietf:params:oauth:grant-type:device_code ", false)]
[InlineData(" urn:ietf:params:oauth:grant-type:device_code", false)]
[InlineData(" urn:ietf:params:oauth:grant-type:device_code ", false)]
public void IsDeviceCodeGrantType_ReturnsExpectedResult(string type, bool result)
{
// Arrange
var request = new OpenIddictRequest
{
GrantType = type
};
// Act and assert
Assert.Equal(result, request.IsDeviceCodeGrantType());
}
[Fact]
public void IsPasswordGrantType_ThrowsAnExceptionForNullRequest()
{
// Arrange
var request = (OpenIddictRequest) null;
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() => request.IsPasswordGrantType());
Assert.Equal("request", exception.ParamName);
}
[Theory]
[InlineData(null, false)]
[InlineData("unknown", false)]
[InlineData("authorization_code", false)]
[InlineData("client_credentials", false)]
[InlineData("password", true)]
[InlineData("password ", false)]
[InlineData(" password", false)]
[InlineData(" password ", false)]
[InlineData("refresh_token", false)]
[InlineData("AUTHORIZATION_CODE", false)]
[InlineData("CLIENT_CREDENTIALS", false)]
[InlineData("PASSWORD", false)]
[InlineData("REFRESH_TOKEN", false)]
[InlineData("urn:ietf:params:oauth:grant-type:device_code", false)]
public void IsPasswordGrantType_ReturnsExpectedResult(string type, bool result)
{
// Arrange
var request = new OpenIddictRequest
{
GrantType = type
};
// Act and assert
Assert.Equal(result, request.IsPasswordGrantType());
}
[Fact]
public void IsRefreshTokenGrantType_ThrowsAnExceptionForNullRequest()
{
// Arrange
var request = (OpenIddictRequest) null;
// Act and assert
var exception = Assert.Throws<ArgumentNullException>(() => request.IsRefreshTokenGrantType());
Assert.Equal("request", exception.ParamName);
}
[Theory]
[InlineData(null, false)]
[InlineData("unknown", false)]
[InlineData("authorization_code", false)]
[InlineData("client_credentials", false)]
[InlineData("password", false)]
[InlineData("refresh_token", true)]
[InlineData("refresh_token ", false)]
[InlineData(" refresh_token", false)]
[InlineData(" refresh_token ", false)]
[InlineData("AUTHORIZATION_CODE", false)]
[InlineData("CLIENT_CREDENTIALS", false)]
[InlineData("PASSWORD", false)]
[InlineData("REFRESH_TOKEN", false)]
[InlineData("urn:ietf:params:oauth:grant-type:device_code", false)]
public void IsRefreshTokenGrantType_ReturnsExpectedResult(string type, bool result)
{
// Arrange
var request = new OpenIddictRequest
{
GrantType = type
};
// Act and assert
Assert.Equal(result, request.IsRefreshTokenGrantType());
}
}
}

450
test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictMessageTests.cs

@ -0,0 +1,450 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/openiddict/openiddict-core for more information concerning
* the license and the contributors participating to this project.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Xunit;
namespace OpenIddict.Abstractions.Tests.Primitives
{
public class OpenIddictMessageTests
{
[Fact]
public void Constructor_ImportsParameters()
{
// Arrange and act
var message = new OpenIddictMessage(new[]
{
new KeyValuePair<string, OpenIddictParameter>("parameter", 42)
});
// Assert
Assert.Equal(42, (long) message.GetParameter("parameter"));
}
[Fact]
public void Constructor_IgnoresNamelessParameters()
{
// Arrange and act
var message = new OpenIddictMessage(new[]
{
new KeyValuePair<string, OpenIddictParameter>(null, new OpenIddictParameter()),
new KeyValuePair<string, OpenIddictParameter>(string.Empty, new OpenIddictParameter())
});
// Assert
Assert.Empty(message.GetParameters());
}
[Fact]
public void Constructor_PreservesEmptyParameters()
{
// Arrange and act
var message = new OpenIddictMessage(new[]
{
new KeyValuePair<string, OpenIddictParameter>("null-parameter", (string) null),
new KeyValuePair<string, OpenIddictParameter>("empty-parameter", string.Empty)
});
// Assert
Assert.Equal(2, message.GetParameters().Count());
}
[Fact]
public void Constructor_IgnoresDuplicateParameters()
{
// Arrange and act
var message = new OpenIddictMessage(new[]
{
new KeyValuePair<string, OpenIddictParameter>("parameter", "Fabrikam"),
new KeyValuePair<string, OpenIddictParameter>("parameter", "Contoso")
});
// Assert
Assert.Single(message.GetParameters());
Assert.Equal("Fabrikam", message.GetParameter("parameter"));
}
[Fact]
public void Constructor_SupportsMultiValuedParameters()
{
// Arrange and act
var message = new OpenIddictMessage(new[]
{
new KeyValuePair<string, string[]>("parameter", new[] { "Fabrikam", "Contoso" })
});
// Assert
Assert.Single(message.GetParameters());
Assert.Equal(new[] { "Fabrikam", "Contoso" }, (string[]) message.GetParameter("parameter"));
}
[Fact]
public void Constructor_ExtractsSingleValuedParameters()
{
// Arrange and act
var message = new OpenIddictMessage(new[]
{
new KeyValuePair<string, string[]>("parameter", new[] { "Fabrikam" })
});
// Assert
Assert.Single(message.GetParameters());
Assert.Equal("Fabrikam", message.GetParameter("parameter")?.Value);
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void AddParameter_ThrowsAnExceptionForNullOrEmptyName(string name)
{
// Arrange
var message = new OpenIddictMessage();
// Act and assert
var exception = Assert.Throws<ArgumentException>(() =>
{
message.AddParameter(name, new OpenIddictParameter());
});
Assert.Equal("name", exception.ParamName);
Assert.StartsWith("The parameter name cannot be null or empty.", exception.Message);
}
[Fact]
public void AddParameter_AddsExpectedParameter()
{
// Arrange
var message = new OpenIddictMessage();
// Act
message.AddParameter("parameter", 42);
// Assert
Assert.Equal(42, message.GetParameter("parameter"));
}
[Fact]
public void AddParameter_IsCaseSensitive()
{
// Arrange
var message = new OpenIddictMessage();
// Act
message.AddParameter("PARAMETER", 42);
// Assert
Assert.Null(message.GetParameter("parameter"));
}
[Fact]
public void AddParameter_PreservesEmptyParameters()
{
// Arrange
var message = new OpenIddictMessage();
// Act
message.AddParameter("string", string.Empty);
message.AddParameter("array", new JArray());
message.AddParameter("object", new JObject());
message.AddParameter("value", new JValue(string.Empty));
// Assert
Assert.Empty((string) message.GetParameter("string"));
Assert.Equal(new JArray(), (JArray) message.GetParameter("array"));
Assert.Equal(new JObject(), (JObject) message.GetParameter("object"));
Assert.Equal(new JValue(string.Empty), (JValue) message.GetParameter("value"));
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void GetParameter_ThrowsAnExceptionForNullOrEmptyName(string name)
{
// Arrange
var message = new OpenIddictMessage();
// Act and assert
var exception = Assert.Throws<ArgumentException>(() => message.GetParameter(name));
Assert.Equal("name", exception.ParamName);
Assert.StartsWith("The parameter name cannot be null or empty.", exception.Message);
}
[Fact]
public void GetParameter_ReturnsExpectedParameter()
{
// Arrange
var message = new OpenIddictMessage();
message.SetParameter("parameter", 42);
// Act and assert
Assert.Equal(42, (int) message.GetParameter("parameter"));
}
[Fact]
public void GetParameter_IsCaseSensitive()
{
// Arrange
var message = new OpenIddictMessage();
message.SetParameter("parameter", 42);
// Act and assert
Assert.Null(message.GetParameter("PARAMETER"));
}
[Fact]
public void GetParameter_ReturnsNullForUnsetParameter()
{
// Arrange
var message = new OpenIddictMessage();
// Act and assert
Assert.Null(message.GetParameter("parameter"));
}
[Fact]
public void GetParameters_EnumeratesParameters()
{
// Arrange
var parameters = new Dictionary<string, OpenIddictParameter>
{
["int"] = int.MaxValue,
["long"] = long.MaxValue,
["string"] = "value"
};
var message = new OpenIddictMessage(parameters);
// Act and assert
Assert.Equal(parameters, message.GetParameters());
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void HasParameter_ThrowsAnExceptionForNullOrEmptyName(string name)
{
// Arrange
var message = new OpenIddictMessage();
// Act and assert
var exception = Assert.Throws<ArgumentException>(() => message.HasParameter(name));
Assert.Equal("name", exception.ParamName);
Assert.StartsWith("The parameter name cannot be null or empty.", exception.Message);
}
[Theory]
[InlineData("parameter", true)]
[InlineData("PARAMETER", false)]
[InlineData("missing_parameter", false)]
public void HasParameter_ReturnsExpectedResult(string parameter, bool result)
{
// Arrange
var message = new OpenIddictMessage();
message.SetParameter("parameter", "value");
// Act and assert
Assert.Equal(result, message.HasParameter(parameter));
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void RemoveParameter_ThrowsAnExceptionForNullOrEmptyName(string name)
{
// Arrange
var message = new OpenIddictMessage();
// Act and assert
var exception = Assert.Throws<ArgumentException>(() => message.RemoveParameter(name));
Assert.Equal("name", exception.ParamName);
Assert.StartsWith("The parameter name cannot be null or empty.", exception.Message);
}
[Fact]
public void RemoveParameter_RemovesExpectedParameter()
{
// Arrange
var message = new OpenIddictMessage();
message.AddParameter("parameter", 42);
// Act
message.RemoveParameter("parameter");
// Assert
Assert.Null(message.GetParameter("parameter"));
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void SetParameter_ThrowsAnExceptionForNullOrEmptyName(string name)
{
// Arrange
var message = new OpenIddictMessage();
// Act and assert
var exception = Assert.Throws<ArgumentException>(() => message.SetParameter(name, null));
Assert.Equal("name", exception.ParamName);
Assert.StartsWith("The parameter name cannot be null or empty.", exception.Message);
}
[Fact]
public void SetParameter_AddsExpectedParameter()
{
// Arrange
var message = new OpenIddictMessage();
// Act
message.SetParameter("parameter", 42);
// Assert
Assert.Equal(42, message.GetParameter("parameter"));
}
[Fact]
public void SetParameter_IsCaseSensitive()
{
// Arrange
var message = new OpenIddictMessage();
// Act
message.SetParameter("PARAMETER", 42);
// Assert
Assert.Null(message.GetParameter("parameter"));
}
[Fact]
public void SetParameter_RemovesNullParameters()
{
// Arrange
var message = new OpenIddictMessage();
// Act
message.SetParameter("null", null);
// Assert
Assert.Empty(message.GetParameters());
}
[Fact]
public void SetParameter_RemovesEmptyParameters()
{
// Arrange
var message = new OpenIddictMessage();
// Act
message.SetParameter("string", string.Empty);
message.SetParameter("array", new JArray());
message.SetParameter("object", new JObject());
message.SetParameter("value", new JValue(string.Empty));
// Assert
Assert.Empty(message.GetParameters());
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void TryGetParameter_ThrowsAnExceptionForNullOrEmptyName(string name)
{
// Arrange
var message = new OpenIddictMessage();
// Act
var exception = Assert.Throws<ArgumentException>(() => message.TryGetParameter(name, out OpenIddictParameter parameter));
// Assert
Assert.Equal("name", exception.ParamName);
Assert.StartsWith("The parameter name cannot be null or empty.", exception.Message);
}
[Fact]
public void TryGetParameter_ReturnsTrueAndExpectedParameter()
{
// Arrange
var name = "paramName";
var val = "paramValue";
var message = new OpenIddictMessage();
message.SetParameter(name, val);
// Act
var success = message.TryGetParameter(name, out OpenIddictParameter parameter);
// Assert
Assert.True(success);
Assert.Equal(val, (string)parameter.Value);
}
[Fact]
public void TryGetParameter_ReturnsFalse()
{
// Arrange
var name = "paramName";
var message = new OpenIddictMessage();
// Act
var success = message.TryGetParameter(name, out OpenIddictParameter parameter);
// Assert
Assert.False(success);
Assert.Null(parameter.Value);
}
[Fact]
public void ToString_ReturnsJsonRepresentation()
{
// Arrange
var message = JsonConvert.DeserializeObject<OpenIddictMessage>(@"{
""redirect_uris"": [
""https://client.example.org/callback"",
""https://client.example.org/callback2""
],
""client_name"": ""My Example Client"",
""token_endpoint_auth_method"": ""client_secret_basic"",
""logo_uri"": ""https://client.example.org/logo.png"",
""jwks_uri"": ""https://client.example.org/my_public_keys.jwks"",
""example_extension_parameter"": ""example_value""
}");
// Act and assert
Assert.Equal(JsonConvert.SerializeObject(message, Formatting.Indented), message.ToString());
}
[Theory]
[InlineData(OpenIddictConstants.Parameters.AccessToken)]
[InlineData(OpenIddictConstants.Parameters.Assertion)]
[InlineData(OpenIddictConstants.Parameters.ClientAssertion)]
[InlineData(OpenIddictConstants.Parameters.ClientSecret)]
[InlineData(OpenIddictConstants.Parameters.Code)]
[InlineData(OpenIddictConstants.Parameters.IdToken)]
[InlineData(OpenIddictConstants.Parameters.IdTokenHint)]
[InlineData(OpenIddictConstants.Parameters.Password)]
[InlineData(OpenIddictConstants.Parameters.RefreshToken)]
[InlineData(OpenIddictConstants.Parameters.Token)]
public void ToString_ExcludesSensitiveParameters(string parameter)
{
// Arrange
var message = new OpenIddictMessage();
message.AddParameter(parameter, "secret value");
// Act and assert
Assert.DoesNotContain("secret value", message.ToString());
Assert.Equal("[removed for security reasons]", JObject.Parse(message.ToString())[parameter]);
}
}
}

1026
test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictParameterTests.cs

File diff suppressed because it is too large

334
test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictRequestTests.cs

@ -0,0 +1,334 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/openiddict/openiddict-core for more information concerning
* the license and the contributors participating to this project.
*/
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Xunit;
namespace OpenIddict.Abstractions.Tests.Primitives
{
public class OpenIddictRequestTests
{
public static IEnumerable<object[]> Properties
{
get
{
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.AccessToken),
/* name: */ OpenIddictConstants.Parameters.AccessToken,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.AcrValues),
/* name: */ OpenIddictConstants.Parameters.AcrValues,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.Assertion),
/* name: */ OpenIddictConstants.Parameters.Assertion,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.Audiences),
/* name: */ OpenIddictConstants.Parameters.Audience,
/* value: */ new OpenIddictParameter(new[] { "Fabrikam", "Contoso" })
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.Claims),
/* name: */ OpenIddictConstants.Parameters.Claims,
/* value: */ new OpenIddictParameter(new JObject { ["userinfo"] = new JObject() })
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.ClaimsLocales),
/* name: */ OpenIddictConstants.Parameters.ClaimsLocales,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.ClientAssertion),
/* name: */ OpenIddictConstants.Parameters.ClientAssertion,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.ClientAssertionType),
/* name: */ OpenIddictConstants.Parameters.ClientAssertionType,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.ClientId),
/* name: */ OpenIddictConstants.Parameters.ClientId,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.ClientSecret),
/* name: */ OpenIddictConstants.Parameters.ClientSecret,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.Code),
/* name: */ OpenIddictConstants.Parameters.Code,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.CodeChallenge),
/* name: */ OpenIddictConstants.Parameters.CodeChallenge,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.CodeChallengeMethod),
/* name: */ OpenIddictConstants.Parameters.CodeChallengeMethod,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.CodeVerifier),
/* name: */ OpenIddictConstants.Parameters.CodeVerifier,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.DeviceCode),
/* name: */ OpenIddictConstants.Parameters.DeviceCode,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.Display),
/* name: */ OpenIddictConstants.Parameters.Display,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.GrantType),
/* name: */ OpenIddictConstants.Parameters.GrantType,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.IdentityProvider),
/* name: */ OpenIddictConstants.Parameters.IdentityProvider,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.IdTokenHint),
/* name: */ OpenIddictConstants.Parameters.IdTokenHint,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.LoginHint),
/* name: */ OpenIddictConstants.Parameters.LoginHint,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.Nonce),
/* name: */ OpenIddictConstants.Parameters.Nonce,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.MaxAge),
/* name: */ OpenIddictConstants.Parameters.MaxAge,
/* value: */ new OpenIddictParameter((long?) 42)
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.Password),
/* name: */ OpenIddictConstants.Parameters.Password,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.PostLogoutRedirectUri),
/* name: */ OpenIddictConstants.Parameters.PostLogoutRedirectUri,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.Prompt),
/* name: */ OpenIddictConstants.Parameters.Prompt,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.RedirectUri),
/* name: */ OpenIddictConstants.Parameters.RedirectUri,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.RefreshToken),
/* name: */ OpenIddictConstants.Parameters.RefreshToken,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.Registration),
/* name: */ OpenIddictConstants.Parameters.Registration,
/* value: */ new OpenIddictParameter(new JObject { ["policy_uri"] = "http://www.fabrikam.com/policy" })
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.Request),
/* name: */ OpenIddictConstants.Parameters.Request,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.RequestId),
/* name: */ OpenIddictConstants.Parameters.RequestId,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.RequestUri),
/* name: */ OpenIddictConstants.Parameters.RequestUri,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.Resources),
/* name: */ OpenIddictConstants.Parameters.Resource,
/* value: */ new OpenIddictParameter(new[] { "https://fabrikam.com/", "https://contoso.com/" })
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.ResponseMode),
/* name: */ OpenIddictConstants.Parameters.ResponseMode,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.ResponseType),
/* name: */ OpenIddictConstants.Parameters.ResponseType,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.Scope),
/* name: */ OpenIddictConstants.Parameters.Scope,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.State),
/* name: */ OpenIddictConstants.Parameters.State,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.Token),
/* name: */ OpenIddictConstants.Parameters.Token,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.TokenTypeHint),
/* name: */ OpenIddictConstants.Parameters.TokenTypeHint,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.UiLocales),
/* name: */ OpenIddictConstants.Parameters.UiLocales,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.UserCode),
/* name: */ OpenIddictConstants.Parameters.UserCode,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictRequest.Username),
/* name: */ OpenIddictConstants.Parameters.Username,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
}
}
[Theory]
[MemberData(nameof(Properties))]
public void PropertyGetter_ReturnsExpectedParameter(string property, string name, OpenIddictParameter value)
{
// Arrange
var request = new OpenIddictRequest();
request.SetParameter(name, value);
// Act and assert
Assert.Equal(value.Value, typeof(OpenIddictRequest).GetProperty(property).GetValue(request));
}
[Theory]
[MemberData(nameof(Properties))]
public void PropertySetter_AddsExpectedParameter(string property, string name, OpenIddictParameter value)
{
// Arrange
var request = new OpenIddictRequest();
// Act
typeof(OpenIddictRequest).GetProperty(property).SetValue(request, value.Value);
// Assert
Assert.Equal(value, request.GetParameter(name));
}
}
}

137
test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictResponseTests.cs

@ -0,0 +1,137 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/openiddict/openiddict-core for more information concerning
* the license and the contributors participating to this project.
*/
using System.Collections.Generic;
using Xunit;
namespace OpenIddict.Abstractions.Tests.Primitives
{
public class OpenIddictResponseTests
{
public static IEnumerable<object[]> Properties
{
get
{
yield return new object[]
{
/* property: */ nameof(OpenIddictResponse.AccessToken),
/* name: */ OpenIddictConstants.Parameters.AccessToken,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictResponse.Code),
/* name: */ OpenIddictConstants.Parameters.Code,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictResponse.DeviceCode),
/* name: */ OpenIddictConstants.Parameters.DeviceCode,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictResponse.Error),
/* name: */ OpenIddictConstants.Parameters.Error,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictResponse.ErrorDescription),
/* name: */ OpenIddictConstants.Parameters.ErrorDescription,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictResponse.ErrorUri),
/* name: */ OpenIddictConstants.Parameters.ErrorUri,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictResponse.ExpiresIn),
/* name: */ OpenIddictConstants.Parameters.ExpiresIn,
/* value: */ new OpenIddictParameter((long?) 42)
};
yield return new object[]
{
/* property: */ nameof(OpenIddictResponse.IdToken),
/* name: */ OpenIddictConstants.Parameters.IdToken,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictResponse.RefreshToken),
/* name: */ OpenIddictConstants.Parameters.RefreshToken,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictResponse.Scope),
/* name: */ OpenIddictConstants.Parameters.Scope,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictResponse.State),
/* name: */ OpenIddictConstants.Parameters.State,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictResponse.TokenType),
/* name: */ OpenIddictConstants.Parameters.TokenType,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
yield return new object[]
{
/* property: */ nameof(OpenIddictResponse.UserCode),
/* name: */ OpenIddictConstants.Parameters.UserCode,
/* value: */ new OpenIddictParameter("802A3E3E-DCCA-4EFC-89FA-7D82FE8C27E4")
};
}
}
[Theory]
[MemberData(nameof(Properties))]
public void PropertyGetter_ReturnsExpectedParameter(string property, string name, OpenIddictParameter value)
{
// Arrange
var response = new OpenIddictResponse();
response.SetParameter(name, value);
// Act and assert
Assert.Equal(value.Value, typeof(OpenIddictResponse).GetProperty(property).GetValue(response));
}
[Theory]
[MemberData(nameof(Properties))]
public void PropertySetter_AddsExpectedParameter(string property, string name, OpenIddictParameter value)
{
// Arrange
var response = new OpenIddictResponse();
// Act
typeof(OpenIddictResponse).GetProperty(property).SetValue(response, value.Value);
// Assert
Assert.Equal(value, response.GetParameter(name));
}
}
}
Loading…
Cancel
Save