Skip to content

Commit

Permalink
[api] groups can have an email adress now
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianschroeter committed Sep 4, 2013
1 parent bfe7ee0 commit 5aaefc2
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/api/api/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,11 @@ POST /group/<group title>
Multiple commands on processing group.
add_user: add a user to a group
remove_user: remove a user from a group
set_email: set email adress of group.

Parameters:
userid: user login name, required for add_user and remove_user command
email: email address for set_email command. email of group gets removed if not defined

XmlResult: status

Expand Down
3 changes: 3 additions & 0 deletions docs/api/api/group.rng
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<define ns="" name="group-element">
<element name="group">
<element name="title"><text/></element>
<optional>
<element name="email"><text/></element>
</optional>
<element name="person"> <!-- FIXME3.0: should be plural -->
<zeroOrMore>
<element name="person">
Expand Down
1 change: 1 addition & 0 deletions docs/api/api/group.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<group>
<title>review-team</title>
<email>review@openbuildservice.org</email>
<person>
<person userid="Iggy"/>
<person userid="king"/>
Expand Down
2 changes: 2 additions & 0 deletions src/api/app/controllers/group_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def command
group.add_user user
elsif params[:cmd] == "remove_user"
group.remove_user user
elsif params[:cmd] == "set_email"
group.set_email params[:email]
else
render_error :status => 400, :errorcode => "unknown_command", :message => "cmd must be set to add_user or remove_user"
return
Expand Down
17 changes: 15 additions & 2 deletions src/api/app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,22 @@ def self.get_by_title(title)
end

def update_from_xml( xmlhash )
self.title = xmlhash.value('title')
self.with_lock do
self.title = xmlhash.value('title')

if xmlhash.value('email')
self.email = xmlhash.value('email')
else
self.email = nil
end
end
self.save!

# update user list
cache = Hash.new
self.groups_users.each do |gu|
cache[gu.user.id] = gu
end
self.save!

persons = xmlhash.elements('person').first
if persons
Expand Down Expand Up @@ -76,6 +84,11 @@ def remove_user(user)
GroupsUser.delete_all(["user_id = ? AND group_id = ?", user.id, self.id])
end

def set_email(email)
self.email = email
self.save!
end

def to_s
self.title
end
Expand Down
1 change: 1 addition & 0 deletions src/api/app/views/group/show.xml.builder
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
xml.group do
xml.title(@group.title)
xml.email(@group.email) if @group.email
xml.person do
@group.groups_users.each do |gu|
xml.person(userid: gu.user.login)
Expand Down
5 changes: 5 additions & 0 deletions src/api/db/migrate/20130904071147_add_email_to_groups.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddEmailToGroups < ActiveRecord::Migration
def change
add_column :groups, :email, :string
end
end
3 changes: 3 additions & 0 deletions src/api/db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ CREATE TABLE `groups` (
`updated_at` datetime DEFAULT NULL,
`title` varchar(200) CHARACTER SET utf8 NOT NULL DEFAULT '',
`parent_id` int(11) DEFAULT NULL,
`email` varchar(255) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `groups_parent_id_index` (`parent_id`),
KEY `index_groups_on_title` (`title`)
Expand Down Expand Up @@ -1249,6 +1250,8 @@ INSERT INTO schema_migrations (version) VALUES ('20130830043205');

INSERT INTO schema_migrations (version) VALUES ('20130903114302');

INSERT INTO schema_migrations (version) VALUES ('20130904071147');

INSERT INTO schema_migrations (version) VALUES ('21');

INSERT INTO schema_migrations (version) VALUES ('22');
Expand Down
16 changes: 14 additions & 2 deletions src/api/test/functional/group_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,20 @@ def test_create_modify_and_delete_group
assert_response :success

# add a user
xml2 = "<group><title>new_group</title> <person><person userid='fred' /></person> </group>"
xml2 = "<group><title>new_group</title> <email>obs@obs.com</email> <person><person userid='fred' /></person> </group>"
put "/group/new_group", xml2
assert_response :success
# double save is done by webui, we need to support it
get "/group/new_group"
assert_response :success
assert_xml_tag :tag => 'email', :content => "obs@obs.com"
# double save is done by webui, we need to support it. Drop email adress also
xml2 = "<group><title>new_group</title> <person><person userid='fred' /><person userid='fred' /></person> </group>"
put "/group/new_group", xml2
assert_response :success
get "/group/new_group"
assert_response :success
assert_xml_tag :tag => 'person', :attributes => {:userid => 'fred'}
assert_no_xml_tag :tag => 'email'

# remove user
put "/group/new_group", xml
Expand All @@ -90,6 +94,8 @@ def test_add_and_remove_users_from_group
assert_response 403
post "/group/test_group", :cmd => "remove_user", :userid => "Iggy"
assert_response 403
post "/group/test_group", :cmd => "set_email", :email => "obs@obs.de"
assert_response 403
get "/group/test_group"
assert_response :success
assert_no_xml_tag :tag => 'person', :attributes => {:userid => 'Iggy'}
Expand All @@ -101,14 +107,20 @@ def test_add_and_remove_users_from_group
# double add is a dummy operation, but needs to work for webui
post "/group/test_group", :cmd => "add_user", :userid => "Iggy"
assert_response :success
post "/group/test_group", :cmd => "set_email", :email => "email@me"
assert_response :success
get "/group/test_group"
assert_response :success
assert_xml_tag :tag => 'person', :attributes => {:userid => 'Iggy'}
assert_xml_tag :tag => 'email', :content => "email@me"
post "/group/test_group", :cmd => "remove_user", :userid => "Iggy"
assert_response :success
post "/group/test_group", :cmd => "set_email"
assert_response :success
get "/group/test_group"
assert_response :success
assert_no_xml_tag :tag => 'person', :attributes => {:userid => 'Iggy'}
assert_no_xml_tag :tag => 'email'

# done, back at old state
end
Expand Down

0 comments on commit 5aaefc2

Please sign in to comment.