forked from JabbR/JabbR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatFacts.cs
165 lines (133 loc) · 5.86 KB
/
ChatFacts.cs
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System.Collections.Specialized;
using System.Security.Principal;
using JabbR.ContentProviders.Core;
using JabbR.Models;
using JabbR.Services;
using Moq;
using Newtonsoft.Json;
using SignalR;
using SignalR.Hosting;
using SignalR.Hubs;
using Xunit;
namespace JabbR.Test
{
public class ChatFacts
{
public class Join
{
[Fact]
public void CannotJoinChat()
{
var clientState = new TrackingDictionary();
string clientId = "1";
var user = new ChatUser
{
Id = "1234",
Name = "John"
};
TestableChat chat = GetTestableChat(clientId, clientState, user);
chat.Caller.id = "1234";
bool result = chat.Join();
Assert.False(result);
}
[Fact]
public void CanJoinChatIfIdentitySet()
{
var clientState = new TrackingDictionary();
string clientId = "1";
var user = new ChatUser
{
Id = "1234",
Name = "John",
Identity = "foo"
};
TestableChat chat = GetTestableChat(clientId, clientState, user);
chat.Caller.id = "1234";
bool result = chat.Join();
Assert.Equal("1234", clientState["id"]);
Assert.Equal("John", clientState["name"]);
Assert.True(result);
// TODO: find out why these don't work
//Assert.Equal(1, user.ConnectedClients.Count);
//Assert.Equal("1", user.ConnectedClients.First().Id);
chat.MockedConnection.Verify(m => m.Broadcast("Chat." + clientId, It.IsAny<object>()), Times.Once());
}
[Fact]
public void MissingUsernameReturnsFalse()
{
var clientState = new TrackingDictionary();
string clientId = "1";
var user = new ChatUser();
TestableChat chat = GetTestableChat(clientId, clientState, user);
bool result = chat.Join();
Assert.False(result);
}
[Fact]
public void CanDeserializeClientState()
{
var clientState = new TrackingDictionary();
string clientId = "1";
var user = new ChatUser
{
Id = "1234",
Name = "John",
Identity = "foo"
};
var cookies = new NameValueCollection();
cookies["jabbr.state"] = JsonConvert.SerializeObject(new ClientState { UserId = user.Id });
TestableChat chat = GetTestableChat(clientId, clientState, user, cookies);
bool result = chat.Join();
Assert.Equal("1234", clientState["id"]);
Assert.Equal("John", clientState["name"]);
Assert.True(result);
chat.MockedConnection.Verify(m => m.Broadcast("Chat." + clientId, It.IsAny<object>()), Times.Once());
}
}
public static TestableChat GetTestableChat(string clientId, TrackingDictionary clientState, ChatUser user)
{
return GetTestableChat(clientId, clientState, user, new NameValueCollection());
}
public static TestableChat GetTestableChat(string clientId, TrackingDictionary clientState, ChatUser user, NameValueCollection cookies)
{
// setup things needed for chat
var repository = new InMemoryRepository();
var resourceProcessor = new Mock<IResourceProcessor>();
var chatService = new Mock<IChatService>();
var connection = new Mock<IConnection>();
var settings = new Mock<IApplicationSettings>();
settings.Setup(m => m.AuthApiKey).Returns("key");
// add user to repository
repository.Add(user);
// create testable chat
var chat = new TestableChat(settings, resourceProcessor, chatService, repository, connection);
var mockedConnectionObject = chat.MockedConnection.Object;
// setup client agent
chat.Agent = new ClientAgent(mockedConnectionObject, "Chat");
var request = new Mock<IRequest>();
request.Setup(m => m.Cookies).Returns(cookies);
// setup signal agent
var prinicipal = new Mock<IPrincipal>();
chat.Caller = new SignalAgent(mockedConnectionObject, clientId, "Chat", clientState);
// setup context
chat.Context = new HubContext(new HostContext(request.Object, null, prinicipal.Object), clientId);
return chat;
}
public class TestableChat : Chat
{
public Mock<IResourceProcessor> MockedResourceProcessor { get; private set; }
public Mock<IChatService> MockedChatService { get; private set; }
public IJabbrRepository Repository { get; private set; }
public Mock<IConnection> MockedConnection { get; private set; }
public Mock<IApplicationSettings> MockSettings { get; set; }
public TestableChat(Mock<IApplicationSettings> mockSettings, Mock<IResourceProcessor> mockedResourceProcessor, Mock<IChatService> mockedChatService, IJabbrRepository repository, Mock<IConnection> connection)
: base(mockSettings.Object, mockedResourceProcessor.Object, mockedChatService.Object, repository)
{
MockedResourceProcessor = mockedResourceProcessor;
MockedChatService = mockedChatService;
MockSettings = mockSettings;
Repository = repository;
MockedConnection = connection;
}
}
}
}