contentful-management 0.0.1.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (188) hide show
  1. data/.gitignore +24 -0
  2. data/.reek +3 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +16 -0
  5. data/.travis.yml +9 -0
  6. data/CHANGELOG.md +2 -0
  7. data/Gemfile +13 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +408 -0
  10. data/Rakefile +37 -0
  11. data/bin/cma-console +8 -0
  12. data/contentful-management.gemspec +33 -0
  13. data/examples/blog.rb +82 -0
  14. data/examples/content_types.rb +45 -0
  15. data/examples/create_space.rb +35 -0
  16. data/lib/contentful/management.rb +12 -0
  17. data/lib/contentful/management/array.rb +35 -0
  18. data/lib/contentful/management/asset.rb +163 -0
  19. data/lib/contentful/management/client.rb +160 -0
  20. data/lib/contentful/management/content_type.rb +180 -0
  21. data/lib/contentful/management/dynamic_entry.rb +71 -0
  22. data/lib/contentful/management/entry.rb +213 -0
  23. data/lib/contentful/management/error.rb +62 -0
  24. data/lib/contentful/management/field.rb +34 -0
  25. data/lib/contentful/management/file.rb +15 -0
  26. data/lib/contentful/management/http_client.rb +22 -0
  27. data/lib/contentful/management/link.rb +23 -0
  28. data/lib/contentful/management/locale.rb +48 -0
  29. data/lib/contentful/management/location.rb +15 -0
  30. data/lib/contentful/management/request.rb +82 -0
  31. data/lib/contentful/management/resource.rb +166 -0
  32. data/lib/contentful/management/resource/array_like.rb +37 -0
  33. data/lib/contentful/management/resource/fields.rb +69 -0
  34. data/lib/contentful/management/resource/refresher.rb +21 -0
  35. data/lib/contentful/management/resource/system_properties.rb +56 -0
  36. data/lib/contentful/management/resource_builder.rb +269 -0
  37. data/lib/contentful/management/response.rb +72 -0
  38. data/lib/contentful/management/space.rb +159 -0
  39. data/lib/contentful/management/support.rb +24 -0
  40. data/lib/contentful/management/version.rb +6 -0
  41. data/spec/fixtures/vcr_cassettes/array_page_1.yml +1337 -0
  42. data/spec/fixtures/vcr_cassettes/asset/all.yml +611 -0
  43. data/spec/fixtures/vcr_cassettes/asset/archive.yml +228 -0
  44. data/spec/fixtures/vcr_cassettes/asset/archive_published.yml +177 -0
  45. data/spec/fixtures/vcr_cassettes/asset/archived_false.yml +228 -0
  46. data/spec/fixtures/vcr_cassettes/asset/archived_true.yml +228 -0
  47. data/spec/fixtures/vcr_cassettes/asset/create.yml +150 -0
  48. data/spec/fixtures/vcr_cassettes/asset/create_with_already_used_id.yml +56 -0
  49. data/spec/fixtures/vcr_cassettes/asset/create_with_custom_id.yml +150 -0
  50. data/spec/fixtures/vcr_cassettes/asset/destroy.yml +159 -0
  51. data/spec/fixtures/vcr_cassettes/asset/destroy_published.yml +175 -0
  52. data/spec/fixtures/vcr_cassettes/asset/find.yml +108 -0
  53. data/spec/fixtures/vcr_cassettes/asset/find_not_found.yml +64 -0
  54. data/spec/fixtures/vcr_cassettes/asset/image_url.yml +133 -0
  55. data/spec/fixtures/vcr_cassettes/asset/locale.yml +110 -0
  56. data/spec/fixtures/vcr_cassettes/asset/publish.yml +228 -0
  57. data/spec/fixtures/vcr_cassettes/asset/publish_already_published.yml +176 -0
  58. data/spec/fixtures/vcr_cassettes/asset/published_false.yml +228 -0
  59. data/spec/fixtures/vcr_cassettes/asset/published_true.yml +226 -0
  60. data/spec/fixtures/vcr_cassettes/asset/save_update.yml +242 -0
  61. data/spec/fixtures/vcr_cassettes/asset/set_locale.yml +110 -0
  62. data/spec/fixtures/vcr_cassettes/asset/unarchive.yml +228 -0
  63. data/spec/fixtures/vcr_cassettes/asset/unarchive_already_unarchive.yml +168 -0
  64. data/spec/fixtures/vcr_cassettes/asset/unarchive_already_unarchived.yml +168 -0
  65. data/spec/fixtures/vcr_cassettes/asset/unpublish.yml +228 -0
  66. data/spec/fixtures/vcr_cassettes/asset/unpublish_already_unpublished.yml +168 -0
  67. data/spec/fixtures/vcr_cassettes/asset/update_file.yml +207 -0
  68. data/spec/fixtures/vcr_cassettes/asset/update_to_specified_locale.yml +224 -0
  69. data/spec/fixtures/vcr_cassettes/asset/update_with_default_locale_without_file.yml +214 -0
  70. data/spec/fixtures/vcr_cassettes/content_type/activate.yml +246 -0
  71. data/spec/fixtures/vcr_cassettes/content_type/activate_with_invalid_version.yml +186 -0
  72. data/spec/fixtures/vcr_cassettes/content_type/activated_false.yml +246 -0
  73. data/spec/fixtures/vcr_cassettes/content_type/activated_true.yml +257 -0
  74. data/spec/fixtures/vcr_cassettes/content_type/all.yml +190 -0
  75. data/spec/fixtures/vcr_cassettes/content_type/create.yml +87 -0
  76. data/spec/fixtures/vcr_cassettes/content_type/create_content_type_with_id.yml +87 -0
  77. data/spec/fixtures/vcr_cassettes/content_type/create_with_Array_field.yml +94 -0
  78. data/spec/fixtures/vcr_cassettes/content_type/create_with_Boolean_field.yml +94 -0
  79. data/spec/fixtures/vcr_cassettes/content_type/create_with_Date_field.yml +94 -0
  80. data/spec/fixtures/vcr_cassettes/content_type/create_with_Integer_field.yml +94 -0
  81. data/spec/fixtures/vcr_cassettes/content_type/create_with_Link_field.yml +95 -0
  82. data/spec/fixtures/vcr_cassettes/content_type/create_with_Location_field.yml +94 -0
  83. data/spec/fixtures/vcr_cassettes/content_type/create_with_Number_field.yml +94 -0
  84. data/spec/fixtures/vcr_cassettes/content_type/create_with_Object_field.yml +94 -0
  85. data/spec/fixtures/vcr_cassettes/content_type/create_with_Symbol_field.yml +94 -0
  86. data/spec/fixtures/vcr_cassettes/content_type/create_with_Text_field.yml +94 -0
  87. data/spec/fixtures/vcr_cassettes/content_type/deactivate.yml +246 -0
  88. data/spec/fixtures/vcr_cassettes/content_type/deactivate_already_deactivated.yml +176 -0
  89. data/spec/fixtures/vcr_cassettes/content_type/deactivate_with_entries.yml +236 -0
  90. data/spec/fixtures/vcr_cassettes/content_type/deactivate_with_version_change.yml +248 -0
  91. data/spec/fixtures/vcr_cassettes/content_type/destroy.yml +148 -0
  92. data/spec/fixtures/vcr_cassettes/content_type/destroy_activated.yml +164 -0
  93. data/spec/fixtures/vcr_cassettes/content_type/entry/create.yml +1344 -0
  94. data/spec/fixtures/vcr_cassettes/content_type/entry/create_with_camel_case_id_to_multiple_locales.yml +235 -0
  95. data/spec/fixtures/vcr_cassettes/content_type/entry/create_with_entries.yml +398 -0
  96. data/spec/fixtures/vcr_cassettes/content_type/entry/create_with_entries_for_multiple_locales.yml +3157 -0
  97. data/spec/fixtures/vcr_cassettes/content_type/entry/create_with_multiple_locales.yml +230 -0
  98. data/spec/fixtures/vcr_cassettes/content_type/fields/add.yml +345 -0
  99. data/spec/fixtures/vcr_cassettes/content_type/fields/create.yml +353 -0
  100. data/spec/fixtures/vcr_cassettes/content_type/fields/create_array_with_params.yml +228 -0
  101. data/spec/fixtures/vcr_cassettes/content_type/fields/create_with_params.yml +221 -0
  102. data/spec/fixtures/vcr_cassettes/content_type/fields/destroy.yml +349 -0
  103. data/spec/fixtures/vcr_cassettes/content_type/fields/update_field.yml +358 -0
  104. data/spec/fixtures/vcr_cassettes/content_type/find.yml +180 -0
  105. data/spec/fixtures/vcr_cassettes/content_type/find_not_found.yml +64 -0
  106. data/spec/fixtures/vcr_cassettes/content_type/save_new.yml +876 -0
  107. data/spec/fixtures/vcr_cassettes/content_type/save_updated.yml +345 -0
  108. data/spec/fixtures/vcr_cassettes/content_type/save_with_added_field.yml +217 -0
  109. data/spec/fixtures/vcr_cassettes/content_type/update.yml +175 -0
  110. data/spec/fixtures/vcr_cassettes/content_type/update_change_field_name.yml +222 -0
  111. data/spec/fixtures/vcr_cassettes/content_type/update_remove_field.yml +231 -0
  112. data/spec/fixtures/vcr_cassettes/content_type/update_with_fields.yml +222 -0
  113. data/spec/fixtures/vcr_cassettes/content_type/update_with_one_new_field.yml +251 -0
  114. data/spec/fixtures/vcr_cassettes/entry/all.yml +139 -0
  115. data/spec/fixtures/vcr_cassettes/entry/archive.yml +222 -0
  116. data/spec/fixtures/vcr_cassettes/entry/archive_published.yml +187 -0
  117. data/spec/fixtures/vcr_cassettes/entry/archived_false.yml +178 -0
  118. data/spec/fixtures/vcr_cassettes/entry/archived_true.yml +248 -0
  119. data/spec/fixtures/vcr_cassettes/entry/create.yml +245 -0
  120. data/spec/fixtures/vcr_cassettes/entry/create_test.yml +150 -0
  121. data/spec/fixtures/vcr_cassettes/entry/create_with_asset.yml +403 -0
  122. data/spec/fixtures/vcr_cassettes/entry/create_with_assets.yml +419 -0
  123. data/spec/fixtures/vcr_cassettes/entry/create_with_entries.yml +418 -0
  124. data/spec/fixtures/vcr_cassettes/entry/create_with_entry.yml +493 -0
  125. data/spec/fixtures/vcr_cassettes/entry/create_with_location.yml +259 -0
  126. data/spec/fixtures/vcr_cassettes/entry/create_with_symbols.yml +271 -0
  127. data/spec/fixtures/vcr_cassettes/entry/destory_published.yml +172 -0
  128. data/spec/fixtures/vcr_cassettes/entry/destroy.yml +156 -0
  129. data/spec/fixtures/vcr_cassettes/entry/find.yml +129 -0
  130. data/spec/fixtures/vcr_cassettes/entry/find_not_found.yml +64 -0
  131. data/spec/fixtures/vcr_cassettes/entry/publish.yml +248 -0
  132. data/spec/fixtures/vcr_cassettes/entry/publish_already_published.yml +186 -0
  133. data/spec/fixtures/vcr_cassettes/entry/published_false.yml +248 -0
  134. data/spec/fixtures/vcr_cassettes/entry/published_true.yml +257 -0
  135. data/spec/fixtures/vcr_cassettes/entry/save_update.yml +211 -0
  136. data/spec/fixtures/vcr_cassettes/entry/unarchive.yml +248 -0
  137. data/spec/fixtures/vcr_cassettes/entry/unarchive_already_unarchived.yml +178 -0
  138. data/spec/fixtures/vcr_cassettes/entry/unpublish.yml +248 -0
  139. data/spec/fixtures/vcr_cassettes/entry/unpublish_already_unpublished.yml +178 -0
  140. data/spec/fixtures/vcr_cassettes/entry/update.yml +626 -0
  141. data/spec/fixtures/vcr_cassettes/entry/update_unlocalized_field.yml +308 -0
  142. data/spec/fixtures/vcr_cassettes/entry/update_with_custom_locale.yml +332 -0
  143. data/spec/fixtures/vcr_cassettes/get_request.yml +37 -0
  144. data/spec/fixtures/vcr_cassettes/locale/all_for_space.yml +321 -0
  145. data/spec/fixtures/vcr_cassettes/locale/create_for_space.yml +95 -0
  146. data/spec/fixtures/vcr_cassettes/locale/find.yml +95 -0
  147. data/spec/fixtures/vcr_cassettes/locale/find_for_space_not_found.yml +67 -0
  148. data/spec/fixtures/vcr_cassettes/space/all.yml +1201 -0
  149. data/spec/fixtures/vcr_cassettes/space/asset/all.yml +475 -0
  150. data/spec/fixtures/vcr_cassettes/space/asset/assets.yml +826 -0
  151. data/spec/fixtures/vcr_cassettes/space/asset/create.yml +508 -0
  152. data/spec/fixtures/vcr_cassettes/space/asset/create_with_multiple_locales.yml +1062 -0
  153. data/spec/fixtures/vcr_cassettes/space/asset/find.yml +634 -0
  154. data/spec/fixtures/vcr_cassettes/space/content_type/all.yml +470 -0
  155. data/spec/fixtures/vcr_cassettes/space/content_type/content_types.yml +341 -0
  156. data/spec/fixtures/vcr_cassettes/space/content_type/create.yml +425 -0
  157. data/spec/fixtures/vcr_cassettes/space/content_type/find.yml +493 -0
  158. data/spec/fixtures/vcr_cassettes/space/create.yml +85 -0
  159. data/spec/fixtures/vcr_cassettes/space/create_when_limit_has_been_reached.yml +67 -0
  160. data/spec/fixtures/vcr_cassettes/space/create_with_unknown_organization.yml +65 -0
  161. data/spec/fixtures/vcr_cassettes/space/create_without_organization.yml +83 -0
  162. data/spec/fixtures/vcr_cassettes/space/destory.yml +196 -0
  163. data/spec/fixtures/vcr_cassettes/space/entry/all.yml +634 -0
  164. data/spec/fixtures/vcr_cassettes/space/entry/entries.yml +498 -0
  165. data/spec/fixtures/vcr_cassettes/space/entry/find.yml +624 -0
  166. data/spec/fixtures/vcr_cassettes/space/find.yml +270 -0
  167. data/spec/fixtures/vcr_cassettes/space/find_not_found.yml +67 -0
  168. data/spec/fixtures/vcr_cassettes/space/locale/all.yml +690 -0
  169. data/spec/fixtures/vcr_cassettes/space/locale/create.yml +572 -0
  170. data/spec/fixtures/vcr_cassettes/space/locale/create_with_the_same_code.yml +585 -0
  171. data/spec/fixtures/vcr_cassettes/space/locale/find.yml +572 -0
  172. data/spec/fixtures/vcr_cassettes/space/locale/find_not_found.yml +580 -0
  173. data/spec/fixtures/vcr_cassettes/space/locale/locales.yml +480 -0
  174. data/spec/fixtures/vcr_cassettes/space/locale/update.yml +700 -0
  175. data/spec/fixtures/vcr_cassettes/space/save_new_space.yml +67 -0
  176. data/spec/fixtures/vcr_cassettes/space/save_update_space.yml +352 -0
  177. data/spec/fixtures/vcr_cassettes/space/update.yml +350 -0
  178. data/spec/fixtures/vcr_cassettes/space/update_with_the_same_data.yml +350 -0
  179. data/spec/lib/contentful/management/array_spec.rb +59 -0
  180. data/spec/lib/contentful/management/asset_spec.rb +321 -0
  181. data/spec/lib/contentful/management/client_spec.rb +80 -0
  182. data/spec/lib/contentful/management/content_type_spec.rb +455 -0
  183. data/spec/lib/contentful/management/entry_spec.rb +308 -0
  184. data/spec/lib/contentful/management/locale_spec.rb +53 -0
  185. data/spec/lib/contentful/management/space_spec.rb +317 -0
  186. data/spec/spec_helper.rb +10 -0
  187. data/spec/support/vcr.rb +17 -0
  188. metadata +554 -0
@@ -0,0 +1,80 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'contentful/management/client'
4
+
5
+ module Contentful
6
+ module Management
7
+ describe Client do
8
+ let(:token) { '<ACCESS_TOKEN>' }
9
+ let(:client) { Client.new(token) }
10
+
11
+ subject { client }
12
+
13
+ its(:access_token) { should be token }
14
+
15
+ describe 'headers' do
16
+ describe '#authentication_header' do
17
+ its(:authentication_header) { should be_kind_of Hash }
18
+ its(:authentication_header) { should eql 'Authorization' => 'Bearer <ACCESS_TOKEN>' }
19
+ end
20
+
21
+ describe '#api_version_header' do
22
+ its(:api_version_header) { should be_kind_of Hash }
23
+ its(:api_version_header) { should eql 'Content-Type' => 'application/vnd.contentful.management.v1+json' }
24
+ end
25
+
26
+ describe '#request_headers' do
27
+ its(:request_headers) { should be_kind_of Hash }
28
+ its(:request_headers) { should include client.authentication_header }
29
+ its(:request_headers) { should include client.api_version_header }
30
+ its(:request_headers) { should include client.user_agent }
31
+ end
32
+
33
+ describe '#user_agent' do
34
+ its(:user_agent) { should be_kind_of Hash }
35
+ its(:user_agent) { should eq 'User-Agent' => "RubyContenfulManagementGem/#{ Contentful::Management::VERSION }" }
36
+ end
37
+
38
+ describe '#organization_header' do
39
+ it 'is a hash' do
40
+ expect(client.organization_header('MyOrganizationID')).to be_kind_of Hash
41
+ end
42
+
43
+ it 'returns the "X-Contentful-Organization" header' do
44
+ expect(client.organization_header('MyOrganizationID'))
45
+ .to eql 'X-Contentful-Organization' => 'MyOrganizationID'
46
+ end
47
+ end
48
+ end
49
+
50
+ describe '#protocol' do
51
+ its(:protocol) { should eql 'https' }
52
+
53
+ it 'is http when secure set to true' do
54
+ client = Client.new('token', secure: true)
55
+ expect(client.protocol).to eql 'https'
56
+ end
57
+
58
+ it 'is http when secure set to false' do
59
+ client = Client.new('token', secure: false)
60
+ expect(client.protocol).to eql 'http'
61
+ end
62
+ end
63
+
64
+ describe '#default_locale' do
65
+ it 'is http when secure set to true' do
66
+ client = Client.new('token', secure: true)
67
+ expect(client.default_locale).to eql 'en-US'
68
+ end
69
+ end
70
+
71
+ describe '.get_http' do
72
+ subject { Client }
73
+ it 'does a GET request' do
74
+ vcr(:get_request) { subject.get_http('http://example.com', foo: 'bar') }
75
+ end
76
+ end
77
+
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,455 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'contentful/management/space'
4
+ require 'contentful/management/client'
5
+
6
+ module Contentful
7
+ module Management
8
+ describe ContentType do
9
+ let(:token) { '<ACCESS_TOKEN>' }
10
+ let(:space_id) { 'yr5m0jky5hsh' }
11
+ let!(:client) { Client.new(token) }
12
+ let(:content_type_id) { '5DSpuKrl04eMAGQoQckeIq' }
13
+
14
+ subject { Contentful::Management::ContentType }
15
+
16
+ describe '.all' do
17
+ it 'returns a Contentful::Array' do
18
+ vcr('content_type/all') { expect(subject.all(space_id)).to be_kind_of Contentful::Management::Array }
19
+ end
20
+ it 'builds a Contentful::Management::ContentType object' do
21
+ vcr('content_type/all') { expect(subject.all(space_id).first).to be_kind_of Contentful::Management::ContentType }
22
+ end
23
+ end
24
+
25
+ describe '#find' do
26
+ it 'returns a Contentful::Management::ContentType' do
27
+ vcr('content_type/find') { expect(subject.find(space_id, content_type_id)).to be_kind_of Contentful::Management::ContentType }
28
+ end
29
+ it 'returns the content_type for a given key' do
30
+ vcr('content_type/find') do
31
+ content_type = subject.find(space_id, content_type_id)
32
+ expect(content_type.id).to eql content_type_id
33
+ end
34
+ end
35
+ it 'returns an error when content_type does not found' do
36
+ vcr('content_type/find_not_found') do
37
+ result = subject.find(space_id, 'not_exist')
38
+ expect(result).to be_kind_of Contentful::Management::NotFound
39
+ end
40
+ end
41
+ end
42
+
43
+ describe '#destroy' do
44
+ it 'returns Contentful::BadRequest error when content type is activated' do
45
+ vcr('content_type/destroy_activated') do
46
+ result = subject.find(space_id, '66jvD8UhNKmWGk24KKq0EW').destroy
47
+ expect(result).to be_kind_of Contentful::Management::BadRequest
48
+ expect(result.message).to eq 'Cannot deleted published'
49
+ end
50
+ end
51
+
52
+ it 'returns true when content type is not activated' do
53
+ vcr('content_type/destroy') do
54
+ result = subject.find(space_id, '66jvD8UhNKmWGk24KKq0EW').destroy
55
+ expect(result).to eql true
56
+ end
57
+ end
58
+ end
59
+
60
+ describe '#activate' do
61
+ let(:active_content) { '4EnwylPOikyMGUIy8uQgQY' }
62
+ it 'returns Contentful::Management::ContentType' do
63
+ vcr('content_type/activate') do
64
+ result = subject.find(space_id, active_content).activate
65
+ expect(result).to be_kind_of Contentful::Management::ContentType
66
+ end
67
+ end
68
+ it 'increases object version' do
69
+ vcr('content_type/activate') do
70
+ content_type = subject.find(space_id, active_content)
71
+ initial_version = content_type.sys[:version]
72
+ content_type.activate
73
+ expect(content_type.sys[:version]).to eql initial_version + 1
74
+ end
75
+ end
76
+ it 'returns error when not valid version' do
77
+ vcr('content_type/activate_with_invalid_version') do
78
+ content_type = subject.find(space_id, active_content)
79
+ content_type.sys[:version] = -1
80
+ result = content_type.activate
81
+ expect(result).to be_kind_of Contentful::Management::BadRequest
82
+ end
83
+ end
84
+ end
85
+
86
+ describe '#deactivate' do
87
+ let(:deactivate_content) { '4EnwylPOikyMGUIy8uQgQY' }
88
+ it 'returns Contentful::Management::ContentType' do
89
+ vcr('content_type/deactivate') do
90
+ content_type = subject.find(space_id, deactivate_content)
91
+ result = content_type.deactivate
92
+ expect(result).to be_kind_of Contentful::Management::ContentType
93
+ end
94
+ end
95
+ it 'increases object version' do
96
+ vcr('content_type/deactivate_with_version_change') do
97
+ content_type = subject.find(space_id, deactivate_content)
98
+ initial_version = content_type.sys[:version]
99
+ content_type.activate
100
+ expect(content_type.sys[:version]).to eql initial_version + 1
101
+ end
102
+ end
103
+ it 'returns error when has entries' do
104
+ vcr('content_type/deactivate_with_entries') do
105
+ result = subject.find(space_id, '5DSpuKrl04eMAGQoQckeIq').deactivate
106
+ expect(result).to be_kind_of Contentful::Management::BadRequest
107
+ end
108
+ end
109
+ it 'returns error message when already deactivated' do
110
+ vcr('content_type/deactivate_already_deactivated') do
111
+ content_type = subject.find(space_id, deactivate_content)
112
+ content_type.sys[:version] = -1
113
+ result = content_type.deactivate
114
+ expect(result.message).to eq 'Not published'
115
+ end
116
+ end
117
+ end
118
+
119
+ describe '#active?' do
120
+ it 'returns true if content_type is active' do
121
+ vcr('content_type/activated_true') do
122
+ content_type = subject.find(space_id, '4EnwylPOikyMGUIy8uQgQY')
123
+ content_type.activate
124
+ expect(content_type.active?).to be_truthy
125
+ end
126
+ end
127
+ it 'returns false if content_type is not active' do
128
+ vcr('content_type/activated_false') do
129
+ content_type = subject.find(space_id, '4EnwylPOikyMGUIy8uQgQY')
130
+ content_type.deactivate
131
+ expect(content_type.active?).to be_falsey
132
+ end
133
+ end
134
+ end
135
+
136
+ describe '.create' do
137
+ let(:content_type_name) { 'Blog' }
138
+ let(:content_type_description) { 'Blog content type' }
139
+
140
+ it 'creates a content_type within a space without id and without fields' do
141
+ vcr('content_type/create') do
142
+ content_type = Contentful::Management::ContentType.create(space_id, name: content_type_name, description: content_type_description)
143
+ expect(content_type).to be_kind_of Contentful::Management::ContentType
144
+ expect(content_type.name).to eq content_type_name
145
+ expect(content_type.description).to eq content_type_description
146
+ end
147
+ end
148
+
149
+ it 'creates a content_type within a space with custom id and without fields' do
150
+ vcr('content_type/create_content_type_with_id') do
151
+ content_type_id = 'custom_id'
152
+ content_type = Contentful::Management::ContentType.create(space_id, { name: content_type_name, id: content_type_id })
153
+ expect(content_type).to be_kind_of Contentful::Management::ContentType
154
+ expect(content_type.name).to eq content_type_name
155
+ expect(content_type.id).to eq content_type_id
156
+ end
157
+ end
158
+
159
+ Contentful::Management::ContentType::FIELD_TYPES.each do |field_type|
160
+ it "creates within a space with #{ field_type } field" do
161
+ vcr("content_type/create_with_#{ field_type }_field") do
162
+ field = Contentful::Management::Field.new
163
+ field.id = "my_#{ field_type }_field"
164
+ field.name = "My #{ field_type } Field"
165
+ field.type = field_type
166
+ field.link_type = 'Entry' if field_type == 'Link'
167
+ content_type = Contentful::Management::ContentType.create(space_id, name: "#{ field_type }", description: "Content type with #{ field_type } field", fields: [field])
168
+ expect(content_type).to be_kind_of Contentful::Management::ContentType
169
+ expect(content_type.name).to eq "#{ field_type }"
170
+ expect(content_type.description).to eq "Content type with #{ field_type } field"
171
+ expect(content_type.fields.size).to eq 1
172
+ result_field = content_type.fields.first
173
+ expect(result_field.id).to eq field.id
174
+ expect(result_field.name).to eq field.name
175
+ expect(result_field.type).to eq field.type
176
+ end
177
+ end
178
+ end
179
+ end
180
+
181
+ describe '#update' do
182
+ let(:content_type_name) { 'Blog Content' }
183
+ let(:content_type_description) { 'Blogs content type' }
184
+ let(:content_type_id) { 'qw3F2rn3FeoOiceqAiCSC' }
185
+ it 'updates content_type name and description' do
186
+ vcr('content_type/update') do
187
+ content_type = subject.find(space_id, content_type_id)
188
+ content_type.update(name: content_type_name, description: content_type_description)
189
+ expect(content_type.name).to eq content_type_name
190
+ expect(content_type.description).to eq content_type_description
191
+ end
192
+ end
193
+
194
+ it 'updates content_type with fields (leave fields untouched)' do
195
+ vcr('content_type/update_with_fields') do
196
+ content_type = subject.find(space_id, content_type_id)
197
+ content_type.update(name: content_type_name)
198
+ expect(content_type.name).to eq content_type_name
199
+ expect(content_type.fields.size).to eq 2
200
+ end
201
+ end
202
+
203
+ it 'updates content_type adding one field' do
204
+ vcr('content_type/update_with_one_new_field') do
205
+ field = Contentful::Management::Field.new
206
+ field.id = 'blog_author'
207
+ field.name = 'Author of blog'
208
+ field.type = 'Text'
209
+ content_type = subject.find(space_id, content_type_id)
210
+ content_type.update(fields: content_type.fields + [field])
211
+ expect(content_type.fields.size).to eq 5
212
+ end
213
+ end
214
+
215
+ it 'updates content_type updating existing field' do
216
+ vcr('content_type/update_change_field_name') do
217
+ new_field_name = 'blog_author'
218
+ content_type = subject.find(space_id, content_type_id)
219
+ field = content_type.fields.first
220
+ field.name = new_field_name
221
+ content_type.update(fields: content_type.fields)
222
+ expect(content_type.fields.size).to eq 2
223
+ expect(content_type.fields.first.name).to eq new_field_name
224
+ end
225
+ end
226
+
227
+ it 'updates content_type deleting existing field' do
228
+ vcr('content_type/update_remove_field') do
229
+ content_type = subject.find(space_id, content_type_id)
230
+ field = content_type.fields.first
231
+ content_type.update(fields: [field])
232
+ expect(content_type.fields.size).to eq 1
233
+ end
234
+ end
235
+ end
236
+
237
+ describe '#save' do
238
+ it 'updated content type' do
239
+ vcr('content_type/save_updated') do
240
+ content_type = subject.find(space_id, content_type_id)
241
+ content_type.name = 'NewName'
242
+ content_type.save
243
+ expect(content_type).to be_kind_of Contentful::Management::ContentType
244
+ expect(content_type.name).to eq 'NewName'
245
+ end
246
+ end
247
+
248
+ it 'with new field' do
249
+ vcr('content_type/save_with_added_field') do
250
+ content_type = subject.find(space_id, '2tDzYAg5MM6sIkwsOmM0Kc')
251
+ field = Contentful::Management::Field.new
252
+ field.id = 'blog_title'
253
+ field.name = 'Blog Title'
254
+ field.type = 'Text'
255
+ content_type.fields = content_type.fields + [field]
256
+ content_type.save
257
+ expect(content_type).to be_kind_of Contentful::Management::ContentType
258
+ expect(content_type.name).to eq 'Blog testing'
259
+ expect(content_type.fields.size).to eq 2
260
+ end
261
+ end
262
+
263
+ it 'saves new object' do
264
+ vcr('content_type/save_new') do
265
+ space = Contentful::Management::Space.find(space_id)
266
+ content_type = space.content_types.new
267
+ content_type.name = 'Post title'
268
+ field = Contentful::Management::Field.new
269
+ field.id = 'my_text_field'
270
+ field.name = 'My Text Field'
271
+ field.type = 'Text'
272
+ content_type.fields = [field]
273
+ content_type.save
274
+ expect(content_type).to be_kind_of Contentful::Management::ContentType
275
+ expect(content_type.name).to eq 'Post title'
276
+ expect(content_type.fields.size).to eq 1
277
+ end
278
+ end
279
+ end
280
+
281
+ describe '#fields.create' do
282
+ let(:field_id) { 'eye_color' }
283
+ let(:field_type) { 'Text' }
284
+ it 'creates new field' do
285
+ vcr('content_type/fields/create') do
286
+ content_type = subject.find(space_id, content_type_id)
287
+ content_type.fields.create(id: field_id, name: 'Eye color', type: field_type)
288
+ expect(content_type.fields.size).to eq 12
289
+ end
290
+ end
291
+ it 'creates new Link field with additional parameters' do
292
+ vcr('content_type/fields/create_with_params') do
293
+ content_type = subject.find(space_id, 'qw3F2rn3FeoOiceqAiCSC')
294
+ content_type.fields.create(id: 'blog_avatar', name: 'Blog avatar', type: 'Link', link_type: 'Asset', localized: true, required: true)
295
+ expect(content_type.fields.size).to eq 2
296
+ field = content_type.fields.last
297
+ expect(field.name).to eq 'Blog avatar'
298
+ expect(field.type).to eq 'Link'
299
+ expect(field.link_type).to eq 'Asset'
300
+ expect(field.localized).to be_truthy
301
+ expect(field.required).to be_truthy
302
+ end
303
+ end
304
+ it 'creates new Array field with additional parameters' do
305
+ vcr('content_type/fields/create_array_with_params') do
306
+ content_type = subject.find(space_id, '6xzrdCr33OMAeIYUgs6UKi')
307
+ items = Contentful::Management::Field.new
308
+ items.type = 'Link'
309
+ items.link_type = 'Entry'
310
+ content_type.fields.create(id: 'blog_entries', name: 'Entries', type: 'Array', localized: true, items: items)
311
+ expect(content_type.fields.size).to eq 2
312
+ field = content_type.fields.last
313
+ expect(field.name).to eq 'Entries'
314
+ expect(field.type).to eq 'Array'
315
+ expect(field.items.type).to eq items.type
316
+ end
317
+ end
318
+ it 'updates existing field if matched id' do
319
+ vcr('content_type/fields/update_field') do
320
+ content_type = subject.find(space_id, '5DSpuKrl04eMAGQoQckeIq')
321
+ updated_name = 'Eyes color'
322
+ content_type.fields.create(id: field_id, name: updated_name, type: field_type)
323
+ expect(content_type.fields.size).to eq 12
324
+ expect(content_type.fields[11].name).to eq updated_name
325
+ expect(content_type.fields[11].type).to eq field_type
326
+ end
327
+ end
328
+ end
329
+
330
+ describe '#fields.add' do
331
+ it 'creates new field' do
332
+ vcr('content_type/fields/add') do
333
+ content_type = subject.find(space_id, content_type_id)
334
+ field = Contentful::Management::Field.new
335
+ field.id = 'symbol'
336
+ field.name = 'Symbol'
337
+ field.type = 'Symbol'
338
+ field.localized = true
339
+ field.required = true
340
+ content_type.fields.add(field)
341
+ expect(content_type.fields.size).to eq 11
342
+ end
343
+ end
344
+ end
345
+
346
+ describe '#fields.destroy' do
347
+ it 'deletes field by id' do
348
+ vcr('content_type/fields/destroy') do
349
+ content_type = subject.find(space_id, content_type_id)
350
+ content_type.fields.destroy('blog_title')
351
+ expect(content_type.fields.size).to eq 10
352
+ end
353
+ end
354
+ end
355
+
356
+ describe '#entries.create' do
357
+ it 'with Text field' do
358
+ vcr('content_type/entry/create') do
359
+ content_type = subject.find(space_id, content_type_id)
360
+ entry = content_type.entries.create(name: 'Piotrek')
361
+ expect(entry).to be_kind_of Contentful::Management::Entry
362
+ expect(entry.fields[:name]).to eq 'Piotrek'
363
+ end
364
+ end
365
+
366
+ it 'with entry' do
367
+ vcr('content_type/entry/create_with_entries') do
368
+ entry_en = Entry.find(space_id, 'Qa8TW5nPWgiU4MA6AGYgq')
369
+ content_type = subject.find(space_id, '6xzrdCr33OMAeIYUgs6UKi')
370
+ entry = content_type.entries.create(blog_name: 'Piotrek', blog_entry: entry_en, blog_entries: [entry_en, entry_en, entry_en])
371
+ expect(entry).to be_kind_of Contentful::Management::Entry
372
+ expect(entry.blog_name).to eq 'Piotrek'
373
+ expect(entry.fields[:blog_entry]['sys']['id']).to eq 'Qa8TW5nPWgiU4MA6AGYgq'
374
+ expect(entry.fields[:blog_entries].first['sys']['id']).to eq 'Qa8TW5nPWgiU4MA6AGYgq'
375
+ end
376
+ end
377
+ end
378
+
379
+ describe '#entries.new' do
380
+ context 'for multiple locales' do
381
+ it 'for Text field' do
382
+ vcr('content_type/entry/create_with_multiple_locales') do
383
+ content_type = subject.find(space_id, '4EnwylPOikyMGUIy8uQgQY')
384
+ entry = content_type.entries.new
385
+ entry.post_title_with_locales = { 'en-US' => 'Company logo', 'pl' => 'Firmowe logo' }
386
+ entry.post_body_with_locales = { 'en-US' => 'Story about Contentful...', 'pl' => 'Historia o Contentful...' }
387
+ entry.save
388
+
389
+ expect(entry).to be_kind_of Contentful::Management::Entry
390
+ expect(entry.post_title).to eq 'Company logo'
391
+ expect(entry.post_body).to eq 'Story about Contentful...'
392
+ entry.locale = 'pl'
393
+ expect(entry.post_title).to eq 'Firmowe logo'
394
+ expect(entry.post_body).to eq 'Historia o Contentful...'
395
+ end
396
+ end
397
+ it 'with camel case api id' do
398
+ vcr('content_type/entry/create_with_camel_case_id_to_multiple_locales') do
399
+ content_type = subject.find(space_id, '4esHTHIVgc0uWkiwGwOsa6')
400
+ entry = content_type.entries.new
401
+ entry.car_mark_with_locales = { 'en-US' => 'Mercedes Benz', 'pl' => 'Mercedes' }
402
+ entry.car_city_plate_with_locales = { 'en-US' => 'en', 'pl' => 'bia' }
403
+ entry.car_capacity_with_locales = { 'en-US' => 2.5, 'pl' => 2.5 }
404
+ entry.save
405
+
406
+ expect(entry).to be_kind_of Contentful::Management::Entry
407
+ expect(entry.car_mark).to eq 'Mercedes Benz'
408
+ expect(entry.car_city_plate).to eq 'en'
409
+ expect(entry.car_capacity).to eq 2.5
410
+ entry.locale = 'pl'
411
+ expect(entry.car_mark).to eq 'Mercedes'
412
+ expect(entry.car_city_plate).to eq 'bia'
413
+ expect(entry.car_capacity).to eq 2.5
414
+ end
415
+ end
416
+ it 'with entries' do
417
+ vcr('content_type/entry/create_with_entries_for_multiple_locales') do
418
+ space = Contentful::Management::Space.find(space_id)
419
+ space.content_types #filling cache
420
+
421
+ entry_en = space.entries.find('664EPJ6zHqAeMO6O0mGggU')
422
+ entry_pl = space.entries.find('664EPJ6zHqAeMO6O0mGggU')
423
+
424
+ content_type = space.content_types.find('6xzrdCr33OMAeIYUgs6UKi')
425
+ entry = content_type.entries.new
426
+ entry.blog_name_with_locales = { 'en-US' => 'Contentful en', 'pl' => 'Contentful pl' }
427
+ entry.blog_entries_with_locales = { 'en-US' => [entry_en, entry_en], 'pl' => [entry_pl, entry_pl] }
428
+ entry.blog_entry_with_locales = { 'en-US' => entry_en, 'pl' => entry_pl }
429
+ entry.save
430
+ expect(entry.blog_name).to eq 'Contentful en'
431
+ end
432
+ end
433
+
434
+ it 'with assets' do
435
+ vcr('content_type/entry/create_with_entries_for_multiple_locales') do
436
+ space = Contentful::Management::Space.find(space_id)
437
+ space.content_types #filling cache
438
+
439
+ entry_en = space.entries.find('664EPJ6zHqAeMO6O0mGggU')
440
+ entry_pl = space.entries.find('664EPJ6zHqAeMO6O0mGggU')
441
+
442
+ content_type = space.content_types.find('6xzrdCr33OMAeIYUgs6UKi')
443
+ entry = content_type.entries.new
444
+ entry.blog_name_with_locales = { 'en-US' => 'Contentful en', 'pl' => 'Contentful pl' }
445
+ entry.blog_entries_with_locales = { 'en-US' => [entry_en, entry_en], 'pl' => [entry_pl, entry_pl] }
446
+ entry.blog_entry_with_locales = { 'en-US' => entry_en, 'pl' => entry_pl }
447
+ entry.save
448
+ expect(entry.blog_name).to eq 'Contentful en'
449
+ end
450
+ end
451
+ end
452
+ end
453
+ end
454
+ end
455
+ end