Mailinglist Archive: yast-commit (2114 mails)

< Previous Next >
[yast-commit] <rest-service> jr_ntp : return whole object, not just aliases
  • From: Jiri Suchomel <jsuchome@xxxxxxx>
  • Date: Fri, 11 Sep 2009 16:08:11 +0200
  • Message-id: <E1MpkqQ-00054u-Fa@xxxxxxxxxxxxxxxx>
ref: refs/heads/jr_ntp
commit f14b66fbb6af38ff673cdc54f1bcb81fdd23959d
Author: Jiri Suchomel <jsuchome@xxxxxxx>
Date: Fri Sep 11 16:07:44 2009 +0200

return whole object, not just aliases
make aliases a string, not list
---
.../app/controllers/administrator_controller.rb | 29 +++++++++++++-------
plugins/administrator/app/models/administrator.rb | 28 +++++++++++++++---
.../functional/administrator_controller_test.rb | 8 +++--
.../administrator/test/unit/administrator_test.rb | 6 ++--
4 files changed, 50 insertions(+), 21 deletions(-)

diff --git a/plugins/administrator/app/controllers/administrator_controller.rb
b/plugins/administrator/app/controllers/administrator_controller.rb
index 60b90e0..21e560d 100644
--- a/plugins/administrator/app/controllers/administrator_controller.rb
+++ b/plugins/administrator/app/controllers/administrator_controller.rb
@@ -4,38 +4,47 @@ class AdministratorController < ApplicationController

# GET action
def show
+
unless
permission_check("org.opensuse.yast.modules.yapi.administrator.read")
render ErrorResult.error(403, 1, "no permission for reading") and return
end

@admin = Administrator.instance
- @aliases = @admin.read_aliases
+ @admin.read_aliases

respond_to do |format|
- format.html { render :xml => @aliases.to_xml(:root => 'aliases'),
:location => "none" } #return xml only
- format.xml { render :xml => @aliases.to_xml(:root => 'aliases'),
:location => "none" }
- format.json { render :json => @aliases.to_json, :location => "none" }
+ format.html { render :xml => @admin.to_xml(:root => "administrator"),
:location => "none" } #return xml only
+ format.xml { render :xml => @admin.to_xml(:root => "administrator"),
:location => "none" }
+ format.json { render :json => @admin.to_json, :location => "none" }
end
end

# PUT action
def update
-
unless
permission_check("org.opensuse.yast.modules.yapi.administrator.write")
render ErrorResult.error(403, 1, "no permission for writing") and return
end

+ data = params["administrator"]
@admin = Administrator.instance
+ @admin.read_aliases

- if params.has_key?(:password) && !params[:password].empty?
- @admin.save_password(params[:password])
+ if data.has_key?(:password) && !data[:password].nil? &&
!data[:password].empty?
+ @admin.save_password(data[:password])
end

- if params.has_key?(:aliases)
- @admin.save_aliases(params[:aliases])
+ if data.has_key?(:aliases)
+ begin
+ @admin.save_aliases(data[:aliases])
+ rescue Exception => e
+ render ErrorResult.error(500, 2, e.message) and return
+ end
end
-
show
end

+ def create
+ update
+ end
+
end
diff --git a/plugins/administrator/app/models/administrator.rb
b/plugins/administrator/app/models/administrator.rb
index 87186fd..abda993 100644
--- a/plugins/administrator/app/models/administrator.rb
+++ b/plugins/administrator/app/models/administrator.rb
@@ -3,12 +3,14 @@ require 'yast_service'

class Administrator

- attr_reader :aliases
+ attr_accessor :aliases
+ attr_reader :password

include Singleton

def initialize
- @aliases = []
+ @aliases = ""
+ @password = ""
end

def read_aliases
@@ -16,7 +18,7 @@ class Administrator
if yapi_ret.nil?
raise "Can't read administrator data"
elsif yapi_ret.has_key?("aliases")
- @aliases = yapi_ret["aliases"]
+ @aliases = yapi_ret["aliases"].join(",")
end
@aliases
end
@@ -30,12 +32,13 @@ class Administrator
end

def save_aliases(new_aliases)
- if @aliases.sort == new_aliases
+ new_aliases = "" if new_aliases.nil?
+ if @aliases.split(",").sort == new_aliases.split(",").sort
Rails.logger.debug "mail aliases have not been changed"
return
end
parameters = {
- "aliases" => ["as", new_aliases]
+ "aliases" => ["as", new_aliases.split(",")]
}
yapi_ret = YastService.Call("YaPI::ADMINISTRATOR::Write", parameters)
Rails.logger.debug "YaPI returns: '#{yapi_ret}'"
@@ -44,4 +47,19 @@ class Administrator
@aliases = new_aliases
end

+ def to_xml( options = {} )
+ xml = options[:builder] ||= Builder::XmlMarkup.new(options)
+ xml.instruct! unless options[:skip_instruct]
+
+ xml.administrator do
+ xml.password password
+ xml.aliases aliases
+ end
+ end
+
+ def to_json( options = {} )
+ hash = Hash.from_xml(to_xml())
+ return hash.to_json
+ end
+
end
diff --git
a/plugins/administrator/test/functional/administrator_controller_test.rb
b/plugins/administrator/test/functional/administrator_controller_test.rb
index d3a4879..85faa7b 100644
--- a/plugins/administrator/test/functional/administrator_controller_test.rb
+++ b/plugins/administrator/test/functional/administrator_controller_test.rb
@@ -12,10 +12,11 @@ class AdministratorControllerTest <
ActionController::TestCase
@request.session[:account_id] = 1 # defined in fixtures

@model = Administrator.instance
- @model.stubs(:read_aliases).returns([])
+ @model.stubs(:read_aliases).returns("")
end

test "check 'show' result" do
+
ret = get :show
# success (200 OK)
assert_response :success
@@ -23,8 +24,9 @@ class AdministratorControllerTest < ActionController::TestCase
# is returned a valid XML?
ret_hash = Hash.from_xml(ret.body)
assert ret_hash
- assert ret_hash.has_key?("aliases")
- assert ret_hash["aliases"].is_a? Array
+ assert ret_hash.has_key?("administrator")
+ assert ret_hash["administrator"].has_key?("aliases")
+ Rails.logger.debug ret_hash.inspect
end

end
diff --git a/plugins/administrator/test/unit/administrator_test.rb
b/plugins/administrator/test/unit/administrator_test.rb
index 1e8ef08..008977d 100644
--- a/plugins/administrator/test/unit/administrator_test.rb
+++ b/plugins/administrator/test/unit/administrator_test.rb
@@ -18,15 +18,15 @@ class AdministratorTest < ActiveSupport::TestCase
YastService.stubs(:Call).with('YaPI::ADMINISTRATOR::Read').returns({
"aliases" => [ "a@b" ] })
ret = @model.read_aliases
assert ret
- assert @model.aliases.size == 1
+ assert @model.aliases.split(",").size == 1
end

def test_save_aliases
new_aliases = [ "test@xxxxxxxxxx", "a@b" ];
YastService.stubs(:Call).with('YaPI::ADMINISTRATOR::Write', {"aliases" =>
[ "as", new_aliases ]}).returns("")
- ret = @model.save_aliases(new_aliases)
+ ret = @model.save_aliases(new_aliases.join(","))
assert ret
- assert @model.aliases.size == 2
+ assert @model.aliases.split(",").size == 2
end

end
--
To unsubscribe, e-mail: yast-commit+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: yast-commit+help@xxxxxxxxxxxx

< Previous Next >
This Thread
  • No further messages