Skip to content

Commit

Permalink
Add filter, risk and log endpoint clients (#41)
Browse files Browse the repository at this point in the history

Co-authored-by: Rafal Malinowski <rafal@malinowski.be>
Co-authored-by: bartes <bartesrlz@gmail.com>
  • Loading branch information
3 people committed Jun 2, 2021
1 parent 6adbf7b commit 128f21c
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 39 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -110,3 +110,7 @@ Whenever something unexpected happens, an exception is thrown to indicate what w

## Running test suite
Execute `vendor/bin/phpunit test` to run the full test suite

## Documentation

[Official Castle docs](https://docs.castle.io)
42 changes: 42 additions & 0 deletions lib/Castle/Castle.php
Expand Up @@ -129,4 +129,46 @@ public static function track(Array $attributes)
$request = new Castle_Request();
$request->send('post', '/track', $attributes);
}


/**
* Filter an action
* @param String $attributes 'request_token', 'event', 'context' are required, 'user' with 'id' and 'properties' are optional
* @return Castle_Log
*/
public static function filter(Array $attributes)
{
$request = new Castle_Request();
list($response, $request) = $request->send('post', '/filter', $attributes);
if ($request->rStatus == 204) {
$response = array();
}
return new RestModel($response);
}

/**
* Log events
* @param String $attributes 'request_token', 'event', 'status' and 'user' object with 'id' are required
* @return Castle_Log
*/
public static function log(Array $attributes)
{
$request = new Castle_Request();
$request->send('post', '/log', $attributes);
}

/**
* Risk
* @param String $attributes 'request_token', 'event', 'context', 'user' with 'id' are required, 'status', 'properties' are optional
* @return Castle_Risk
*/
public static function risk(Array $attributes)
{
$request = new Castle_Request();
list($response, $request) = $request->send('post', '/risk', $attributes);
if ($request->rStatus == 204) {
$response = array();
}
return new RestModel($response);
}
}
37 changes: 36 additions & 1 deletion test/CastleTest.php
Expand Up @@ -16,7 +16,7 @@ public function setUp(): void

public function testSetApiKey()
{
$this->assertContains('secretkey', Castle::getApiKey());
$this->assertEquals('secretkey', Castle::getApiKey());
}

public function testTrack()
Expand All @@ -26,6 +26,41 @@ public function testTrack()
$this->assertRequest('post', '/track');
}

public function testFilter()
{
Castle_RequestTransport::setResponse(204, '');
Castle::filter(Array(
'request_token' => '7e51335b-f4bc-4bc7-875d-b713fb61eb23-bf021a3022a1a302',
'name' => '$registration',
'user' => Array('id' => 'abc', 'email' => 'user@foobar.io')
));
$this->assertRequest('post', '/filter');
}

public function testLog()
{
Castle_RequestTransport::setResponse(204, '');
Castle::log(Array(
'request_token' => '7e51335b-f4bc-4bc7-875d-b713fb61eb23-bf021a3022a1a302',
'name' => '$login',
'status' => '$failed',
'user' => Array('id' => 'abc', 'email' => 'user@foobar.io')
));
$this->assertRequest('post', '/log');
}

public function testRisk()
{
Castle_RequestTransport::setResponse(204, '');
Castle::risk(Array(
'request_token' => '7e51335b-f4bc-4bc7-875d-b713fb61eb23-bf021a3022a1a302',
'name' => '$login',
'status' => '$succeeded',
'user' => Array('id' => 'abc', 'email' => 'user@foobar.io')
));
$this->assertRequest('post', '/risk');
}

public function testAuthenticate()
{
Castle_RequestTransport::setResponse(201, '{ "status": "approve" }');
Expand Down
30 changes: 12 additions & 18 deletions test/ErrorsTest.php
Expand Up @@ -11,57 +11,51 @@ public function setUp(): void
$this->request = new Castle_Request();
}

/**
* @expectedException Castle_BadRequest
*/
public function testBadRequest()
{
Castle_RequestTransport::setResponse(400);

$this->expectException(Castle_BadRequest::class);
$this->request->send('GET', '/test');
}

/**
* @expectedException Castle_UnauthorizedError
*/
public function testUnauthorized()
{
Castle_RequestTransport::setResponse(401);

$this->expectException(Castle_UnauthorizedError::class);
$this->request->send('GET', '/test');
}

/**
* @expectedException Castle_ForbiddenError
*/
public function testForbidden()
{
Castle_RequestTransport::setResponse(403);

$this->expectException(Castle_ForbiddenError::class);
$this->request->send('GET', '/test');
}

/**
* @expectedException Castle_NotFoundError
*/
public function testNotFound()
{
Castle_RequestTransport::setResponse(404);

$this->expectException(Castle_NotFoundError::class);
$this->request->send('GET', '/test');
}

/**
* @expectedException Castle_InvalidParametersError
*/
public function testInvalidParameters()
{
Castle_RequestTransport::setResponse(422);

$this->expectException(Castle_InvalidParametersError::class);
$this->request->send('GET', '/test');
}

/**
* @expectedException Castle_ConfigurationError
*/
public function testConfiguration()
{
Castle::setApiKey(null);

$this->expectException(Castle_ConfigurationError::class);
$this->request->send('GET', '/test');
}
}
30 changes: 10 additions & 20 deletions test/RequestTest.php
Expand Up @@ -23,9 +23,6 @@ public function tearDown(): void
Castle_RequestTransport::setResponse();
}

/**
* @expectedException Castle_CurlOptionError
*/
public function testCastleCurlOptions()
{
// Will not throw.
Expand All @@ -34,58 +31,51 @@ public function testCastleCurlOptions()
CURLOPT_TIMEOUT => 1,
CURLOPT_TIMEOUT_MS => 1000));
// Will throw.
$this->expectException(Castle_CurlOptionError::class);
Castle::setCurlOpts(array(CURLOPT_USERAGENT => "BadBrowser/6.6.6b"));
}



/**
* @expectedException Castle_ApiError
*/
public function testInvalidResponse()
{
Castle_RequestTransport::setResponse(200, '{invalid');
$req = new Castle_Request();

$this->expectException(Castle_ApiError::class);
$req->send('GET', '/users');
}

/**
* @expectedException Castle_ApiError
*/
public function testApiErrorRequest()
{
Castle_RequestTransport::setResponse(500);
$req = new Castle_Request();

$this->expectException(Castle_ApiError::class);
$req->send('GET', '/users');
}

/**
* @expectedException Castle_UnauthorizedError
*/
public function testUnauthorizedRequest()
{
Castle_RequestTransport::setResponse(401);
$req = new Castle_Request();

$this->expectException(Castle_UnauthorizedError::class);
$req->send('GET', '/users');
}

/**
* @expectedException Castle_ForbiddenError
*/
public function testForbiddenRequest()
{
Castle_RequestTransport::setResponse(403);
$req = new Castle_Request();

$this->expectException(Castle_ForbiddenError::class);
$req->send('GET', '/users');
}

/**
* @expectedException Castle_InvalidParametersError
*/
public function testInvalidParametersRequest()
{
Castle_RequestTransport::setResponse(422);
$req = new Castle_Request();
$this->expectException(Castle_InvalidParametersError::class);
$req->send('GET', '/users');
}
}

0 comments on commit 128f21c

Please sign in to comment.