CDB-2891 Add specs for Superadmin::OrganizationsController and fix a couple of bugs

pull/567/head
Jose A. Rilla 10 years ago
parent c4eb87d181
commit 9995dc9c7c

@ -47,6 +47,7 @@ WORKING_SPECS_4 = \
spec/requests/api/permissions_controller_spec.rb \
spec/models/shared_entity_spec.rb \
spec/requests/superadmin/users_spec.rb \
spec/requests/superadmin/organizations_spec.rb \
$(NULL)

@ -18,9 +18,8 @@ class Superadmin::OrganizationsController < Superadmin::SuperadminController
def create
@organization = Organization.new
@organization.set_fields_from_central(params[:organization], :create)
@organization.save
# TODO: move this into a callback or a model method
if params[:organization][:owner_id].present? && @organization.owner.nil?
if @organization.save && params[:organization][:owner_id].present? && @organization.owner.nil?
# TODO: move this into a callback or a model method
uo = CartoDB::UserOrganization.new(@organization.id, params[:organization][:owner_id])
uo.promote_user_to_admin
end

@ -60,7 +60,7 @@ module Concerns
if self.is_a?(Organization)
case action
when :create
raise "Can't create organizations in editor"
raise "Can't create organizations from editor"
when :update
self.values.slice(:seats, :display_name, :description, :website,
:discus_shortname, :twitter_username)
@ -85,6 +85,7 @@ module Concerns
end
def set_fields_from_central(params, action)
return self unless params.present? && action.present?
self.set(params.slice(*allowed_attributes_from_central(action)))
if self.is_a?(User) && params.has_key?(:password)
self.password = self.password_confirmation = params[:password]

@ -25,6 +25,8 @@ class Organization < Sequel::Model
# @param display_name String
# @param discus_shortname String
# @param twitter_username String
# @param geocoding_quota Integer
# @param map_view_quota Integer
one_to_many :users
many_to_one :owner, class_name: 'User', key: 'owner_id'

@ -1,8 +1,30 @@
FactoryGirl.define do
factory :organization do
name 'vizzuality'
seats 10
quota_in_bytes 100.megabytes
geocoding_quota 1000
map_view_quota 100000
website 'cartodb.com'
description 'Lorem ipsum dolor sit amet'
display_name 'Vizzuality Inc'
discus_shortname 'cartodb'
twitter_username 'cartodb'
factory :organization_with_users do
after(:create) do |org|
owner = FactoryGirl.create(:user)
uo = CartoDB::UserOrganization.new(org.id, owner.id)
uo.promote_user_to_admin
org.reload
user = FactoryGirl.build(:user)
user.organization_id = org.id
user.enabled = true
user.save
org.reload
end
end
end
end

@ -0,0 +1,79 @@
# encoding: utf-8
require_relative '../../acceptance_helper'
require 'ruby-debug'
feature "Superadmin's organization API" do
before(:all) do
# Capybara.current_driver = :rack_test
@organization = FactoryGirl.create(:organization_with_users, name: 'vizzuality')
@org_atts = @organization.values
end
scenario "Http auth is needed" do
post_json superadmin_users_path, { :format => "json" } do |response|
response.status.should == 401
end
end
scenario "organization create fail" do
pending "Exception handling isn' implemented yet"
end
scenario "organization create success" do
@org_atts = FactoryGirl.build(:organization, name: 'wadus').values
post_json superadmin_organizations_path, { :organization => @org_atts }, default_headers do |response|
response.status.should == 201
response.body[:name].should == 'wadus'
# Double check that the organization has been created properly
organization = Organization.filter(:name => @org_atts[:name]).first
organization.should be_present
organization.id.should == response.body[:id]
organization.destroy
end
end
scenario "organization update fail" do
pending "Exception handling isn' implemented yet"
end
scenario "organization update success" do
put_json superadmin_organization_path(@organization), { :organization => { :display_name => "Update Test", :map_view_quota => 800000 } }, default_headers do |response|
response.status.should == 204
end
organization = Organization[@organization.id]
organization.display_name.should == "Update Test"
organization.map_view_quota.should == 800000
end
scenario "organization delete success" do
pending "Delete is not implemented yet"
end
scenario "organization get info success" do
get_json superadmin_organization_path(@organization), {}, default_headers do |response|
response.status.should == 200
response.body[:id].should == @organization.id
end
end
describe "GET /superadmin/organization" do
it "gets all organizations" do
@organization2 = FactoryGirl.create(:organization, name: 'wadus')
get_json superadmin_organizations_path, {}, default_headers do |response|
response.status.should == 200
response.body.map { |u| u["name"] }.should include(@organization.name, @organization2.name)
response.body.length.should >= 2
end
end
end
private
def default_headers(user = Cartodb.config[:superadmin]["username"], password = Cartodb.config[:superadmin]["password"])
{
'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user, password),
'HTTP_ACCEPT' => "application/json"
}
end
end
Loading…
Cancel
Save