Commit c4c6ba443f40304118a92da92cbe79d79e713865
Committed by
Rodrigo Souto
1 parent
59fa04e8
Exists in
master
and in
29 other branches
api: adding method for user and community creation
Showing
7 changed files
with
62 additions
and
3 deletions
Show diff stats
lib/api/v1/articles.rb
| ... | ... | @@ -59,7 +59,7 @@ module API |
| 59 | 59 | end |
| 60 | 60 | |
| 61 | 61 | # Example Request: |
| 62 | - # POST api/v1/communites/:community_id/articles?private_toke=234298743290432&article[name]=title&article[body]=body | |
| 62 | + # POST api/v1/communites/:community_id/articles?private_token=234298743290432&article[name]=title&article[body]=body | |
| 63 | 63 | post do |
| 64 | 64 | community = environment.communities.find(params[:community_id]) |
| 65 | 65 | return forbidden! unless current_person.can_post_content?(community) | ... | ... |
lib/api/v1/communities.rb
| ... | ... | @@ -21,6 +21,24 @@ module API |
| 21 | 21 | present communities, :with => Entities::Community |
| 22 | 22 | end |
| 23 | 23 | |
| 24 | + | |
| 25 | + # Example Request: | |
| 26 | + # POST api/v1/communties?private_token=234298743290432&community[name]=some_name | |
| 27 | + post do | |
| 28 | + params[:community] ||= {} | |
| 29 | + begin | |
| 30 | + community = Community.create_after_moderation(current_person, params[:community].merge({:environment => environment})) | |
| 31 | + rescue | |
| 32 | + community = Community.new(params[:community]) | |
| 33 | + end | |
| 34 | + | |
| 35 | + if !community.save | |
| 36 | + render_api_errors!(community.errors.full_messages) | |
| 37 | + end | |
| 38 | + | |
| 39 | + present community, :with => Entities::Community | |
| 40 | + end | |
| 41 | + | |
| 24 | 42 | get ':id' do |
| 25 | 43 | community = environment.communities.visible.find_by_id(params[:id]) |
| 26 | 44 | present community, :with => Entities::Community | ... | ... |
lib/api/v1/users.rb
| ... | ... | @@ -11,6 +11,19 @@ module API |
| 11 | 11 | present environment.users, :with => Entities::User |
| 12 | 12 | end |
| 13 | 13 | |
| 14 | + # Example Request: | |
| 15 | + # POST api/v1/users?user[login]=some_login&user[password]=some | |
| 16 | + post do | |
| 17 | + user = User.new(params[:user]) | |
| 18 | + user.terms_of_use = environment.terms_of_use | |
| 19 | + user.environment = environment | |
| 20 | + if !user.save | |
| 21 | + render_api_errors!(user.errors.full_messages) | |
| 22 | + end | |
| 23 | + | |
| 24 | + present user, :with => Entities::User | |
| 25 | + end | |
| 26 | + | |
| 14 | 27 | get ":id" do |
| 15 | 28 | present environment.users.find_by_id(params[:id]), :with => Entities::User |
| 16 | 29 | end | ... | ... |
test/unit/api/communities_test.rb
| ... | ... | @@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/test_helper' |
| 3 | 3 | class CommunitiesTest < ActiveSupport::TestCase |
| 4 | 4 | |
| 5 | 5 | def setup |
| 6 | + Community.delete_all | |
| 6 | 7 | login_api |
| 7 | 8 | end |
| 8 | 9 | |
| ... | ... | @@ -42,6 +43,19 @@ class CommunitiesTest < ActiveSupport::TestCase |
| 42 | 43 | assert_equivalent [c1.id, c2.id], json['communities'].map {|c| c['id']} |
| 43 | 44 | end |
| 44 | 45 | |
| 46 | + should 'create a community' do | |
| 47 | + params[:community] = {:name => 'some'} | |
| 48 | + post "/api/v1/communities?#{params.to_query}" | |
| 49 | + json = JSON.parse(last_response.body) | |
| 50 | + assert_equal 'some', json['community']['name'] | |
| 51 | + end | |
| 52 | + | |
| 53 | + should 'return 400 status for invalid community creation' do | |
| 54 | + post "/api/v1/communities?#{params.to_query}" | |
| 55 | + json = JSON.parse(last_response.body) | |
| 56 | + assert_equal 400, last_response.status | |
| 57 | + end | |
| 58 | + | |
| 45 | 59 | should 'get community' do |
| 46 | 60 | community = fast_create(Community) |
| 47 | 61 | ... | ... |
test/unit/api/enterprises_test.rb
test/unit/api/people_test.rb
| ... | ... | @@ -3,10 +3,10 @@ require File.dirname(__FILE__) + '/test_helper' |
| 3 | 3 | class PeopleTest < ActiveSupport::TestCase |
| 4 | 4 | |
| 5 | 5 | def setup |
| 6 | + Person.delete_all | |
| 6 | 7 | login_api |
| 7 | 8 | end |
| 8 | 9 | |
| 9 | - | |
| 10 | 10 | should 'list all people' do |
| 11 | 11 | person1 = fast_create(Person, :public_profile => true) |
| 12 | 12 | person2 = fast_create(Person) | ... | ... |
test/unit/api/users_test.rb
| ... | ... | @@ -12,13 +12,26 @@ class UsersTest < ActiveSupport::TestCase |
| 12 | 12 | assert_includes json["users"].map { |a| a["login"] }, user.login |
| 13 | 13 | end |
| 14 | 14 | |
| 15 | + should 'create a user' do | |
| 16 | + params[:user] = {:login => 'some', :password => '123456', :password_confirmation => '123456', :email => 'some@some.com'} | |
| 17 | + post "/api/v1/users?#{params.to_query}" | |
| 18 | + json = JSON.parse(last_response.body) | |
| 19 | + assert_equal 'some', json['user']['login'] | |
| 20 | + end | |
| 21 | + | |
| 22 | + should 'return 400 status for invalid user creation' do | |
| 23 | + params[:user] = {:login => 'some'} | |
| 24 | + post "/api/v1/users?#{params.to_query}" | |
| 25 | + json = JSON.parse(last_response.body) | |
| 26 | + assert_equal 400, last_response.status | |
| 27 | + end | |
| 28 | + | |
| 15 | 29 | should 'get user' do |
| 16 | 30 | get "/api/v1/users/#{user.id}?#{params.to_query}" |
| 17 | 31 | json = JSON.parse(last_response.body) |
| 18 | 32 | assert_equal user.id, json['user']['id'] |
| 19 | 33 | end |
| 20 | 34 | |
| 21 | - | |
| 22 | 35 | should 'list user permissions' do |
| 23 | 36 | community = fast_create(Community) |
| 24 | 37 | community.add_admin(person) | ... | ... |