Skip to content

Commit

Permalink
Store the api key in the plugin directory
Browse files Browse the repository at this point in the history
* WordPress SaaS's ABS directory is owned by root, the plugin directory is owned by the WordPress user process.
* Move the file on plugin load if it exists, the file should be in the new location.
* Fix the docker compose file to allow the volume to have the same permission of my workstation.
* Amend tests to include use the new encryption key file.
  • Loading branch information
Jack-Dane committed Nov 3, 2024
1 parent 3cef8ea commit c8427a3
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ vendor/
.idea

*.phpunit.result.cache

odoo_conn.key
1 change: 1 addition & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
wordpress:
image: wordpress:apache
restart: always
user: 1000:1000
ports:
- 8080:80
environment:
Expand Down
4 changes: 2 additions & 2 deletions encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class OdooConnEncryptionFileHandler

public function __construct()
{
$this->encryption_key_path = ABSPATH . "odoo_conn.key";
$this->encryption_key_path = plugin_dir_path(__FILE__) . "odoo_conn.key";
}

public function write_to_file($api_key)
Expand Down Expand Up @@ -38,7 +38,7 @@ public function write_to_file($api_key)
} finally {
// https://github.com/Jack-Dane/odoo-wp-plugin/issues/2
// Sometimes (Integration test) the encryption_file isn't seen as a resource to flock
// add this check to avoid 500 errors when intially creating data in Wordpress
// add this check to avoid 500 errors when initially creating data in WordPress
// until a better solution is found, this will do.
if (is_resource($encryption_file)) {
flock($encryption_file, LOCK_UN);
Expand Down
13 changes: 13 additions & 0 deletions loaded.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
<?php

function move_existing_encryption_file(): void
{
// Original encryption file path that isn't compatible with WordPress SaaS.
// The directory is owned by root in WordPress SaaS.
$old_encryption_key_path = ABSPATH . "odoo_conn.key";
$new_encryption_key_path = plugin_dir_path(__FILE__) . "odoo_conn.key";

if (file_exists($old_encryption_key_path)) {
rename($old_encryption_key_path, $new_encryption_key_path);
}
}

function odoo_conn_update_db_check()
{
require_once(ABSPATH . "wp-admin/includes/upgrade.php");
Expand Down Expand Up @@ -48,6 +60,7 @@ function odoo_conn_add_multi_table_column() {
$wpdb->query($sql);
}

add_action("plugins_loaded", "move_existing_encryption_file");
add_action("plugins_loaded", "odoo_conn_update_db_check");

?>
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace php\admin\database_connection\cf7_posts;

require_once __DIR__ . '/../../../TestClassBrainMonkey.php';

use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use odoo_conn\admin\database_connection\OdooConnGetContact7Form;
use PHPUnit\Framework\TestCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
namespace odoo_conn\tests\OdooConnEncryptionFileHandler_Test;

use odoo_conn\encryption\OdooConnEncryptionFileHandler;
use \org\bovigo\vfs\vfsStream;
use \PHPUnit\Framework\TestCase;

define("ABSPATH", "vfs://root/");
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
use Brain\Monkey\Functions;

require_once(__DIR__ . "/../../../encryption.php");

Expand All @@ -15,13 +14,18 @@ class OdooConnEncryptionFileHandler_read_from_file_Test extends TestCase

public function setUp(): void
{
if (!defined("ABSPATH")) {
define("ABSPATH", "vfs://root/");
}
Functions\when("plugin_dir_path")->justReturn("vfs://root/plugins/odoo-wp-plugin/");

$this->root = vfsStream::setup("root", 0777);
$this->file_handler = new OdooConnEncryptionFileHandler();
}

public function test_existing_file()
{
vfsStream::newFile("odoo_conn.key")->at($this->root)->setContent("abc");
vfsStream::newFile("plugins/odoo-wp-plugin/odoo_conn.key")->at($this->root)->setContent("abc");

$key = $this->file_handler->read_from_file();

Expand All @@ -30,7 +34,7 @@ public function test_existing_file()

public function test_non_existing_file()
{
$this->assertFalse($this->root->hasChild("odoo_conn.key"));
$this->assertFalse($this->root->hasChild("plugins/odoo-wp-plugin/odoo_conn.key"));

$key = $this->file_handler->read_from_file();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

use odoo_conn\encryption\OdooConnEncryptionFileHandler;
use \org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStream;
use phpmock\phpunit\PHPMock;
use \PHPUnit\Framework\TestCase;

define("ABSPATH", "vfs://root/");
use PHPUnit\Framework\TestCase;
use Brain\Monkey\Functions;

require_once(__DIR__ . "/../../../encryption.php");

Expand All @@ -16,15 +15,21 @@ class OdooConnEncryptionFileHandler_write_to_file_Test extends TestCase

public function setUp(): void
{
if (!defined("ABSPATH")) {
define("ABSPATH", "vfs://root/");
}
Functions\when("plugin_dir_path")->justReturn("vfs://root/plugins/odoo-wp-plugin/");

$this->root = vfsStream::setup("root", 0777);
$this->flock = $this->getFunctionMock("odoo_conn\\encryption", "flock");
vfsStream::newDirectory("/plugins/odoo-wp-plugin/")->at($this->root);
$this->file_handler = new OdooConnEncryptionFileHandler();
$this->flock = $this->getFunctionMock("odoo_conn\\encryption", "flock");
$this->sleep = $this->getFunctionMock("odoo_conn\\encryption", "sleep");
}

public function test_ok()
{
vfsStream::newFile("odoo_conn.key")->at($this->root)->setContent("def");
vfsStream::newFile("plugins/odoo-wp-plugin/odoo_conn.key")->at($this->root)->setContent("def");
$this->flock->expects($this->exactly(1))->willReturnCallback(
function ($encryption_file, $lock_type, &$would_block_lock = false) {
$would_block_lock = false;
Expand All @@ -34,7 +39,7 @@ function ($encryption_file, $lock_type, &$would_block_lock = false) {

$this->file_handler->write_to_file("abc");

$this->assertEquals("abc", $this->root->getChild("odoo_conn.key")->getContent());
$this->assertEquals("abc", $this->root->getChild("plugins/odoo-wp-plugin/odoo_conn.key")->getContent());
}

public function test_timeout()
Expand Down
59 changes: 59 additions & 0 deletions tests/php/loaded/move_existing_encryption_file_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace php\loaded;
require_once __DIR__ . "/../TestClassBrainMonkey.php";

use Brain\Monkey\Functions;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use org\bovigo\vfs\vfsStream;
use TestClassBrainMonkey;

class move_existing_encryption_file_test extends TestClassBrainMonkey
{

use MockeryPHPUnitIntegration;

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

if (!defined("ABSPATH")) {
define("ABSPATH", "vfs://root/");
}
Functions\when("plugin_dir_path")->justReturn("vfs://root/plugins/odoo-wp-plugin/");

require_once __DIR__ . "/../../../loaded.php";

$this->root = vfsStream::setup("root", 0777);
vfsStream::newDirectory("/plugins/odoo-wp-plugin/")->at($this->root);
}

function test_key_file_moved()
{
vfsStream::newFile("odoo_conn.key")->at($this->root)->setContent("key");

move_existing_encryption_file();

$this->assertTrue(file_exists("vfs://root/plugins/odoo-wp-plugin/odoo_conn.key"));
$this->assertFalse(file_exists("vfs://root/odoo_conn.key"));
$this->assertEquals(
"key", $this->root->getChild("plugins/odoo-wp-plugin/odoo_conn.key")->getContent()
);
}

function test_old_key_file_does_not_exist()
{
vfsStream::newFile("plugins/odoo-wp-plugin/odoo_conn.key")->at($this->root)->setContent("key");

move_existing_encryption_file();

$this->assertTrue(file_exists("vfs://root/plugins/odoo-wp-plugin/odoo_conn.key"));
$this->assertFalse(file_exists("vfs://root/odoo_conn.key"));
$this->assertEquals(
"key", $this->root->getChild("plugins/odoo-wp-plugin/odoo_conn.key")->getContent()
);
}

}

?>

0 comments on commit c8427a3

Please sign in to comment.