[Git][noosfero/noosfero][master] 3 commits: Api endpoints for domains

Victor Costa gitlab at mg.gitlab.com
Wed Jan 11 08:57:47 BRST 2017


Victor Costa pushed to branch master at Noosfero / noosfero


Commits:
7ac83992 by Evandro Junior at 2017-01-10T13:01:45-03:00
Api endpoints for domains

- - - - -
0e73338b by Evandro Junior at 2017-01-10T13:27:44-03:00
Tests for domain endpoint

- - - - -
ab4606af by Victor Costa at 2017-01-11T10:57:41+00:00
Merge branch 'fix_domain_for_angular' into 'master'

Passing domain info via Api

See merge request !1080
- - - - -


6 changed files:

- app/api/app.rb
- app/api/entities.rb
- + app/api/v1/domains.rb
- app/models/domain.rb
- + test/api/domains_test.rb
- test/unit/domain_test.rb


Changes:

=====================================
app/api/app.rb
=====================================
--- a/app/api/app.rb
+++ b/app/api/app.rb
@@ -55,6 +55,7 @@ module Api
     mount V1::Profiles
     mount V1::Activities
     mount V1::Roles
+    mount V1::Domains
 
     # hook point which allow plugins to add Grape::API extensions to Api::App
     #finds for plugins which has api mount points classes defined (the class should extends Grape::API)


=====================================
app/api/entities.rb
=====================================
--- a/app/api/entities.rb
+++ b/app/api/entities.rb
@@ -304,6 +304,9 @@ module Api
       expose :signup_intro
       expose :terms_of_use
       expose :top_url, as: :host
+      expose :type do |environment, options|
+        "Environment"
+      end
       expose :settings, if: lambda { |instance, options| options[:is_admin] }
       expose :permissions, if: lambda { |environment, options| options[:current_person].present? } do |environment, options|
         environment.permissions_for(options[:current_person])
@@ -349,5 +352,15 @@ module Api
     class AbuseComplaint < Task
       expose :abuse_reports, using: AbuseReport
     end
+
+    class Domain < Entity
+      expose :id
+      expose :name
+      expose :is_default
+      expose :owner do |domain, options|
+        type_map = {Profile => ::Profile, Environment => ::Environment}.find {|k,v| domain.owner.kind_of?(v)}
+        type_map.first.represent(domain.owner, options) unless type_map.nil?
+      end
+    end
   end
 end


=====================================
app/api/v1/domains.rb
=====================================
--- /dev/null
+++ b/app/api/v1/domains.rb
@@ -0,0 +1,27 @@
+module Api
+  module V1
+    class Domains < Grape::API
+
+      resource :domains do
+
+        desc "Return all domains information"
+        get '/' do
+          present Domain.all, with: Entities::Domain, :current_person => current_person
+        end
+
+        get ':id' do
+          local_domain = nil
+          if (params[:id] == "context")
+            local_domain = Domain.by_context(request.host)
+          else
+            local_domain = Domain.find(params[:id])
+          end
+          return not_found! unless local_domain.present?
+          present_partial local_domain, with: Entities::Domain, :current_person => current_person
+        end
+
+      end
+
+    end
+  end
+end


=====================================
app/models/domain.rb
=====================================
--- a/app/models/domain.rb
+++ b/app/models/domain.rb
@@ -99,4 +99,12 @@ class Domain < ApplicationRecord
     FastGettext.translation_repositories.keys.include?(domain) ? domain : FastGettext.default_text_domain
   end
 
+  def self.default
+    Domain.find_by(is_default: true)
+  end
+
+  def self.by_context(name)
+    self.by_name(name) || self.default || Domain.new(owner: Environment.default)
+  end
+
 end


=====================================
test/api/domains_test.rb
=====================================
--- /dev/null
+++ b/test/api/domains_test.rb
@@ -0,0 +1,39 @@
+require_relative 'test_helper'
+
+class DomainsTest < ActiveSupport::TestCase
+
+  def setup
+    Domain.clear_cache
+  end
+
+  should 'return all domains' do
+    environment = Environment.default
+    profile = fast_create(Profile, name: 'save-free-software')
+    domain1 = create(Domain, name: 'test1.org', owner: environment)
+    domain2 = create(Domain, name: 'test2.org', owner: profile)
+    get "/api/v1/domains"
+    json = JSON.parse(last_response.body)
+    assert_equivalent ['test1.org', 'test2.org'], json.map { |d| d["name"]}
+    assert_equivalent [environment.name, profile.name], json.map { |d| d["owner"]["name"]}
+  end
+
+  should 'return context domain' do
+    context_env = fast_create(Environment)
+    context_env.name = "example org"
+    context_env.save
+    context_env.domains << Domain.new(:name => 'example.org')
+    default_env = Environment.default
+    get "/api/v1/domains/context"
+    json = JSON.parse(last_response.body)
+    assert_equal context_env.domains.first.id, json['id']
+  end
+
+  should 'return domain by id' do
+    environment = Environment.default
+    domain = create(Domain, name: 'mynewdomain.org', owner: environment)
+    get "/api/v1/domains/#{domain.id}"
+    json = JSON.parse(last_response.body)
+    assert_equal domain.id, json['id']
+  end
+
+end


=====================================
test/unit/domain_test.rb
=====================================
--- a/test/unit/domain_test.rb
+++ b/test/unit/domain_test.rb
@@ -127,4 +127,28 @@ class DomainTest < ActiveSupport::TestCase
     assert_equal 'DOMAIN_KEY', domain.google_maps_key
   end
 
+  should 'return default domain' do
+    d1 = fast_create(Domain, :name => 'example1.net')
+    d2 = fast_create(Domain, :name => 'example2.net', is_default: true)
+    d3 = fast_create(Domain, :name => 'example3.net')
+    assert_equal d2, Domain.default
+  end
+
+  should 'return by_context' do
+    d1 = fast_create(Domain, :name => 'example1.net')
+    d2 = fast_create(Domain, :name => 'example2.net', is_default: true)
+    d3 = fast_create(Domain, :name => 'example3.net')
+    assert_equal d3, Domain.by_context('example3.net')
+  end
+
+  should 'return default when passing a unknow domain' do
+    d1 = fast_create(Domain, :name => 'example1.net')
+    d2 = fast_create(Domain, :name => 'example2.net', is_default: true)
+    d3 = fast_create(Domain, :name => 'example3.net')
+    assert_equal d2, Domain.by_context('unknown.net')
+  end
+
+  should 'return a domain with the default environment when no domains exists' do
+    assert_equal Environment.default, Domain.by_context('unknown.net').owner
+  end
 end



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/f0d0759e4d316c0704c45c38669291b4b6587106...ab4606af74e5a900f1be738034a2cb9ced435c42
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20170111/423774f3/attachment-0001.html>


More information about the Noosfero-dev mailing list