-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathContentstack001_LoginTest.cs
More file actions
297 lines (265 loc) · 12.1 KB
/
Contentstack001_LoginTest.cs
File metadata and controls
297 lines (265 loc) · 12.1 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using System;
using System.Net;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using System.Threading;
using Contentstack.Management.Core.Queryable;
using Newtonsoft.Json.Linq;
namespace Contentstack.Management.Core.Tests.IntegrationTest
{
[TestClass]
public class Contentstack001_LoginTest
{
private readonly IConfigurationRoot _configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
[TestMethod]
[DoNotParallelize]
public void Test001_Should_Return_Failuer_On_Wrong_Login_Credentials()
{
ContentstackClient client = new ContentstackClient();
NetworkCredential credentials = new NetworkCredential("mock_user", "mock_pasword");
try
{
ContentstackResponse contentstackResponse = client.Login(credentials);
} catch (Exception e)
{
ContentstackErrorException errorException = e as ContentstackErrorException;
Assert.AreEqual(HttpStatusCode.UnprocessableEntity, errorException.StatusCode);
Assert.AreEqual("Looks like your email or password is invalid. Please try again or reset your password.", errorException.Message);
Assert.AreEqual("Looks like your email or password is invalid. Please try again or reset your password.", errorException.ErrorMessage);
Assert.AreEqual(104, errorException.ErrorCode);
}
}
[TestMethod]
[DoNotParallelize]
public void Test002_Should_Return_Failuer_On_Wrong_Async_Login_Credentials()
{
ContentstackClient client = new ContentstackClient();
NetworkCredential credentials = new NetworkCredential("mock_user", "mock_pasword");
var response = client.LoginAsync(credentials);
response.ContinueWith((t) =>
{
if (t.IsCompleted && t.Status == System.Threading.Tasks.TaskStatus.Faulted)
{
ContentstackErrorException errorException = t.Exception.InnerException as ContentstackErrorException;
Assert.AreEqual(HttpStatusCode.UnprocessableEntity, errorException.StatusCode);
Assert.AreEqual("Looks like your email or password is invalid. Please try again or reset your password.", errorException.Message);
Assert.AreEqual("Looks like your email or password is invalid. Please try again or reset your password.", errorException.ErrorMessage);
Assert.AreEqual(104, errorException.ErrorCode);
}
});
Thread.Sleep(3000);
}
[TestMethod]
[DoNotParallelize]
public void Test003_Should_Return_Success_On_Async_Login()
{
ContentstackClient client = new ContentstackClient();
var response = client.LoginAsync(Contentstack.Credential);
response.ContinueWith((t) =>
{
if (t.IsCompleted)
{
try
{
ContentstackResponse contentstackResponse = t.Result;
string loginResponse = contentstackResponse.OpenResponse();
Assert.IsNotNull(client.contentstackOptions.Authtoken);
Assert.IsNotNull(loginResponse);
}catch (Exception e)
{
Assert.Fail(e.Message);
}
}
});
Thread.Sleep(3000);
}
[TestMethod]
[DoNotParallelize]
public void Test004_Should_Return_Success_On_Login()
{
string email = _configuration.GetSection("Contentstack:Credentials:Email").Value;
string password = _configuration.GetSection("Contentstack:Credentials:Password").Value;
try
{
ContentstackClient client = Contentstack.Client;
ContentstackResponse contentstackResponse = client.Login(Contentstack.Credential);
string loginResponse = contentstackResponse.OpenResponse();
Assert.IsNotNull(client.contentstackOptions.Authtoken);
Assert.IsNotNull(loginResponse);
}
catch (Exception e)
{
Assert.Fail(e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test005_Should_Return_Loggedin_User()
{
try
{
ContentstackClient client = Contentstack.Client;
ContentstackResponse response = client.GetUser();
var user = response.OpenJObjectResponse();
Assert.IsNotNull(user);
}
catch (Exception e)
{
Assert.Fail(e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public async System.Threading.Tasks.Task Test006_Should_Return_Loggedin_User_Async()
{
try
{
ContentstackClient client = Contentstack.Client;
ContentstackResponse response = await client.GetUserAsync();
var user = response.OpenJObjectResponse();
Assert.IsNotNull(user);
Assert.IsNotNull(user["user"]["organizations"]);
Assert.IsInstanceOfType(user["user"]["organizations"], typeof(JArray));
Assert.IsNull(user["user"]["organizations"][0]["org_roles"]);
}
catch (Exception e)
{
Assert.Fail(e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test007_Should_Return_Loggedin_User_With_Organizations_detail()
{
try
{
ParameterCollection collection = new ParameterCollection();
collection.Add("include_orgs_roles", true);
ContentstackClient client = Contentstack.Client;
ContentstackResponse response = client.GetUser(collection);
var user = response.OpenJObjectResponse();
Assert.IsNotNull(user);
Assert.IsNotNull(user["user"]["organizations"]);
Assert.IsInstanceOfType(user["user"]["organizations"], typeof(JArray));
Assert.IsNotNull(user["user"]["organizations"][0]["org_roles"]);
}
catch (Exception e)
{
Assert.Fail(e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test008_Should_Fail_Login_With_Invalid_MfaSecret()
{
ContentstackClient client = new ContentstackClient();
NetworkCredential credentials = new NetworkCredential("test_user", "test_password");
string invalidMfaSecret = "INVALID_BASE32_SECRET!@#";
try
{
ContentstackResponse contentstackResponse = client.Login(credentials, null, invalidMfaSecret);
Assert.Fail("Expected exception for invalid MFA secret");
}
catch (ArgumentException)
{
// Expected exception for invalid Base32 encoding
Assert.IsTrue(true);
}
catch (Exception e)
{
Assert.Fail($"Unexpected exception type: {e.GetType().Name} - {e.Message}");
}
}
[TestMethod]
[DoNotParallelize]
public void Test009_Should_Generate_TOTP_Token_With_Valid_MfaSecret()
{
ContentstackClient client = new ContentstackClient();
NetworkCredential credentials = new NetworkCredential("test_user", "test_password");
string validMfaSecret = "JBSWY3DPEHPK3PXP"; // Valid Base32 test secret
try
{
// This should fail due to invalid credentials, but should succeed in generating TOTP
ContentstackResponse contentstackResponse = client.Login(credentials, null, validMfaSecret);
}
catch (ContentstackErrorException errorException)
{
// Expected to fail due to invalid credentials, but we verify it processed the MFA secret
// The error should be about credentials, not about MFA secret format
Assert.AreEqual(HttpStatusCode.UnprocessableEntity, errorException.StatusCode);
Assert.IsTrue(errorException.Message.Contains("email or password") ||
errorException.Message.Contains("credentials") ||
errorException.Message.Contains("authentication"));
}
catch (ArgumentException)
{
Assert.Fail("Should not throw ArgumentException for valid MFA secret");
}
catch (Exception e)
{
Assert.Fail($"Unexpected exception type: {e.GetType().Name} - {e.Message}");
}
}
[TestMethod]
[DoNotParallelize]
public async System.Threading.Tasks.Task Test010_Should_Generate_TOTP_Token_With_Valid_MfaSecret_Async()
{
ContentstackClient client = new ContentstackClient();
NetworkCredential credentials = new NetworkCredential("test_user", "test_password");
string validMfaSecret = "JBSWY3DPEHPK3PXP"; // Valid Base32 test secret
try
{
// This should fail due to invalid credentials, but should succeed in generating TOTP
ContentstackResponse contentstackResponse = await client.LoginAsync(credentials, null, validMfaSecret);
}
catch (ContentstackErrorException errorException)
{
// Expected to fail due to invalid credentials, but we verify it processed the MFA secret
// The error should be about credentials, not about MFA secret format
Assert.AreEqual(HttpStatusCode.UnprocessableEntity, errorException.StatusCode);
Assert.IsTrue(errorException.Message.Contains("email or password") ||
errorException.Message.Contains("credentials") ||
errorException.Message.Contains("authentication"));
}
catch (ArgumentException)
{
Assert.Fail("Should not throw ArgumentException for valid MFA secret");
}
catch (Exception e)
{
Assert.Fail($"Unexpected exception type: {e.GetType().Name} - {e.Message}");
}
}
[TestMethod]
[DoNotParallelize]
public void Test011_Should_Prefer_Explicit_Token_Over_MfaSecret()
{
ContentstackClient client = new ContentstackClient();
NetworkCredential credentials = new NetworkCredential("test_user", "test_password");
string validMfaSecret = "JBSWY3DPEHPK3PXP";
string explicitToken = "123456";
try
{
// This should fail due to invalid credentials, but should use explicit token
ContentstackResponse contentstackResponse = client.Login(credentials, explicitToken, validMfaSecret);
}
catch (ContentstackErrorException errorException)
{
// Expected to fail due to invalid credentials
// The important thing is that it didn't throw an exception about MFA secret processing
Assert.AreEqual(HttpStatusCode.UnprocessableEntity, errorException.StatusCode);
}
catch (ArgumentException)
{
Assert.Fail("Should not throw ArgumentException when explicit token is provided");
}
catch (Exception e)
{
Assert.Fail($"Unexpected exception type: {e.GetType().Name} - {e.Message}");
}
}
}
}