summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/OAuth2/MessageFactoryTests.cs
blob: e6237cafb81c82da73ba18e4c800240a79f7fec6 (plain)
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
//-----------------------------------------------------------------------
// <copyright file="MessageFactoryTests.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Test.OAuth2 {
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.OAuth2;
	using DotNetOpenAuth.OAuth2.ChannelElements;
	using DotNetOpenAuth.OAuth2.Messages;
	using Moq;
	using NUnit.Framework;

	/// <summary>
	/// Verifies that the WRAP message types are recognized.
	/// </summary>
	public class MessageFactoryTests : OAuth2TestBase {
		private readonly MessageReceivingEndpoint recipient = new MessageReceivingEndpoint("http://who", HttpDeliveryMethods.PostRequest);
		private OAuth2AuthorizationServerChannel channel;
		private IMessageFactory messageFactory;

		public override void SetUp() {
			base.SetUp();

			this.channel = new OAuth2AuthorizationServerChannel(new Mock<IAuthorizationServer>().Object);
			this.messageFactory = this.channel.MessageFactoryTestHook;
		}

		#region End user authorization messages

		[TestCase]
		public void EndUserAuthorizationRequest() {
			var fields = new Dictionary<string, string> {
				{ Protocol.response_type, "code" },
				{ Protocol.client_id, "abc" },
				{ Protocol.redirect_uri, "abc" },
			};
			IDirectedProtocolMessage request = this.messageFactory.GetNewRequestMessage(this.recipient, fields);
			Assert.IsInstanceOf(typeof(EndUserAuthorizationRequest), request);
		}

		[TestCase]
		public void EndUserAuthorizationSuccessResponseWithCode() {
			var fields = new Dictionary<string, string> {
				{ Protocol.code, "abc" },
			};
			IDirectedProtocolMessage request = this.messageFactory.GetNewRequestMessage(this.recipient, fields);
			Assert.IsInstanceOf(typeof(EndUserAuthorizationSuccessResponseBase), request);
		}

		[TestCase, Ignore("Not yet supported")]
		public void EndUserAuthorizationSuccessResponseWithAccessToken() {
			var fields = new Dictionary<string, string> {
				{ Protocol.access_token, "abc" },
			};
			IDirectedProtocolMessage request = this.messageFactory.GetNewRequestMessage(this.recipient, fields);
			Assert.IsInstanceOf(typeof(EndUserAuthorizationSuccessResponseBase), request);
		}

		[TestCase]
		public void EndUserAuthorizationFailedResponse() {
			var fields = new Dictionary<string, string> {
				{ Protocol.error, "access-denied" },
			};
			IDirectedProtocolMessage request = this.messageFactory.GetNewRequestMessage(this.recipient, fields);
			Assert.IsInstanceOf(typeof(EndUserAuthorizationFailedResponse), request);
		}

		#endregion

		#region Access token request messages

		[TestCase]
		public void AccessTokenRefreshRequest() {
			var fields = new Dictionary<string, string> {
				{ Protocol.client_id, "abc" },
				{ Protocol.refresh_token, "abc" },
				{ Protocol.grant_type, "refresh-token" },
			};
			IDirectedProtocolMessage request = this.messageFactory.GetNewRequestMessage(this.recipient, fields);
			Assert.IsInstanceOf(typeof(AccessTokenRefreshRequest), request);
		}

		[TestCase]
		public void AccessTokenAuthorizationCodeRequest() {
			var fields = new Dictionary<string, string> {
				{ Protocol.client_id, "abc" },
				{ Protocol.code, "code" },
				{ Protocol.grant_type, "authorization-code" },
				{ Protocol.redirect_uri, "http://someUri" },
			};
			IDirectedProtocolMessage request = this.messageFactory.GetNewRequestMessage(this.recipient, fields);
			Assert.IsInstanceOf(typeof(AccessTokenAuthorizationCodeRequest), request);
		}

		[TestCase]
		public void AccessTokenBasicCredentialsRequest() {
			var fields = new Dictionary<string, string> {
				{ Protocol.client_id, "abc" },
				{ Protocol.client_secret, "abc" },
				{ Protocol.grant_type, "basic-credentials" },
				{ Protocol.username, "abc" },
				{ Protocol.password, "abc" },
			};
			IDirectedProtocolMessage request = this.messageFactory.GetNewRequestMessage(this.recipient, fields);
			Assert.IsInstanceOf(typeof(AccessTokenResourceOwnerPasswordCredentialsRequest), request);
		}

		[TestCase]
		public void AccessTokenClientCredentialsRequest() {
			var fields = new Dictionary<string, string> {
				{ Protocol.client_id, "abc" },
				{ Protocol.client_secret, "abc" },
				{ Protocol.grant_type, "none" },
			};
			IDirectedProtocolMessage request = this.messageFactory.GetNewRequestMessage(this.recipient, fields);
			Assert.IsInstanceOf(typeof(AccessTokenClientCredentialsRequest), request);
		}

		#endregion
	}
}