56 lines
1.5 KiB
Ruby
56 lines
1.5 KiB
Ruby
|
# frozen_string_literal: true
|
||
|
|
||
|
require 'test_helper'
|
||
|
|
||
|
class PostsControllerTest < ActionDispatch::IntegrationTest
|
||
|
setup do
|
||
|
@rol = create :rol
|
||
|
@site = @rol.site
|
||
|
@usuarie = @rol.usuarie
|
||
|
@post = @site.posts.build(title: SecureRandom.hex)
|
||
|
@post.save
|
||
|
|
||
|
@authorization = {
|
||
|
Authorization: ActionController::HttpAuthentication::Basic
|
||
|
.encode_credentials(@usuarie.email, @usuarie.password)
|
||
|
}
|
||
|
end
|
||
|
|
||
|
teardown do
|
||
|
@site.destroy
|
||
|
end
|
||
|
|
||
|
test 'se pueden ver' do
|
||
|
get site_posts_url(@site), headers: @authorization
|
||
|
|
||
|
assert_match @site.name, response.body
|
||
|
assert_match @post.title.value, response.body
|
||
|
end
|
||
|
|
||
|
test 'se pueden crear nuevos' do
|
||
|
title = SecureRandom.hex
|
||
|
post site_posts_url(@site), headers: @authorization,
|
||
|
params: {
|
||
|
post: {
|
||
|
title: title,
|
||
|
date: 2.days.ago.strftime('%F')
|
||
|
}
|
||
|
}
|
||
|
|
||
|
# TODO: implementar reload?
|
||
|
site = Site.find(@site.id)
|
||
|
site.read
|
||
|
new_post = site.posts.first
|
||
|
|
||
|
assert_equal 302, response.status
|
||
|
|
||
|
# XXX: No usamos follow_redirect! porque pierde la autenticación
|
||
|
get site_posts_url(@site), headers: @authorization
|
||
|
|
||
|
assert_match new_post.title.value, response.body
|
||
|
assert_equal title, new_post.title.value
|
||
|
assert_equal I18n.t('post_service.created', title: new_post.title.value),
|
||
|
@site.repository.rugged.head.target.message
|
||
|
end
|
||
|
end
|