-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathContentstackClientTest.cs
More file actions
242 lines (215 loc) · 8.18 KB
/
ContentstackClientTest.cs
File metadata and controls
242 lines (215 loc) · 8.18 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
// Contentstack.Core/ContentstackClientTest.cs
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Xunit;
using Contentstack.Core.Configuration;
namespace Contentstack.Core.Tests
{
public class ContentstackClientTest
{
private ContentstackClient CreateClient()
{
var options = new ContentstackOptions()
{
ApiKey = "api_key",
DeliveryToken = "delivery_token",
Environment = "environment",
Version = "1.2.3",
LivePreview = new LivePreviewConfig()
{
Enable = true,
Host = "https://preview.contentstack.com",
ReleaseId = "rid",
PreviewTimestamp = "ts",
PreviewToken = "pt"
}
};
return new ContentstackClient(options);
}
private string GetPrivateField(ContentstackClient client, string fieldName)
{
var field = typeof(ContentstackClient).GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
return (string)field.GetValue(client);
}
[Fact]
public void SetEntryUid_SetsValue_WhenNonEmpty()
{
var client = CreateClient();
client.SetEntryUid("entry123");
Assert.Equal("entry123", GetPrivateField(client, "currentEntryUid"));
}
[Fact]
public void SetEntryUid_DoesNotSet_WhenEmpty()
{
var client = CreateClient();
client.SetEntryUid("entry123");
client.SetEntryUid("");
Assert.Equal("entry123", GetPrivateField(client, "currentEntryUid"));
}
[Fact]
public void SetEntryUid_DoesNotSet_WhenNull()
{
var client = CreateClient();
client.SetEntryUid("entry123");
client.SetEntryUid(null);
Assert.Equal("entry123", GetPrivateField(client, "currentEntryUid"));
}
[Fact]
public void ContentType_SetscurrentContenttypeUid_WhenNonEmpty()
{
var client = CreateClient();
client.ContentType("blog");
Assert.Equal("blog", GetPrivateField(client, "currentContenttypeUid"));
}
[Fact]
public void ContentType_DoesNotSet_WhenEmpty()
{
var client = CreateClient();
client.ContentType("blog");
client.ContentType("");
Assert.Equal("blog", GetPrivateField(client, "currentContenttypeUid"));
}
[Fact]
public void ContentType_DoesNotSet_WhenNull()
{
var client = CreateClient();
client.ContentType("blog");
client.ContentType(null);
Assert.Equal("blog", GetPrivateField(client, "currentContenttypeUid"));
}
[Fact]
public void ContentType_ReturnsContentTypeInstance()
{
var client = CreateClient();
var contentType = client.ContentType("blog");
Assert.NotNull(contentType);
Assert.Equal("blog", contentType.ContentTypeId);
}
[Fact]
public void GlobalField_ReturnsGlobalFieldInstance()
{
var client = CreateClient();
var globalField = client.GlobalField("author");
Assert.NotNull(globalField);
Assert.Equal("author", globalField.GlobalFieldId);
}
[Fact]
public void Asset_ReturnsAssetInstance()
{
var client = CreateClient();
var asset = client.Asset("asset_uid");
Assert.NotNull(asset);
Assert.Equal("asset_uid", asset.Uid);
}
[Fact]
public void AssetLibrary_ReturnsAssetLibraryInstance()
{
var client = CreateClient();
var assetLibrary = client.AssetLibrary();
Assert.NotNull(assetLibrary);
}
[Fact]
public void Taxonomies_ReturnsTaxonomyInstance()
{
var client = CreateClient();
var taxonomy = client.Taxonomies();
Assert.NotNull(taxonomy);
}
[Fact]
public void GetVersion_ReturnsVersion()
{
var client = CreateClient();
var t = client.GetVersion();
Assert.Equal("1.2.3", client.GetVersion());
}
[Fact]
public void GetApplicationKey_ReturnsApiKey()
{
var client = CreateClient();
Assert.Equal("api_key", client.GetApplicationKey());
}
[Fact]
public void GetAccessToken_ReturnsDeliveryToken()
{
var client = CreateClient();
Assert.Equal("delivery_token", client.GetAccessToken());
}
[Fact]
public void GetLivePreviewConfig_ReturnsConfig()
{
var client = CreateClient();
Assert.NotNull(client.GetLivePreviewConfig());
}
[Fact]
public void RemoveHeader_RemovesHeader()
{
var client = CreateClient();
client.SetHeader("custom", "value");
client.RemoveHeader("custom");
var localHeaders = typeof(ContentstackClient)
.GetField("_LocalHeaders", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(client) as Dictionary<string, object>;
Assert.False(localHeaders.ContainsKey("custom"));
}
[Fact]
public void SetHeader_AddsHeader()
{
var client = CreateClient();
client.SetHeader("custom", "value");
var localHeaders = typeof(ContentstackClient)
.GetField("_LocalHeaders", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(client) as Dictionary<string, object>;
Assert.True(localHeaders.ContainsKey("custom"));
Assert.Equal("value", localHeaders["custom"]);
}
[Fact]
public async Task LivePreviewQueryAsync_SetsLivePreviewConfigFields()
{
var client = CreateClient();
client.ContentType("ctuid");
client.ContentType("ctuid").Entry("euid");
// Mock GetLivePreviewData to avoid actual HTTP call
var method = typeof(ContentstackClient).GetMethod("GetLivePreviewData", BindingFlags.NonPublic | BindingFlags.Instance);
method.Invoke(client, null); // Just to ensure method exists
var query = new Dictionary<string, string>
{
{ "live_preview", "hash" },
{ "release_id", "rid" },
{ "preview_timestamp", "ts" }
};
// Patch GetLivePreviewData to return a dummy JObject
// Since GetLivePreviewData is private and does a real HTTP call,
// we only test the config fields are set correctly before the call
await client.LivePreviewQueryAsync(query);
var v = client.GetLivePreviewConfig();
Assert.Equal("ctuid", GetPrivateField(client, "currentContenttypeUid"));
Assert.Equal("euid", GetPrivateField(client, "currentEntryUid"));
Assert.Equal(true, v.Enable );
Assert.Equal("rid", v.ReleaseId);
Assert.Equal("ts", v.PreviewTimestamp);
Assert.Equal("pt", v.PreviewToken);
}
// For SyncRecursive, SyncPaginationToken, SyncToken, you should mock HTTP calls.
// Here we just check that the methods exist and can be called (will throw if not configured).
[Fact]
public async Task SyncRecursive_ThrowsOrReturns()
{
var client = CreateClient();
await Assert.ThrowsAnyAsync<Exception>(() => client.SyncRecursive());
}
[Fact]
public async Task SyncPaginationToken_ThrowsOrReturns()
{
var client = CreateClient();
await Assert.ThrowsAnyAsync<Exception>(() => client.SyncPaginationToken("pagetoken"));
}
[Fact]
public async Task SyncToken_ThrowsOrReturns()
{
var client = CreateClient();
await Assert.ThrowsAnyAsync<Exception>(() => client.SyncToken("synctoken"));
}
}
}