From 2094b505537e759e9d030d85ed52d8243a98e1f3 Mon Sep 17 00:00:00 2001
From: Konstantin Haase <konstantin.mailinglists@googlemail.com>
Date: Wed, 29 Apr 2015 14:28:33 +0200
Subject: [PATCH] v3: add @permissions to organization payload

---
 lib/travis/api/v3/access_control/user.rb      |  4 ++
 lib/travis/api/v3/permissions/organization.rb |  9 ++++
 spec/v3/services/organization/find_spec.rb    | 15 +++---
 .../organizations/for_current_user_spec.rb    | 21 +++++----
 spec/v3/services/owner/find_spec.rb           | 47 ++++++++++---------
 5 files changed, 58 insertions(+), 38 deletions(-)
 create mode 100644 lib/travis/api/v3/permissions/organization.rb

diff --git a/lib/travis/api/v3/access_control/user.rb b/lib/travis/api/v3/access_control/user.rb
index c6a2dd25..3376ffb9 100644
--- a/lib/travis/api/v3/access_control/user.rb
+++ b/lib/travis/api/v3/access_control/user.rb
@@ -25,6 +25,10 @@ module Travis::API::V3
 
     protected
 
+    def organization_writable?(organization)
+      organization.members.include? user
+    end
+
     def repository_writable?(repository)
       permission?(:push, repository)
     end
diff --git a/lib/travis/api/v3/permissions/organization.rb b/lib/travis/api/v3/permissions/organization.rb
new file mode 100644
index 00000000..1693d975
--- /dev/null
+++ b/lib/travis/api/v3/permissions/organization.rb
@@ -0,0 +1,9 @@
+require 'travis/api/v3/permissions/generic'
+
+module Travis::API::V3
+  class Permissions::Organization < Permissions::Generic
+    def sync?
+      write?
+    end
+  end
+end
diff --git a/spec/v3/services/organization/find_spec.rb b/spec/v3/services/organization/find_spec.rb
index 030aa172..2245441a 100644
--- a/spec/v3/services/organization/find_spec.rb
+++ b/spec/v3/services/organization/find_spec.rb
@@ -9,13 +9,14 @@ describe Travis::API::V3::Services::Organization::Find do
     before  { get("/v3/org/#{org.id}")       }
     example { expect(last_response).to be_ok }
     example { expect(JSON.load(body)).to be == {
-      "@type"      => "organization",
-      "@href"      => "/v3/org/#{org.id}",
-      "id"         => org.id,
-      "login"      => "example-org",
-      "name"       => nil,
-      "github_id"  => nil,
-      "avatar_url" => nil
+      "@type"        => "organization",
+      "@href"        => "/v3/org/#{org.id}",
+      "@permissions" => { "read"=>true, "sync"=>false },
+      "id"           => org.id,
+      "login"        => "example-org",
+      "name"         => nil,
+      "github_id"    => nil,
+      "avatar_url"   => nil
     }}
   end
 end
diff --git a/spec/v3/services/organizations/for_current_user_spec.rb b/spec/v3/services/organizations/for_current_user_spec.rb
index f402e7cd..dfdf1517 100644
--- a/spec/v3/services/organizations/for_current_user_spec.rb
+++ b/spec/v3/services/organizations/for_current_user_spec.rb
@@ -18,16 +18,17 @@ describe Travis::API::V3::Services::Organizations::ForCurrentUser do
     before  { get("/v3/orgs", {}, headers)     }
     example { expect(last_response).to be_ok   }
     example { expect(JSON.load(body)).to be == {
-      "@type"         => "organizations",
-      "@href"         => "/v3/orgs",
-      "organizations" => [{
-        "@type"       => "organization",
-        "@href"       => "/v3/org/#{org.id}",
-        "id"          => org.id,
-        "login"       => "example-org",
-        "name"        => nil,
-        "github_id"   => nil,
-        "avatar_url"  => nil
+      "@type"          => "organizations",
+      "@href"          => "/v3/orgs",
+      "organizations"  => [{
+        "@type"        => "organization",
+        "@href"        => "/v3/org/#{org.id}",
+        "@permissions" => { "read"=>true, "sync"=>true },
+        "id"           => org.id,
+        "login"        => "example-org",
+        "name"         => nil,
+        "github_id"    => nil,
+        "avatar_url"   => nil
       }]
     }}
   end
diff --git a/spec/v3/services/owner/find_spec.rb b/spec/v3/services/owner/find_spec.rb
index b5dd0c82..53336475 100644
--- a/spec/v3/services/owner/find_spec.rb
+++ b/spec/v3/services/owner/find_spec.rb
@@ -10,13 +10,14 @@ describe Travis::API::V3::Services::Owner::Find do
       before  { get("/v3/owner/example-org")     }
       example { expect(last_response).to be_ok   }
       example { expect(JSON.load(body)).to be == {
-        "@type"      => "organization",
-        "@href"      => "/v3/org/#{org.id}",
-        "id"         => org.id,
-        "login"      => "example-org",
-        "name"       => nil,
-        "github_id"  => nil,
-        "avatar_url" => nil
+        "@type"        => "organization",
+        "@href"        => "/v3/org/#{org.id}",
+        "@permissions" => { "read"=>true, "sync"=>false },
+        "id"           => org.id,
+        "login"        => "example-org",
+        "name"         => nil,
+        "github_id"    => nil,
+        "avatar_url"   => nil
       }}
     end
 
@@ -31,6 +32,7 @@ describe Travis::API::V3::Services::Owner::Find do
       example { expect(JSON.load(body)).to be == {
         "@type"             => "organization",
         "@href"             => "/v3/org/#{org.id}",
+        "@permissions"      => { "read"=>true, "sync"=>false },
         "id"                => org.id,
         "login"             => "example-org",
         "name"              => nil,
@@ -73,6 +75,7 @@ describe Travis::API::V3::Services::Owner::Find do
       example { expect(JSON.load(body)).to be == {
         "@type"             => "organization",
         "@href"             => "/v3/org/#{org.id}",
+        "@permissions"      => { "read"=>true, "sync"=>false },
         "id"                => org.id,
         "login"             => "example-org",
         "name"              => nil,
@@ -108,13 +111,14 @@ describe Travis::API::V3::Services::Owner::Find do
       before  { get("/v3/owner/example-ORG")     }
       example { expect(last_response).to be_ok   }
       example { expect(JSON.load(body)).to be == {
-        "@type"      => "organization",
-        "@href"      => "/v3/org/#{org.id}",
-        "id"         => org.id,
-        "login"      => "example-org",
-        "name"       => nil,
-        "github_id"  => nil,
-        "avatar_url" => nil
+        "@type"        => "organization",
+        "@href"        => "/v3/org/#{org.id}",
+        "@permissions" => { "read"=>true, "sync"=>false },
+        "id"           => org.id,
+        "login"        => "example-org",
+        "name"         => nil,
+        "github_id"    => nil,
+        "avatar_url"   => nil
       }}
     end
 
@@ -126,13 +130,14 @@ describe Travis::API::V3::Services::Owner::Find do
       before  { get("/v3/owner/example-org?organization.id=#{other.id}") }
       example { expect(last_response).to be_ok   }
       example { expect(JSON.load(body)).to be == {
-        "@type"      => "organization",
-        "@href"      => "/v3/org/#{org.id}",
-        "id"         => org.id,
-        "login"      => "example-org",
-        "name"       => nil,
-        "github_id"  => nil,
-        "avatar_url" => nil
+        "@type"        => "organization",
+        "@href"        => "/v3/org/#{org.id}",
+        "@permissions" => { "read"=>true, "sync"=>false },
+        "id"           => org.id,
+        "login"        => "example-org",
+        "name"         => nil,
+        "github_id"    => nil,
+        "avatar_url"   => nil
       }}
     end
   end