60 |
60 |
assert_kind_of Hash, json['news'].first
|
61 |
61 |
assert_equal 2, json['news'].first['id']
|
62 |
62 |
end
|
|
63 |
|
|
64 |
test "POST /project/:project_id/news.xml should create a news with the attributes" do
|
|
65 |
payload = <<-XML
|
|
66 |
<?xml version="1.0" encoding="UTF-8" ?>
|
|
67 |
<news>
|
|
68 |
<title>NewsXmlApiTest</title>
|
|
69 |
<summary>News XML-API Test</summary>
|
|
70 |
<description>This is the description</description>
|
|
71 |
</news>
|
|
72 |
XML
|
|
73 |
|
|
74 |
assert_difference('News.count') do
|
|
75 |
post '/projects/1/news.xml',
|
|
76 |
:params => payload,
|
|
77 |
:headers => {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
|
|
78 |
end
|
|
79 |
news = News.find_by(:title => 'NewsXmlApiTest')
|
|
80 |
assert_not_nil news
|
|
81 |
assert_equal 'News XML-API Test', news.summary
|
|
82 |
assert_equal 'This is the description', news.description
|
|
83 |
assert_equal User.find_by_login('jsmith'), news.author
|
|
84 |
assert_equal Project.find(1), news.project
|
|
85 |
assert_response :no_content
|
|
86 |
end
|
|
87 |
|
|
88 |
test "POST /project/:project_id/news.xml with failure should return errors" do
|
|
89 |
assert_no_difference('News.count') do
|
|
90 |
post '/projects/1/news.xml',
|
|
91 |
:params => {:news => {:title => ''}},
|
|
92 |
:headers => credentials('jsmith')
|
|
93 |
end
|
|
94 |
assert_select 'errors error', :text => "Title cannot be blank"
|
|
95 |
end
|
|
96 |
|
|
97 |
test "POST /project/:project_id/news.json should create a news with the attributes" do
|
|
98 |
payload = <<-JSON
|
|
99 |
{
|
|
100 |
"news": {
|
|
101 |
"title": "NewsJsonApiTest",
|
|
102 |
"summary": "News JSON-API Test",
|
|
103 |
"description": "This is the description"
|
|
104 |
}
|
|
105 |
}
|
|
106 |
JSON
|
|
107 |
|
|
108 |
assert_difference('News.count') do
|
|
109 |
post '/projects/1/news.json',
|
|
110 |
:params => payload,
|
|
111 |
:headers => {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
|
|
112 |
end
|
|
113 |
news = News.find_by(:title => 'NewsJsonApiTest')
|
|
114 |
assert_not_nil news
|
|
115 |
assert_equal 'News JSON-API Test', news.summary
|
|
116 |
assert_equal 'This is the description', news.description
|
|
117 |
assert_equal User.find_by_login('jsmith'), news.author
|
|
118 |
assert_equal Project.find(1), news.project
|
|
119 |
assert_response :no_content
|
|
120 |
end
|
|
121 |
|
|
122 |
test "POST /project/:project_id/news.json with failure should return errors" do
|
|
123 |
assert_no_difference('News.count') do
|
|
124 |
post '/projects/1/news.json',
|
|
125 |
:params => {:news => {:title => ''}},
|
|
126 |
:headers => credentials('jsmith')
|
|
127 |
end
|
|
128 |
json = ActiveSupport::JSON.decode(response.body)
|
|
129 |
assert json['errors'].include?("Title cannot be blank")
|
|
130 |
end
|
|
131 |
|
|
132 |
test "POST /project/:project_id/news.xml with attachment should create a news with attachment" do
|
|
133 |
token = xml_upload('test_create_with_attachment', credentials('jsmith'))
|
|
134 |
attachment = Attachment.find_by_token(token)
|
|
135 |
|
|
136 |
assert_difference 'News.count' do
|
|
137 |
post '/projects/1/news.xml',
|
|
138 |
:params => {:news => {:title => 'News XML-API with Attachment',
|
|
139 |
:description => 'desc'},
|
|
140 |
:attachments => [{:token => token, :filename => 'test.txt',
|
|
141 |
:content_type => 'text/plain'}]},
|
|
142 |
:headers => credentials('jsmith')
|
|
143 |
assert_response :no_content
|
|
144 |
end
|
|
145 |
news = News.find_by(:title => 'News XML-API with Attachment')
|
|
146 |
assert_equal attachment, news.attachments.first
|
|
147 |
|
|
148 |
attachment.reload
|
|
149 |
assert_equal 'test.txt', attachment.filename
|
|
150 |
assert_equal 'text/plain', attachment.content_type
|
|
151 |
assert_equal 'test_create_with_attachment'.size, attachment.filesize
|
|
152 |
assert_equal 2, attachment.author_id
|
|
153 |
end
|
|
154 |
|
|
155 |
test "POST /project/:project_id/news.xml with multiple attachment should create a news with attachments" do
|
|
156 |
token1 = xml_upload('File content 1', credentials('jsmith'))
|
|
157 |
token2 = xml_upload('File content 2', credentials('jsmith'))
|
|
158 |
payload = <<-XML
|
|
159 |
<?xml version="1.0" encoding="UTF-8" ?>
|
|
160 |
<news>
|
|
161 |
<title>News XML-API with attachments</title>
|
|
162 |
<description>News with multiple attachments</description>
|
|
163 |
<uploads type="array">
|
|
164 |
<upload>
|
|
165 |
<token>#{token1}</token>
|
|
166 |
<filename>test1.txt</filename>
|
|
167 |
</upload>
|
|
168 |
<upload>
|
|
169 |
<token>#{token2}</token>
|
|
170 |
<filename>test2.txt</filename>
|
|
171 |
</upload>
|
|
172 |
</uploads>
|
|
173 |
</news>
|
|
174 |
XML
|
|
175 |
|
|
176 |
assert_difference('News.count') do
|
|
177 |
post '/projects/1/news.xml',
|
|
178 |
:params => payload,
|
|
179 |
:headers => {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
|
|
180 |
assert_response :no_content
|
|
181 |
end
|
|
182 |
news = News.find_by(:title => 'News XML-API with attachments')
|
|
183 |
assert_equal 2, news.attachments.count
|
|
184 |
end
|
|
185 |
|
|
186 |
test "POST /project/:project_id/news.json with multiple attachment should create a news with attachments" do
|
|
187 |
token1 = json_upload('File content 1', credentials('jsmith'))
|
|
188 |
token2 = json_upload('File content 2', credentials('jsmith'))
|
|
189 |
payload = <<-JSON
|
|
190 |
{
|
|
191 |
"news": {
|
|
192 |
"title": "News JSON-API with attachments",
|
|
193 |
"description": "News with multiple attachments",
|
|
194 |
"uploads": [
|
|
195 |
{"token": "#{token1}", "filename": "test1.txt"},
|
|
196 |
{"token": "#{token2}", "filename": "test2.txt"}
|
|
197 |
]
|
|
198 |
}
|
|
199 |
}
|
|
200 |
JSON
|
|
201 |
|
|
202 |
assert_difference('News.count') do
|
|
203 |
post '/projects/1/news.json',
|
|
204 |
:params => payload,
|
|
205 |
:headers => {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
|
|
206 |
assert_response :no_content
|
|
207 |
end
|
|
208 |
news = News.find_by(:title => 'News JSON-API with attachments')
|
|
209 |
assert_equal 2, news.attachments.count
|
|
210 |
end
|
63 |
211 |
end
|