Skip to content

Commit

Permalink
Added API for Add/Update Organization Membership. Refs[piotrmurach#238]
Browse files Browse the repository at this point in the history
  • Loading branch information
Shwetakale committed Oct 4, 2015
1 parent c7b4e08 commit 453e16e
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 9 deletions.
@@ -0,0 +1 @@
{"http_interactions":[{"request":{"method":"put","uri":"<BASIC_AUTH>@api.github.com/orgs/CodeCu/memberships/anuja-joshi?access_token=<TOKEN>","body":{"encoding":"UTF-8","base64_string":"eyJyb2xlIjoiYWRtaW4ifQ==\n"},"headers":{"Accept":["application/vnd.github.v3+json,application/vnd.github.beta+json;q=0.5,application/json;q=0.1"],"Accept-Charset":["utf-8"],"User-Agent":["Github API Ruby Gem 0.12.4"],"Content-Type":["application/json"],"Accept-Encoding":["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"]}},"response":{"status":{"code":200,"message":"OK"},"headers":{"Server":["GitHub.com"],"Date":["Sat, 03 Oct 2015 12:40:22 GMT"],"Content-Type":["application/json; charset=utf-8"],"Transfer-Encoding":["chunked"],"Status":["200 OK"],"X-Ratelimit-Limit":["5000"],"X-Ratelimit-Remaining":["4982"],"X-Ratelimit-Reset":["1443878521"],"Cache-Control":["private, max-age=60, s-maxage=60"],"Etag":["W/\"587ba2cea74984bad492f0d7d7100c7c\""],"Vary":["Accept, Authorization, Cookie, X-GitHub-OTP","Accept-Encoding"],"X-Github-Media-Type":["github.v3; format=json"],"X-Xss-Protection":["1; mode=block"],"X-Frame-Options":["deny"],"Content-Security-Policy":["default-src 'none'"],"Access-Control-Allow-Credentials":["true"],"Access-Control-Expose-Headers":["ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval"],"Access-Control-Allow-Origin":["*"],"X-Github-Request-Id":["728F85FA:1336B:84A54D8:560FCCB5"],"Strict-Transport-Security":["max-age=31536000; includeSubdomains; preload"],"X-Content-Type-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recorded_at":"Sat, 03 Oct 2015 12:40:22 GMT"}],"recorded_with":"VCR 2.6.0"}
@@ -0,0 +1 @@
{"http_interactions":[{"request":{"method":"put","uri":"https://<BASIC_AUTH>@api.github.com/orgs/CodeCu/memberships/anujaware?access_token=<TOKEN>","body":{"encoding":"UTF-8","base64_string":"eyJyb2xlIjoibWVtYmVyIn0=\n"},"headers":{"Accept":["application/vnd.github.v3+json,application/vnd.github.beta+json;q=0.5,application/json;q=0.1"],"Accept-Charset":["utf-8"],"User-Agent":["Github API Ruby Gem 0.12.4"],"Content-Type":["application/json"],"Accept-Encoding":["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"]}},"response":{"status":{"code":200,"message":"OK"},"headers":{"Server":["GitHub.com"],"Date":["Sat, 03 Oct 2015 12:40:20 GMT"],"Content-Type":["application/json; charset=utf-8"],"Transfer-Encoding":["chunked"],"Status":["200 OK"],"X-Ratelimit-Limit":["5000"],"X-Ratelimit-Remaining":["4983"],"X-Ratelimit-Reset":["1443878521"],"Cache-Control":["private, max-age=60, s-maxage=60"],"Etag":["W/\"20837e0876109f789503d390ad3035b9\""],"Vary":["Accept, Authorization, Cookie, X-GitHub-OTP","Accept-Encoding"],"X-Github-Media-Type":["github.v3; format=json"],"X-Xss-Protection":["1; mode=block"],"X-Frame-Options":["deny"],"Content-Security-Policy":["default-src 'none'"],"Access-Control-Allow-Credentials":["true"],"Access-Control-Expose-Headers":["ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval"],"Access-Control-Allow-Origin":["*"],"X-Github-Request-Id":["728F85FA:1336E:E3BC0A3:560FCCB3"],"Strict-Transport-Security":["max-age=31536000; includeSubdomains; preload"],"X-Content-Type-Options":["nosniff"],"X-Served-By":["76d9828c7e4f1d910f7ba069e90ce976"]},"body":{"encoding":"UTF-8","base64_string":"eyJ1cmwiOiJodHRwczovL2FwaS5naXRodWI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9hbnVqYXdhcmUiLCJm\nb2xsb3dlcnNfdXJsIjoiaHR0cHM6Ly9hcGkuZ2l0aHViLmNvbS91c2Vycy9h\nbnVqYXdhcmUvZm9sbG93ZXJzIiwiZm9sbG93aW5nX3VybCI6Imh0dHBzOi8v\nYXBpLmdpdGh1Yi5jb20vdXNlcnMvYW51amF3YXJlL2ZvbGxvd2luZ3svb3Ro\nZXJfdXNlcn0iLCJnaXN0c191cmwiOiJodHRwczovL2FwaS5naXRodWIuY29t\nL3VzZXJzL2FudWphd2FyZS9naXN0c3svZ2lzdF9pZH0iLCJzdGFycmVkX3Vy\nbCI6Imh0dHBzOi8vYXBpLmdpdGh1Yi5jb20vdXNlcnMvYW51amF3YXJlL3N0\nYXJyZWR7L293bmVyfXsvcmVwb30iLCJzdWJzY3JpcHRpb25zX3VybCI6Imh0\ndHBzOi8vYXBpLmdpdGh1Yi5jb20vdXNlcnMvYW51amF3YXJlL3N1YnNjcmlw\ndGlvbnMiLCJvcmdhbml6YXRpb25zX3VybCI6Imh0dHBzOi8vYXBpLmdpdGh1\nYi5jb20vdXNlcnMvYW51amF3YXJlL29yZ3MiLCJyZXBvc191cmwiOiJodHRw\nczovL2FwaS5naXRodWIuY29tL3VzZXJzL2FudWphd2FyZS9yZXBvcyIsImV2\nZW50c191cmwiOiJodHRwczovL2FwaS5naXRodWIuY29tL3VzZXJzL2FudWph\nd2FyZS9ldmVudHN7L3ByaXZhY3l9IiwicmVjZWl2ZWRfZXZlbnRzX3VybCI6\nImh0dHBzOi8vYXBpLmdpdGh1Yi5jb20vdXNlcnMvYW51amF3YXJlL3JlY2Vp\ndmVkX2V2ZW50cyIsInR5cGUiOiJVc2VyIiwic2l0ZV9hZG1pbiI6ZmFsc2V9\nfQ==\n"},"http_version":null},"recorded_at":"Sat, 03 Oct 2015 12:40:20 GMT"}],"recorded_with":"VCR 2.6.0"}
27 changes: 27 additions & 0 deletions features/orgs/members.feature
Expand Up @@ -55,3 +55,30 @@ Feature: Members API
| true |
When I make request within a cassette named "orgs/members/member_public_true"
Then the response should be true

Scenario: Add/Update Organization Membership (unaffiliated user)

Given I want to grant_organization_membership resource with the following params:
| org | username |
| CodeCu | anujaware |
And I pass the following request options:
| role |
| member |
When I make request within a cassette named "orgs/members/grant_organization_membership_unaffiliated_user"
Then the response status should be 200
And the response type should be JSON
And the response should not be empty


Scenario: Add/Update Organization Membership (already member)

Given I want to grant_organization_membership resource with the following params:
| org | username |
| CodeCu | anuja-joshi |
And I pass the following request options:
| role |
| admin |
When I make request within a cassette named "orgs/members/grant_organization_membership_already_member"
Then the response status should be 200
And the response type should be JSON
And the response should not be empty
34 changes: 25 additions & 9 deletions lib/github_api/client/orgs/members.rb
Expand Up @@ -39,10 +39,10 @@ def list(*args)
org_name = arguments.org_name

response = if params.delete('public')
get_request("/orgs/#{org_name}/public_members", params)
else
get_request("/orgs/#{org_name}/members", params)
end
get_request("/orgs/#{org_name}/public_members", params)
else
get_request("/orgs/#{org_name}/members", params)
end
return response unless block_given?
response.each { |el| yield el }
end
Expand All @@ -67,15 +67,15 @@ def member?(*args)
user = arguments.user

response = if params.delete('public')
get_request("/orgs/#{org_name}/public_members/#{user}", params)
else
get_request("/orgs/#{org_name}/members/#{user}", params)
end
get_request("/orgs/#{org_name}/public_members/#{user}", params)
else
get_request("/orgs/#{org_name}/members/#{user}", params)
end
response.status == 204
rescue Github::Error::NotFound
false
end

end
# Remove a member
#
# Removing a user from this list will remove them from all teams and
Expand Down Expand Up @@ -121,5 +121,21 @@ def conceal(*args)
delete_request("/orgs/#{arguments.org_name}/public_members/#{arguments.user}", arguments.params)
end
alias :conceal_membership :conceal

# Add/update user’s membership with organization
#
# @example
# github = Github.new oauth_token: '...'
# github.orgs.members.grant_organization_membership 'org-name', 'member-name', {role: 'role'}
#
# role is optional and can be member / admin. default - member
#
# @api public
def grant_organization_membership(*args)
params = arguments(args, required: [:org, :username]).params
org = arguments.org
username = arguments.username
response= put_request("/orgs/#{org}/memberships/#{username}", params)
end
end # Client::Orgs::Members
end # Github
37 changes: 37 additions & 0 deletions spec/fixtures/orgs/membership_to_affilliated_user.json
@@ -0,0 +1,37 @@
{
"url": "https://api.github.com/orgs/CodeCu/memberships/anuja-joshi",
"state": "active",
"role": "admin",
"organization_url": "https://api.github.com/orgs/CodeCu",
"organization": {
"login": "CodeCu",
"id": 14146669,
"url": "https://api.github.com/orgs/CodeCu",
"repos_url": "https://api.github.com/orgs/CodeCu/repos",
"events_url": "https://api.github.com/orgs/CodeCu/events",
"members_url": "https://api.github.com/orgs/CodeCu/members{/member}",
"public_members_url": "https://api.github.com/orgs/CodeCu/public_members{/member}",
"avatar_url": "https://avatars.githubusercontent.com/u/14146669?v=3",
"description": null
},
"user": {
"login": "anuja-joshi",
"id": 5124042,
"avatar_url": "https://avatars.githubusercontent.com/u/5124042?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/anuja-joshi",
"html_url": "https://github.com/anuja-joshi",
"followers_url": "https://api.github.com/users/anuja-joshi/followers",
"following_url": "https://api.github.com/users/anuja-joshi/following{/other_user}",
"gists_url": "https://api.github.com/users/anuja-joshi/gists{/gist_id}",
"starred_url": "https://api.github.com/users/anuja-joshi/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/anuja-joshi/subscriptions",
"organizations_url": "https://api.github.com/users/anuja-joshi/orgs",
"repos_url": "https://api.github.com/users/anuja-joshi/repos",
"events_url": "https://api.github.com/users/anuja-joshi/events{/privacy}",
"received_events_url": "https://api.github.com/users/anuja-joshi/received_events",
"type": "User",
"site_admin": false
}
}

37 changes: 37 additions & 0 deletions spec/fixtures/orgs/membership_to_unaffiliated_user.json
@@ -0,0 +1,37 @@
{
"url": "https://api.github.com/orgs/CodeCu/memberships/anujaware",
"state": "pending",
"role": "member",
"organization_url": "https://api.github.com/orgs/CodeCu",
"organization": {
"login": "CodeCu",
"id": 14146669,
"url": "https://api.github.com/orgs/CodeCu",
"repos_url": "https://api.github.com/orgs/CodeCu/repos",
"events_url": "https://api.github.com/orgs/CodeCu/events",
"members_url": "https://api.github.com/orgs/CodeCu/members{/member}",
"public_members_url": "https://api.github.com/orgs/CodeCu/public_members{/member}",
"avatar_url": "https://avatars.githubusercontent.com/u/14146669?v=3",
"description": null
},
"user": {
"login": "anujaware",
"id": 562048,
"avatar_url": "https://avatars.githubusercontent.com/u/562048?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/anujaware",
"html_url": "https://github.com/anujaware",
"followers_url": "https://api.github.com/users/anujaware/followers",
"following_url": "https://api.github.com/users/anujaware/following{/other_user}",
"gists_url": "https://api.github.com/users/anujaware/gists{/gist_id}",
"starred_url": "https://api.github.com/users/anujaware/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/anujaware/subscriptions",
"organizations_url": "https://api.github.com/users/anujaware/orgs",
"repos_url": "https://api.github.com/users/anujaware/repos",
"events_url": "https://api.github.com/users/anujaware/events{/privacy}",
"received_events_url": "https://api.github.com/users/anujaware/received_events",
"type": "User",
"site_admin": false
}
}

@@ -0,0 +1,47 @@
# encoding: utf-8

require 'spec_helper'

describe Github::Client::Orgs::Members, '#grant_organization_membership' do
let(:org) { 'CodeCu' }
let(:status) { 200 }

before {
stub_put(request_path).to_return(:body => body, :status => status,
:headers => {:content_type => "application/json; charset=utf-8"})
}

context 'Add/update organization membership - unaffiliated user' do

let(:username) { 'anujaware' }
let(:request_path) { "/orgs/#{org}/memberships/#{username}" }
let(:body) { fixture('orgs/membership_to_unaffiliated_user.json') }

it 'should create organization membership with pending state' do
response = subject.grant_organization_membership org, username, {role: 'member'}
expect(response.state).to eq 'pending'
end

it 'should create organization membership with role member' do
response = subject.grant_organization_membership org, username, {role: 'member'}
expect(response.role).to eq 'member'
end
end

context 'Add/update organization membership - affiliated user' do

let(:username) { 'anuja-joshi' }
let(:request_path) { "/orgs/#{org}/memberships/#{username}" }
let(:body) { fixture('orgs/membership_to_affilliated_user.json') }

it 'should create organization membership with active state' do
response = subject.grant_organization_membership org, username, {role: 'admin'}
expect(response.state).to eq 'active'
end

it 'should update organization membership with role admin' do
response = subject.grant_organization_membership org, username, {role: 'admin'}
expect(response.role).to eq 'admin'
end
end
end # grant_organization_membership

0 comments on commit 453e16e

Please sign in to comment.