Skip to content

Commit 1e2a11c

Browse files
author
Collin Dauphinee
committed
Adding tests for non-dynamic presence channels
1 parent 602d5ef commit 1e2a11c

File tree

5 files changed

+102
-1
lines changed

5 files changed

+102
-1
lines changed

PusherClient.Tests.Utilities/FakeAuthoriser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public string Authorize(string channelName, string socketId)
2121
{
2222
var channelData = new PresenceChannelData();
2323
channelData.user_id = socketId;
24-
channelData.user_info = new { name = _userName };
24+
channelData.user_info = new FakeUserInfo { name = _userName };
2525

2626
authData = provider.Authenticate(channelName, socketId, channelData).ToJson();
2727
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using PusherServer;
2+
3+
namespace PusherClient.Tests.Utilities
4+
{
5+
public class FakeUserInfo
6+
{
7+
public string name { get; set; }
8+
}
9+
}

PusherClient.Tests.Utilities/PusherClient.Tests.Utilities.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<ItemGroup>
7070
<Compile Include="ChannelNameFactory.cs" />
7171
<Compile Include="Config.cs" />
72+
<Compile Include="FakeUserInfo.cs" />
7273
<Compile Include="FakeAuthoriser.cs" />
7374
<Compile Include="Properties\AssemblyInfo.cs" />
7475
<Compile Include="PusherFactory.cs" />

PusherClient.Tests/AcceptanceTests/PresenceChannel.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,49 @@ public void PresenceChannelShouldAddAMemberWhenGivenAMemberAsync()
9292
Assert.IsTrue(channel.IsSubscribed);
9393
Assert.IsTrue(channelSubscribed);
9494
}
95+
96+
[Test]
97+
public void PresenceChannelShouldUseGenericWhenGivenAMemberAsync()
98+
{
99+
// Arrange
100+
var stubOptions = new PusherOptions
101+
{
102+
Authorizer = new FakeAuthoriser(UserNameFactory.CreateUniqueUserName())
103+
};
104+
105+
var pusher = PusherFactory.GetPusher(stubOptions);
106+
AutoResetEvent reset = new AutoResetEvent(false);
107+
108+
pusher.Connected += sender =>
109+
{
110+
reset.Set();
111+
};
112+
113+
AsyncContext.Run(() => pusher.ConnectAsync());
114+
reset.WaitOne(TimeSpan.FromSeconds(5));
115+
reset.Reset();
116+
117+
var mockChannelName = ChannelNameFactory.CreateUniqueChannelName(presenceChannel: true);
118+
119+
var channelSubscribed = false;
120+
121+
// Act
122+
var channel = AsyncContext.Run(() => pusher.SubscribePresenceAsync<FakeUserInfo>(mockChannelName));
123+
channel.Subscribed += sender =>
124+
{
125+
channelSubscribed = true;
126+
reset.Set();
127+
};
128+
129+
reset.WaitOne(TimeSpan.FromSeconds(10));
130+
131+
// Assert
132+
Assert.IsNotNull(channel);
133+
StringAssert.Contains(mockChannelName, channel.Name);
134+
Assert.IsTrue(channel.IsSubscribed);
135+
Assert.IsTrue(channelSubscribed);
136+
137+
CollectionAssert.IsNotEmpty(channel.Members);
138+
}
95139
}
96140
}

PusherClient.Tests/UnitTests/PusherTests.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using Nito.AsyncEx;
33
using NUnit.Framework;
4+
using PusherClient.Tests.Utilities;
45

56
namespace PusherClient.Tests.UnitTests
67
{
@@ -147,5 +148,51 @@ public void PusherShouldThrowAnExceptionWhenSubscribeIsCalledWithANullStringForA
147148
Assert.IsNotNull(caughtException);
148149
StringAssert.Contains("The channel name cannot be null or whitespace", caughtException.Message);
149150
}
151+
152+
[Test]
153+
public void PusherShouldThrowAnExceptionWhenSubscribePresenceIsCalledWithANonPresenceChannelAsync()
154+
{
155+
// Arrange
156+
ArgumentException caughtException = null;
157+
158+
// Act
159+
try
160+
{
161+
var pusher = new Pusher("FakeAppKey");
162+
var channel = AsyncContext.Run(() => pusher.SubscribePresenceAsync<string>("private-123"));
163+
}
164+
catch (ArgumentException ex)
165+
{
166+
caughtException = ex;
167+
}
168+
169+
// Assert
170+
Assert.IsNotNull(caughtException);
171+
StringAssert.Contains("The channel name must be refer to a presence channel", caughtException.Message);
172+
}
173+
174+
[Test]
175+
public void PusherShouldThrowAnExceptionWhenSubscribePresenceIsCalledWithADifferentTypeAsync()
176+
{
177+
// Arrange
178+
InvalidOperationException caughtException = null;
179+
180+
// Act
181+
var pusher = new Pusher("FakeAppKey", new PusherOptions { Authorizer = new FakeAuthoriser("test") });
182+
AsyncContext.Run(() => pusher.SubscribePresenceAsync<string>("presence-123"));
183+
184+
try
185+
{
186+
AsyncContext.Run(() => pusher.SubscribePresenceAsync<int>("presence-123"));
187+
}
188+
catch (InvalidOperationException ex)
189+
{
190+
caughtException = ex;
191+
}
192+
193+
// Assert
194+
Assert.IsNotNull(caughtException);
195+
StringAssert.Contains("Cannot change channel member type; was previously defined as", caughtException.Message);
196+
}
150197
}
151198
}

0 commit comments

Comments
 (0)