-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adapt API's StatusMessage to the new structure
Handle scope (communication_scope) in status message's XML. The validation schema considers scope as optional. When the value is not passed, the default value "all_users" is stored instead. Add show view to status message and move StatusMessageController related views to partials. Adapt tests and docs's XML files. Co-authored-by: Victor Pereira <vpereira@suse.com>
- Loading branch information
Showing
11 changed files
with
132 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<message severity="0"> | ||
a single sample message. contains some text ... | ||
attributes are optional and ignored when sent (put) to the api. | ||
the format of this example will be used as a convenient way to put a message. | ||
</message> | ||
<status_message> | ||
<message> | ||
a single sample message. contains some text ... | ||
attributes are optional and ignored when sent (put) to the api. | ||
the format of this example will be used as a convenient way to put a message. | ||
</message> | ||
<severity>information</severity> | ||
<scope>all_users</scope> | ||
</status_message> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> | ||
<xs:element name="status_message"> | ||
<xs:complexType> | ||
<xs:sequence> | ||
<xs:element name="message" type="xs:string"></xs:element> | ||
<xs:element name="severity" type="xs:string"></xs:element> | ||
<xs:element name="scope" type="xs:string" minOccurs="0" default="all_users"></xs:element> | ||
</xs:sequence> | ||
<xs:attribute name="id" type="xs:int"></xs:attribute> | ||
</xs:complexType> | ||
</xs:element> | ||
</xs:schema> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,37 @@ | ||
class StatusMessagesController < ApplicationController | ||
class PermissionDeniedError < APIError | ||
setup 403 | ||
end | ||
|
||
class CreatingMessagesError < APIError; end | ||
before_action :require_admin, only: [:create, :destroy] | ||
|
||
def index | ||
@messages = StatusMessage.alive.limit(params[:limit]).order('created_at DESC').includes(:user) | ||
@count = @messages.size | ||
end | ||
|
||
def show | ||
@messages = [StatusMessage.find(params[:id])] | ||
@count = 1 | ||
render :index | ||
@message = StatusMessage.find(params[:id]) | ||
end | ||
|
||
def create | ||
# check permissions | ||
unless permissions.status_message_create | ||
raise PermissionDeniedError, 'message(s) cannot be created, you have not sufficient permissions' | ||
end | ||
|
||
new_messages = Nokogiri::XML(request.raw_post, &:strict).root | ||
@messages = [] | ||
if new_messages.css('message').present? | ||
# message(s) are wrapped in outer xml tag 'status_messages' | ||
new_messages.css('message').each do |msg| | ||
@messages << StatusMessage.create!(message: msg.content, severity: msg['severity'], user: User.session!) | ||
end | ||
else | ||
# TODO: make use of a validator | ||
raise CreatingMessagesError, "no message #{new_messages.to_xml}" if new_messages.name != 'message' | ||
# just one message, NOT wrapped in outer xml tag 'status_messages' | ||
@messages << StatusMessage.create!(message: new_messages.content, severity: new_messages['severity'], user: User.session!) | ||
end | ||
render :index | ||
status_message = StatusMessage.from_xml(validate_status_message) | ||
|
||
authorize status_message | ||
|
||
status_message.save! | ||
|
||
render_ok | ||
end | ||
|
||
def destroy | ||
# check permissions | ||
unless permissions.status_message_create | ||
raise PermissionDeniedError, 'message cannot be deleted, you have not sufficient permissions' | ||
end | ||
|
||
StatusMessage.find(params[:id]).delete | ||
status_message = StatusMessage.find(params[:id]) | ||
authorize status_message | ||
status_message.delete | ||
render_ok | ||
end | ||
|
||
private | ||
|
||
# TODO: make it more robust | ||
def validate_status_message | ||
Suse::Validator.validate(:status_message, request.raw_post) | ||
request.raw_post | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class StatusMessagePolicy < ApplicationPolicy | ||
def create? | ||
user.is_admin? | ||
end | ||
|
||
def destroy? | ||
user.is_admin? | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
src/api/app/views/status_messages/_status_message.xml.builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
builder.status_message(id: status_message.id) do |m| | ||
m.message status_message.message | ||
m.user status_message.user.login | ||
m.severity status_message.severity | ||
m.scope status_message.communication_scope | ||
m.created_at status_message.created_at | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,3 @@ | ||
xml.status_messages(count: @count) do | ||
@messages.each do |msg| | ||
xml.message( | ||
msg.message, | ||
msg_id: msg.id, | ||
user: msg.user.login, | ||
severity: msg.severity, | ||
created_at: msg.created_at, | ||
deleted_at: msg.deleted_at | ||
) | ||
end | ||
render(partial: 'status_message', collection: @messages, locals: { builder: xml }) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
render(partial: 'status_message', locals: { builder: xml, status_message: @message }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe StatusMessage do | ||
let(:admin_user) { create(:admin_user, login: 'admin') } | ||
|
||
describe 'validations' do | ||
it { is_expected.to validate_presence_of(:severity) } | ||
it { is_expected.to validate_presence_of(:message) } | ||
end | ||
|
||
describe '.from_xml' do | ||
before do | ||
allow(User).to receive(:session!).and_return(admin_user) | ||
end | ||
|
||
context 'xml is valid' do | ||
let(:xml) { '<status_message id="4"><message>foo</message><severity>information</severity></status_message>' } | ||
let(:status_message) { StatusMessage.from_xml(xml) } | ||
|
||
it { expect { status_message }.not_to raise_error } | ||
it { expect(status_message).to be_a(StatusMessage) } | ||
end | ||
|
||
context 'xml is invalid' do | ||
it { expect { StatusMessage.from_xml('') }.to raise_error(ActiveRecord::RecordInvalid) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters