summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/OAuth2/MessageFactoryTests.cs
blob: 52b5371385ae79fb544c41201de7f2fcda99098d (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//-----------------------------------------------------------------------
// <copyright file="MessageFactoryTests.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. 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 OAuth 2 message types are recognized.
	/// </summary>
	public class MessageFactoryTests : OAuth2TestBase {
		private readonly MessageReceivingEndpoint recipient = new MessageReceivingEndpoint("http://who", HttpDeliveryMethods.PostRequest);
		private IMessageFactory authServerMessageFactory;

		private IMessageFactory clientMessageFactory;

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

			var authServerChannel = new OAuth2AuthorizationServerChannel(new Mock<IAuthorizationServerHost>().Object, new Mock<ClientAuthenticationModule>().Object);
			this.authServerMessageFactory = authServerChannel.MessageFactoryTestHook;

			var clientChannel = new OAuth2ClientChannel();
			this.clientMessageFactory = clientChannel.MessageFactoryTestHook;
		}

		#region End user authorization messages

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

		[Test]
		public void EndUserAuthorizationImplicitRequest() {
			var fields = new Dictionary<string, string> {
				{ Protocol.response_type, "token" },
				{ Protocol.client_id, "abc" },
				{ Protocol.redirect_uri, "abc" },
			};
			IDirectedProtocolMessage request = this.authServerMessageFactory.GetNewRequestMessage(this.recipient, fields);
			Assert.That(request, Is.InstanceOf(typeof(EndUserAuthorizationImplicitRequest)));
		}

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

		[Test]
		public void EndUserAuthorizationSuccessResponseWithAccessToken() {
			var fields = new Dictionary<string, string> {
				{ Protocol.access_token, "abc" },
				{ Protocol.token_type, "bearer" },
			};
			IDirectedProtocolMessage request = this.clientMessageFactory.GetNewRequestMessage(this.recipient, fields);
			Assert.That(request, Is.InstanceOf(typeof(EndUserAuthorizationSuccessResponseBase)));
		}

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

		#endregion

		#region Access token request messages

		[Test]
		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.authServerMessageFactory.GetNewRequestMessage(this.recipient, fields);
			Assert.That(request, Is.InstanceOf(typeof(AccessTokenRefreshRequest)));
		}

		[Test]
		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.authServerMessageFactory.GetNewRequestMessage(this.recipient, fields);
			Assert.That(request, Is.InstanceOf(typeof(AccessTokenAuthorizationCodeRequest)));
		}

		[Test]
		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.authServerMessageFactory.GetNewRequestMessage(this.recipient, fields);
			Assert.That(request, Is.InstanceOf(typeof(AccessTokenResourceOwnerPasswordCredentialsRequest)));
		}

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

		#endregion
	}
}