Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#26] Add support for setting the environment (whether production or sandbox) #27

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions src/Oauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ class Oauth
**/
private $level = 'pub';

/**
* The ORCID environment type
*
* @var string
**/
private $environment = '';

/**
* The oauth client ID
*
Expand Down Expand Up @@ -149,6 +156,30 @@ public function useMembersApi()
return $this;
}

/**
* Sets the oauth instance to use the production environment
*
* @return $this
**/
public function useProductionEnvironment()
{
$this->environment = '';

return $this;
}

/**
* Sets the oauth instance to use the sandbox environment
*
* @return $this
**/
public function useSandboxEnvironment()
{
$this->environment = 'sandbox';

return $this;
}

/**
* Sets the client ID for future use
*
Expand Down Expand Up @@ -351,7 +382,9 @@ public function getAuthorizationUrl()
}

// Start building url (enpoint is the same for public and member APIs)
$url = 'https://' . self::HOSTNAME . '/' . self::AUTHORIZE;
$url = 'https://';
$url .= (!empty($this->environment)) ? $this->environment . '.' : '';
$url .= self::HOSTNAME . '/' . self::AUTHORIZE;
$url .= '?client_id=' . $this->clientId;
$url .= '&scope=' . $this->scope;
$url .= '&redirect_uri=' . urlencode($this->redirectUri);
Expand Down Expand Up @@ -400,7 +433,12 @@ public function authenticate($code)
'grant_type' => 'authorization_code'
];

$this->http->setUrl('https://' . $this->level . '.' . self::HOSTNAME . '/' . self::TOKEN)
$url = 'https://';
$url .= $this->level . '.';
$url .= (!empty($this->environment)) ? $this->environment . '.' : '';
$url .= self::HOSTNAME . '/' . self::TOKEN;

$this->http->setUrl($url)
->setPostFields($fields)
->setHeader(['Accept' => 'application/json']);

Expand Down Expand Up @@ -472,6 +510,7 @@ private function getApiEndpoint($endpoint, $orcid = null)
{
$url = ($this->level == 'pub') ? 'http://' : 'https://';
$url .= $this->level . '.';
$url .= (!empty($this->environment)) ? $this->environment . '.' : '';
$url .= self::HOSTNAME;
$url .= '/v1.2/';
$url .= $orcid ?: $this->getOrcid();
Expand Down
176 changes: 175 additions & 1 deletion tests/OauthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function oauth()
public function testGetBasicAuthorizationUrl()
{
$oauth = $this->oauth()
->useProductionEnvironment()
->setClientId('1234')
->setScope('/authorize')
->setRedirectUri('here');
Expand All @@ -42,6 +43,40 @@ public function testGetBasicAuthorizationUrl()
$oauth->getAuthorizationUrl(),
'Failed to fetch a properly formatted authorization URL'
);

$oauth->useMembersApi();
$this->assertEquals(
'https://orcid.org/oauth/authorize?client_id=1234&scope=/authorize&redirect_uri=here&response_type=code',
$oauth->getAuthorizationUrl(),
'Failed to fetch a properly formatted authorization URL'
);
}

/**
* Test to make sure we can get a basic authorization url for the sandbox environment
*
* @return void
**/
public function testGetBasicSandboxAuthorizationUrl()
{
$oauth = $this->oauth()
->useSandboxEnvironment()
->setClientId('1234')
->setScope('/authorize')
->setRedirectUri('here');

$this->assertEquals(
'https://sandbox.orcid.org/oauth/authorize?client_id=1234&scope=/authorize&redirect_uri=here&response_type=code',
$oauth->getAuthorizationUrl(),
'Failed to fetch a properly formatted authorization URL'
);

$oauth->useMembersApi();
$this->assertEquals(
'https://sandbox.orcid.org/oauth/authorize?client_id=1234&scope=/authorize&redirect_uri=here&response_type=code',
$oauth->getAuthorizationUrl(),
'Failed to fetch a properly formatted authorization URL'
);
}

/**
Expand Down Expand Up @@ -228,6 +263,104 @@ public function testIsAuthenticatedFailsWithNoAccessToken()
$this->assertFalse($this->oauth()->isAuthenticated(), 'The oauth object failed to report that it was unauthenticated.');
}

/**
* Test to make sure we build the appropriate production url for public api token requests
*
* @return void
**/
public function testBuildPublicProductionTokenUrl()
{
$http = m::mock('Orcid\Http\Curl');
$http->shouldReceive('setPostFields', 'setHeader')->andReturn(m::self())
->getMock()
->shouldReceive('setUrl')->once()->with('https://pub.orcid.org/oauth/token')->andReturn(m::self());

// Tell the curl method to return an empty ORCID iD
$response = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'response-success.json');
$http->shouldReceive('execute')->andReturn($response);

$oauth = m::mock('Orcid\Oauth', [$http])->makePartial();
$oauth->useProductionEnvironment()
->setClientId('1234')
->setClientSecret('12345')
->setRedirectUri('here')
->authenticate('123456');
}

/**
* Test to make sure we build the appropriate production url for member api token requests
*
* @return void
**/
public function testBuildMemberProductionTokenUrl()
{
$http = m::mock('Orcid\Http\Curl');
$http->shouldReceive('setPostFields', 'setHeader')->andReturn(m::self())
->getMock()
->shouldReceive('setUrl')->once()->with('https://api.orcid.org/oauth/token')->andReturn(m::self());

// Tell the curl method to return an empty ORCID iD
$response = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'response-success.json');
$http->shouldReceive('execute')->andReturn($response);

$oauth = m::mock('Orcid\Oauth', [$http])->makePartial();
$oauth->useProductionEnvironment()
->useMembersApi()
->setClientId('1234')
->setClientSecret('12345')
->setRedirectUri('here')
->authenticate('123456');
}

/**
* Test to make sure we build the appropriate sandbox url for public api token requests
*
* @return void
**/
public function testBuildPublicSandboxTokenUrl()
{
$http = m::mock('Orcid\Http\Curl');
$http->shouldReceive('setPostFields', 'setHeader')->andReturn(m::self())
->getMock()
->shouldReceive('setUrl')->once()->with('https://pub.sandbox.orcid.org/oauth/token')->andReturn(m::self());

// Tell the curl method to return an empty ORCID iD
$response = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'response-success.json');
$http->shouldReceive('execute')->andReturn($response);

$oauth = m::mock('Orcid\Oauth', [$http])->makePartial();
$oauth->useSandboxEnvironment()
->setClientId('1234')
->setClientSecret('12345')
->setRedirectUri('here')
->authenticate('123456');
}

/**
* Test to make sure we build the appropriate sandbox url for member api token requests
*
* @return void
**/
public function testBuildMemberSandboxTokenUrl()
{
$http = m::mock('Orcid\Http\Curl');
$http->shouldReceive('setPostFields', 'setHeader')->andReturn(m::self())
->getMock()
->shouldReceive('setUrl')->once()->with('https://api.sandbox.orcid.org/oauth/token')->andReturn(m::self());

// Tell the curl method to return an empty ORCID iD
$response = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'response-success.json');
$http->shouldReceive('execute')->andReturn($response);

$oauth = m::mock('Orcid\Oauth', [$http])->makePartial();
$oauth->useSandboxEnvironment()
->useMembersApi()
->setClientId('1234')
->setClientSecret('12345')
->setRedirectUri('here')
->authenticate('123456');
}

/**
* Test to make sure we can get a profile with the public api
*
Expand Down Expand Up @@ -273,7 +406,10 @@ public function testGetMemberProfileUsesProperUrl()
->shouldReceive('setUrl')->once()->with('https://api.orcid.org/v1.2/0000-0000-0000-0000/orcid-profile');

$oauth = m::mock('Orcid\Oauth', [$http])->makePartial();
$oauth->useMembersApi()->setAccessToken('123456789')->getProfile('0000-0000-0000-0000');
$oauth->useMembersApi()
->useProductionEnvironment()
->setAccessToken('123456789')
->getProfile('0000-0000-0000-0000');
}

/**
Expand All @@ -292,4 +428,42 @@ public function testGetMemberProfileWithNoAccessTokenThrowsException()
$oauth = m::mock('Orcid\Oauth', [$http])->makePartial();
$oauth->useMembersApi()->getProfile('0000-0000-0000-0000');
}

/**
* Test to make we use the proper sandbox url when fetching a sandbox profile
*
* @return void
**/
public function testGetMemberSandboxProfileUsesProperUrl()
{
$http = m::mock('Orcid\Http\Curl');
$http->shouldReceive('execute', 'setHeader', 'setOpt')->andReturn(m::self())
->getMock()
->shouldReceive('setUrl')->once()->with('https://api.sandbox.orcid.org/v1.2/0000-0000-0000-0000/orcid-profile');

$oauth = m::mock('Orcid\Oauth', [$http])->makePartial();
$oauth->useMembersApi()
->useSandboxEnvironment()
->setAccessToken('123456789')
->getProfile('0000-0000-0000-0000');
}

/**
* Test to make we use the proper sandbox url when fetching a sandbox profile
*
* @return void
**/
public function testGetPublicSandboxProfileUsesProperUrl()
{
$http = m::mock('Orcid\Http\Curl');
$http->shouldReceive('execute', 'setHeader', 'setOpt')->andReturn(m::self())
->getMock()
->shouldReceive('setUrl')->once()->with('http://pub.sandbox.orcid.org/v1.2/0000-0000-0000-0000/orcid-profile');

$oauth = m::mock('Orcid\Oauth', [$http])->makePartial();
$oauth->usePublicApi()
->useSandboxEnvironment()
->setAccessToken('123456789')
->getProfile('0000-0000-0000-0000');
}
}