-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_live_preview.py
More file actions
251 lines (223 loc) · 8.21 KB
/
test_live_preview.py
File metadata and controls
251 lines (223 loc) · 8.21 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
import unittest
import config
import contentstack
from contentstack.deep_merge_lp import DeepMergeMixin
management_token = config.MANAGEMENT_TOKEN
entry_uid = config.LIVE_PREVIEW_ENTRY_UID
preview_token = config.PREVIEW_TOKEN
_lp_query = {
'live_preview': '#0#0#0#0#0#0#0#0#0#',
'content_type_uid': 'product',
'entry_uid': entry_uid
}
_lp_preview_timestamp_query = {
'live_preview': '#0#0#0#0#0#0#0#0#0#',
'content_type_uid': 'product',
'entry_uid': entry_uid,
'preview_timestamp': '2025-03-07T12:00:00Z',
'release_id': '123456789'
}
_lp = {
'enable': True,
'host': 'api.contentstack.io',
'management_token': management_token
}
_lp_2_0 = {
'enable': True,
'preview_token': preview_token,
'host': 'rest-preview.contentstack.com'
}
API_KEY = config.APIKEY
DELIVERY_TOKEN = config.DELIVERYTOKEN
ENVIRONMENT = config.ENVIRONMENT
HOST = config.HOST
ENTRY_UID = config.APIKEY
class TestLivePreviewConfig(unittest.TestCase):
def setUp(self):
self.stack = contentstack.Stack(
API_KEY, DELIVERY_TOKEN,
ENVIRONMENT, host=HOST)
def test_live_preview_disabled(self):
self.stack = contentstack.Stack(
API_KEY,
DELIVERY_TOKEN,
ENVIRONMENT,
live_preview={
'enable': False,
'host': 'api.contentstack.io',
'management_token': 'string987654321'
})
self.stack.content_type('product').entry(entry_uid)
self.assertEqual(3, len(self.stack.get_live_preview))
self.assertFalse(self.stack.get_live_preview['enable'])
self.assertTrue(self.stack.get_live_preview['management_token'])
def test_021_live_preview_enabled(self):
self.stack = contentstack.Stack(
API_KEY,
DELIVERY_TOKEN,
ENVIRONMENT,
live_preview=_lp)
self.stack.live_preview_query(live_preview_query=_lp_query)
self.assertIsNotNone(self.stack.live_preview['management_token'])
self.assertEqual(7, len(self.stack.live_preview))
self.assertEqual('product', self.stack.live_preview['content_type_uid'])
def test_022_preview_timestamp_with_livepreview_2_0_enabled(self):
self.stack = contentstack.Stack(
API_KEY,
DELIVERY_TOKEN,
ENVIRONMENT,
live_preview=_lp_2_0)
self.stack.live_preview_query(live_preview_query=_lp_preview_timestamp_query)
self.assertIsNotNone(self.stack.live_preview['preview_token'])
self.assertEqual(9, len(self.stack.live_preview))
self.assertEqual('product', self.stack.live_preview['content_type_uid'])
self.assertEqual('123456789', self.stack.live_preview['release_id'])
self.assertEqual('2025-03-07T12:00:00Z', self.stack.live_preview['preview_timestamp'])
def test_023_livepreview_2_0_enabled(self):
self.stack = contentstack.Stack(
API_KEY,
DELIVERY_TOKEN,
ENVIRONMENT,
live_preview=_lp_2_0)
self.stack.live_preview_query(live_preview_query=_lp_query)
self.assertIsNotNone(self.stack.live_preview['preview_token'])
self.assertEqual(9, len(self.stack.live_preview))
self.assertEqual('product', self.stack.live_preview['content_type_uid'])
def test_03_set_host(self):
self.stack = contentstack.Stack(
API_KEY,
DELIVERY_TOKEN,
ENVIRONMENT,
live_preview=_lp)
self.assertEqual(7, len(self.stack.live_preview))
self.assertEqual(True, 'enable' in self.stack.live_preview)
def test_031_set_host_value(self):
self.stack = contentstack.Stack(
API_KEY,
DELIVERY_TOKEN,
ENVIRONMENT,
live_preview=_lp)
self.stack.live_preview_query(live_preview_query=_lp_query)
self.assertEqual(7, len(self.stack.live_preview))
self.assertIsNotNone(self.stack.live_preview['host'])
def test_06_live_preview_query(self):
self.stack = contentstack.Stack(
API_KEY,
DELIVERY_TOKEN,
ENVIRONMENT,
live_preview=_lp
)
self.assertEqual(7, len(self.stack.live_preview))
def test_07_branching(self):
stack = contentstack.Stack(
'api_key', 'delivery_token', 'environment', branch='dev_branch')
stack.content_type('product')
self.assertEqual('dev_branch', stack.get_branch)
def test_08_live_preview_query_hash_included(self):
self.stack = contentstack.Stack(
API_KEY,
DELIVERY_TOKEN,
ENVIRONMENT,
live_preview=_lp
)
self.stack.live_preview_query(live_preview_query=_lp_query)
self.assertEqual(7, len(self.stack.live_preview))
def test_09_live_preview_query_hash_excluded(self):
self.stack = contentstack.Stack(
API_KEY,
DELIVERY_TOKEN,
ENVIRONMENT,
live_preview=_lp
)
self.stack.live_preview_query(live_preview_query=_lp_query)
self.stack.content_type('product').entry(entry_uid=entry_uid)
self.assertEqual(3, len(self.stack.headers))
self.assertEqual(True, 'access_token' in self.stack.headers)
self.assertEqual(True, 'api_key' in self.stack.headers)
def test_10_live_preview_check_hash_value(self):
self.stack = contentstack.Stack(
API_KEY,
DELIVERY_TOKEN,
ENVIRONMENT,
live_preview=_lp
)
self.stack.live_preview_query(live_preview_query=_lp_query)
entry = self.stack.content_type('product').entry(entry_uid=ENTRY_UID)
resp = entry.fetch()
print(resp)
self.assertEqual(6, len(self.stack.headers))
self.assertEqual(API_KEY, self.stack.headers['api_key'])
self.assertEqual(DELIVERY_TOKEN, self.stack.headers['access_token'])
self.assertEqual(ENVIRONMENT, self.stack.headers['environment'])
lp_response = [
{
"uid": "76743678743",
"comment": "this belongs to live preview object",
"_version": 2,
"locale": "en-us",
"ACL": {},
"author": [
{
"uid": "77f3f0ea3630e06",
"_content_type_uid": "author"
}
]
},
{
"uid": "7634767463",
"_version": 2,
"locale": "en-us",
"comment": "this belongs to live preview object",
"ACL": {},
"author": [
{
"uid": "bltb77f3f0ea3630e06",
"_content_type_uid": "author"
}
]
}
]
entry_response = [
{
"uid": "76743678743",
"_version": 3,
"locale": "en-uk",
"title": "Updated Title",
"comment": "this belongs to entry object",
},
{
"uid": "47634767463",
"_version": 3,
"locale": "en-uk",
"title": "Updated Title",
"comment": "this belongs to entry object",
},
{
"uid": "34767463",
"_version": 3,
"locale": "en-us",
"comment": "this belongs to entry object",
"title": "You have received a new merged entry response"
}
]
class TestLivePreviewObject(unittest.TestCase):
def setUp(self):
self.stack = contentstack.Stack(
API_KEY, DELIVERY_TOKEN,
ENVIRONMENT, host=HOST)
def test_setup_live_preview(self):
self.stack = contentstack.Stack(API_KEY, DELIVERY_TOKEN, ENVIRONMENT, live_preview={
'enable': False,
'host': 'api.contentstack.io',
'management_token': 'string987654321'
})
self.stack.content_type('product').entry(entry_uid)
self.assertEqual(3, len(self.stack.get_live_preview))
self.assertFalse(self.stack.get_live_preview['enable'])
self.assertTrue(self.stack.get_live_preview['management_token'])
def test_deep_merge_object(self):
merged_response = DeepMergeMixin(entry_response, lp_response).to_dict()
self.assertTrue(isinstance(merged_response, list), "Merged response should be a list")
self.assertTrue(all(isinstance(entry, dict) for entry in merged_response), "Each item in merged_response should be a dictionary")
if __name__ == '__main__':
unittest.main()