Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
root = true

[*]
indent_style = spaces
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
Expand Down
8 changes: 7 additions & 1 deletion Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ require 'yaml'
VAGRANTFILE_API_VERSION ||= "2"
confDir = $confDir ||= File.expand_path("vendor/laravel/homestead", File.dirname(__FILE__))

# Homestead.yml is the settings file that ships with the repo. Homestead.yaml /
# Homestead.json are gitignored, so a developer can drop one in to override it
# locally; those take precedence.
homesteadYamlPath = File.expand_path("Homestead.yaml", File.dirname(__FILE__))
homesteadJsonPath = File.expand_path("Homestead.json", File.dirname(__FILE__))
homesteadYmlPath = File.expand_path("Homestead.yml", File.dirname(__FILE__))
afterScriptPath = "after.sh"
aliasesPath = "aliases"

Expand All @@ -28,8 +32,10 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
settings = YAML::load(File.read(homesteadYamlPath))
elsif File.exist? homesteadJsonPath then
settings = JSON.parse(File.read(homesteadJsonPath))
elsif File.exist? homesteadYmlPath then
settings = YAML::load(File.read(homesteadYmlPath))
else
abort "Homestead settings file not found in #{confDir}"
abort "Homestead settings file not found in #{File.dirname(__FILE__)}"
end

# Patch around the pitfalls of the imho dreadful composer update script in Homestead
Expand Down
13 changes: 0 additions & 13 deletions behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@ default:
baseUrl: http://localhost:8000
# proxyUrl: localhost:8888
- Ushahidi\Tests\Integration\Bootstrap\PHPUnitFixtureContext
extensions:
Behat\MinkExtension:
default_session: laravel
base_url: http://localhost:8000
laravel: ~
sessions:
default:
goutte: ~
goutte:
guzzle_parameters:
curl.options:
3 : 8000 #CURLOPT_PORT=3
# 10004: localhost:8888 #CURLOPT_PROXY=10004
ci:
suites:
default:
Expand Down
81 changes: 0 additions & 81 deletions tests/Feature/V3/ExportJobAPI.php

This file was deleted.

100 changes: 100 additions & 0 deletions tests/Feature/V3/ExportJobAPITest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Ushahidi\Tests\Feature\V3;

use Faker;
use Illuminate\Support\Facades\Bus;
use Ushahidi\Authzn\GenericUser;
use Ushahidi\Core\Entity\User;
use Ushahidi\Modules\V3\Jobs\ExportPostsJob;
use Ushahidi\Tests\TestCase;

/**
* @group api
* @group integration
*/
class ExportJobAPITest extends TestCase
{
private const REQUEST = [
'fields' => 'test',
'filters' => [
'status' => ['published', 'draft'],
],
'entity_type' => 'post',
'send_to_browser' => true,
'send_to_hdx' => false,
'hxl_heading_row' => ['something'],
];

private const EXPECTED = [
'fields' => ['test'],
'filters' => [
'status' => ['published', 'draft'],
],
'entity_type' => 'post',
'send_to_browser' => true,
'send_to_hdx' => false,
'hxl_heading_row' => null,
];

/**
* Creating an export job requires admin rights. The test owns its admin
* user rather than relying on a fixture: the behat dataset is not loaded
* for phpunit runs, and it rewrites the users table when it is.
*/
protected $adminId;

public function setUp(): void
{
parent::setUp();

$faker = Faker\Factory::create();

$this->adminId = service('repository.user')->create(new User([
'email' => $faker->unique()->safeEmail,
'realname' => 'Export Admin',
'role' => 'admin',
]));
}

public function tearDown(): void
{
service('repository.user')->delete(new User(['id' => $this->adminId]));

parent::tearDown();
}

private function postJob()
{
return $this->actingAs(new GenericUser(['id' => $this->adminId]))
->json('POST', '/api/v3/exports/jobs', self::REQUEST);
}

/**
* Create a job, asserting the export is queued rather than run inline.
*/
public function testCreateJob()
{
Bus::fake();

$this->withoutMiddleware();

$this->postJob()
->assertStatus(200)
->assertJson(self::EXPECTED);

Bus::assertDispatched(ExportPostsJob::class);
}

/**
* Create a job and let the export actually dispatch.
*/
public function testCreateJobWithRealDispatch()
{
$this->withoutMiddleware();

$this->postJob()
->assertStatus(200)
->assertJson(self::EXPECTED);
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
<?php

namespace Ushahidi\Tests\Feature\V3;

use Faker;
use Ushahidi\Tests\TestCase;
use Ushahidi\Core\Entity\ApiKey;
use Ushahidi\Core\Entity\Post;
use Ushahidi\Tests\TestCase;

/**
* @group api
* @group integration
*/
class WebhooksPostsUpdateAPI extends TestCase
class WebhooksPostsUpdateAPITest extends TestCase
{
protected $postId;

/**
* Moved this out of setup.
* Helper to set the user role and the job correctly since we
* override this setup in the test for authorization
* @param $user_role
*/
public function setUp()
public function setUp(): void
{
parent::setUp();

Expand All @@ -32,7 +27,7 @@ public function setUp()
]));
}

public function tearDown()
public function tearDown(): void
{
service('repository.post')->delete(new Post(['id' => $this->postId]));

Expand All @@ -46,31 +41,27 @@ protected function makeSig($sharedSecret, $url, $payload)
return base64_encode(hash_hmac('sha256', $data, $sharedSecret, true));
}

/**
* Get count
*/
public function testUpdate()
{
$this->withoutMiddleware();

$this->json('PUT', '/api/v3/webhooks/posts/'.$this->postId, [
'title' => 'Updated',
'content' => 'Also updated',
]);

$this->seeStatusCode('200')
->seeJson([
])
->assertStatus(200)
->assertJson([
'title' => 'Updated',
'content' => 'Also updated',
]);
}

/**
* Update a job
* Middleware is left enabled here: the point of the test is that a request
* carrying a valid X-Ushahidi-Signature is accepted.
*/
public function testUpdateWithSignature()
{
// Re-enable middleware
$this->app->instance('middleware.disable', false);
// Set the shared secret
$originalSecret = getenv('PLATFORM_SHARED_SECRET');
putenv('PLATFORM_SHARED_SECRET=asharedsecret');
Expand All @@ -80,32 +71,23 @@ public function testUpdateWithSignature()
$apiKeyId = $apiKeys->create(new ApiKey([]));
$apiKey = $apiKeys->get($apiKeyId);

$url = '/api/v3/webhooks/posts/'.$this->postId.'?api_key='.$apiKey->api_key;

$payload = [
'title' => 'Updated w/sig',
'content' => 'Also updated',
];

// Make a signature
$sig = $this->makeSig(
'asharedsecret',
$this->prepareUrlForRequest(
'/api/v3/webhooks/posts/'.$this->postId.'?api_key='.$apiKey->api_key
),
json_encode([
'title' => 'Updated w/sig',
'content' => 'Also updated',
])
$this->prepareUrlForRequest($url),
json_encode($payload)
);

$this->json(
'PUT',
'/api/v3/webhooks/posts/'.$this->postId.'?api_key='.$apiKey->api_key,
[
'title' => 'Updated w/sig',
'content' => 'Also updated',
],
[
'X-Ushahidi-Signature' => $sig,
]
);

$this->seeStatusCode('200')
->seeJsonStructure([
$this->json('PUT', $url, $payload, ['X-Ushahidi-Signature' => $sig])
->assertStatus(200)
->assertJsonStructure([
'id',
'user_id',
'type',
Expand All @@ -116,10 +98,7 @@ public function testUpdateWithSignature()
'created',
'updated',
])
->seeJson([
'title' => 'Updated w/sig',
'content' => 'Also updated',
]);
->assertJson($payload);

// Clean up
$apiKeys->delete($apiKey);
Expand Down
Loading