Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/gallery.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlivier Paroz <github@oparoz.com>2015-08-21 01:06:33 +0300
committerOlivier Paroz <github@oparoz.com>2015-08-21 01:06:33 +0300
commit0aba55017386a6e56673a62bedcaf88cdadf3e59 (patch)
treed94a6a8e6800b3122b71126094d3ebc072b589e3 /tests/unit
parent7354cf79f1c5cef9a4beee9990358d3c45de98f7 (diff)
Some EnvCheckMiddleware path tests
I've had to add our test setup creator to the unit testing suite because most filesystems methods don't work when using the Node API via the AppFramework
Diffstat (limited to 'tests/unit')
-rw-r--r--tests/unit/middleware/EnvCheckMiddlewareTest.php159
-rw-r--r--tests/unit/middleware/SharingCheckMiddlewareTest.php37
2 files changed, 187 insertions, 9 deletions
diff --git a/tests/unit/middleware/EnvCheckMiddlewareTest.php b/tests/unit/middleware/EnvCheckMiddlewareTest.php
index 51610636..82af3b1c 100644
--- a/tests/unit/middleware/EnvCheckMiddlewareTest.php
+++ b/tests/unit/middleware/EnvCheckMiddlewareTest.php
@@ -13,6 +13,7 @@
namespace OCA\Gallery\Middleware;
use OC\AppFramework\Utility\ControllerMethodReflector;
+use Helper\CoreTestCase;
use OCP\IRequest;
use OCP\Security\IHasher;
@@ -29,11 +30,15 @@ use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCA\Gallery\Environment\Environment;
+use OCA\Gallery\Environment\EnvironmentException;
/**
* @package OCA\Gallery\Middleware\EnvCheckMiddlewareTest
*/
-class EnvCheckMiddlewareTest extends \Test\TestCase {
+class EnvCheckMiddlewareTest extends \Codeception\TestCase\Test {
+
+ /** @var CoreTestCase */
+ private $coreTestCase;
/** @var string */
private $appName = 'gallery';
@@ -56,6 +61,11 @@ class EnvCheckMiddlewareTest extends \Test\TestCase {
/** @var SharingCheckMiddleware */
private $middleware;
+ /** @var string */
+ public $sharedFolderToken;
+ /** @var string */
+ public $passwordForFolderShare;
+
/**
* Test set up
*/
@@ -96,6 +106,93 @@ class EnvCheckMiddlewareTest extends \Test\TestCase {
$this->urlGenerator,
$this->logger
);
+
+ /**
+ * Injects objects we need to bypass the static methods
+ *
+ * CODECEPTION SPECIFIC
+ */
+ $setupData = $this->getModule('\Helper\DataSetup');
+ $this->sharedFolderToken = $setupData->sharedFolderToken;
+ $this->passwordForFolderShare = $setupData->passwordForFolderShare;
+ $this->coreTestCase = $setupData->coreTestCase;
+ }
+
+ /**
+ * Invokes private methods
+ *
+ * CODECEPTION SPECIFIC
+ * This is from the core TestCase
+ *
+ * @param $object
+ * @param $methodName
+ * @param array $parameters
+ *
+ * @return mixed
+ */
+ public function invokePrivate($object, $methodName, array $parameters = []) {
+ return $this->coreTestCase->invokePrivate($object, $methodName, $parameters);
+ }
+
+
+ /**
+ * @todo Mock an environment response
+ */
+ public function testBeforeControllerWithoutNotation() {
+ $this->reflector->reflect(__CLASS__, __FUNCTION__);
+ $this->middleware->beforeController(__CLASS__, __FUNCTION__);
+ }
+
+ /**
+ * @PublicPage
+ *
+ * @expectedException \OCA\Gallery\Middleware\CheckException
+ */
+ public function testBeforeControllerWithPublicNotationAndInvalidToken() {
+ $this->reflector->reflect(__CLASS__, __FUNCTION__);
+
+ $token = 'aaaabbbbccccdddd';
+ $this->mockGetTokenParam($token);
+
+ $this->middleware->beforeController(__CLASS__, __FUNCTION__);
+ }
+
+ /**
+ * @PublicPage
+ *
+ * Because the method tested is static, we need to load our test environment \Helper\DataSetup
+ */
+ public function testBeforeControllerWithPublicNotationAndToken() {
+ $this->reflector->reflect(__CLASS__, __FUNCTION__);
+
+ $this->mockGetTokenAndPasswordParams($this->sharedFolderToken, $this->passwordForFolderShare);
+ $linkItem = Share::getShareByToken($this->sharedFolderToken, false);
+
+ $this->mockHasherVerify($this->passwordForFolderShare, $linkItem['share_with'], true);
+
+ $this->middleware->beforeController(__CLASS__, __FUNCTION__);
+ }
+
+ /**
+ * @PublicPage
+ *
+ * @expectedException \OCA\Gallery\Middleware\CheckException
+ */
+ public function testBeforeControllerWithPublicNotationAndNoToken() {
+ $this->reflector->reflect(__CLASS__, __FUNCTION__);
+
+ $token = null;
+ $this->mockGetTokenParam($token);
+ $this->middleware->beforeController(__CLASS__, __FUNCTION__);
+ }
+
+ /**
+ * @@Guest
+ */
+ public function testBeforeControllerWithGuestNotation() {
+ $this->reflector->reflect(__CLASS__, __FUNCTION__);
+
+ $this->middleware->beforeController(__CLASS__, __FUNCTION__);
}
public function testCheckSessionAfterPasswordEntry() {
@@ -187,6 +284,20 @@ class EnvCheckMiddlewareTest extends \Test\TestCase {
self::invokePrivate($this->middleware, 'authenticate', [$linkItem, $password]);
}
+ /**
+ * @expectedException \OCA\Gallery\Middleware\CheckException
+ */
+ public function testAuthenticateWithWrongLinkType() {
+ $password = 'Je suis une pipe';
+ $linkItem = [
+ 'id' => 12345,
+ 'share_with' => 'tester',
+ 'share_type' => Share::SHARE_TYPE_USER
+ ];
+
+ self::invokePrivate($this->middleware, 'authenticate', [$linkItem, $password]);
+ }
+
public function testCheckAuthorisationAfterValidPasswordEntry() {
$password = 'Je suis une pipe';
$linkItem = [
@@ -373,12 +484,25 @@ class EnvCheckMiddlewareTest extends \Test\TestCase {
$template = $this->mockJsonResponse($message, $code);
$response =
- $this->middleware->afterException($this->controller, 'checkLinkItemIsValid', $exception);
+ $this->middleware->afterException(
+ $this->controller, 'checkLinkItemIsValid', $exception
+ );
$this->assertEquals($template, $response);
}
/**
+ * @expectedException \OCA\Gallery\Environment\EnvironmentException
+ */
+ public function testAfterExceptionWithNonCheckException() {
+ $message = 'fail';
+ $code = Http::STATUS_NOT_FOUND;
+ $exception = new EnvironmentException($message, $code);
+
+ $this->middleware->afterException($this->controller, 'checkLinkItemIsValid', $exception);
+ }
+
+ /**
* Mocks ISession->exists('public_link_authenticated')
*
* @param int $linkItemId
@@ -403,8 +527,8 @@ class EnvCheckMiddlewareTest extends \Test\TestCase {
}
/**
- * @param string $givenPassword
- * @param string $tokenPassword
+ * @param string $givenPassword clear text password
+ * @param string $tokenPassword encrypted password
* @param bool $valid
*/
private function mockHasherVerify($givenPassword, $tokenPassword, $valid) {
@@ -484,4 +608,31 @@ class EnvCheckMiddlewareTest extends \Test\TestCase {
->willReturn($url);
}
+ /**
+ * Mocks IRequest->getParam()
+ *
+ * @param $token
+ * @param $password
+ */
+ private function mockGetTokenAndPasswordParams($token, $password = null) {
+ $this->request->expects($this->at(0))
+ ->method('getParam')
+ ->with('token')
+ ->willReturn($token);
+ $this->request->expects($this->at(1))
+ ->method('getParam')
+ ->with('password')
+ ->willReturn($password);
+ }
+
+ /**
+ * Mocks IRequest->getParam('token')
+ */
+ private function mockGetTokenParam($token) {
+ $this->request->expects($this->any())
+ ->method('getParam')
+ ->with('token')
+ ->willReturn($token);
+ }
+
}
diff --git a/tests/unit/middleware/SharingCheckMiddlewareTest.php b/tests/unit/middleware/SharingCheckMiddlewareTest.php
index e8689ea4..ff3b31b3 100644
--- a/tests/unit/middleware/SharingCheckMiddlewareTest.php
+++ b/tests/unit/middleware/SharingCheckMiddlewareTest.php
@@ -98,10 +98,10 @@ class SharingCheckMiddlewareTest extends \Test\TestCase {
}
/**
- * @PublicPage
* @Guest
+ * @PublicPage
*
- * Contains both notations and sharing is always enabled, so should not throw any exception
+ * Guest pages don't need to be checked
*/
public function testBeforeControllerWithGuestNotation() {
$this->reflector->reflect(__CLASS__, __FUNCTION__);
@@ -111,11 +111,34 @@ class SharingCheckMiddlewareTest extends \Test\TestCase {
/**
* @PublicPage
*
- * The special Guest notation is missing
+ * Contains PublicPage notation, does not contain Guest notation and sharing is enabled, so
+ * should not throw any exception
+ */
+ public function testBeforeControllerWithAllRequirements() {
+ $this->mockSharingConfigTo('yes');
+ $this->reflector->reflect(__CLASS__, __FUNCTION__);
+ $this->middleware->beforeController(__CLASS__, __FUNCTION__);
+ }
+
+ /**
+ * Non-public pages don't need to be checked
+ */
+ public function testBeforeControllerWithoutPublicPageNotation() {
+ $this->mockSharingConfigTo('yes');
+
+ $this->reflector->reflect(__CLASS__, __FUNCTION__);
+ $this->middleware->beforeController(__CLASS__, __FUNCTION__);
+ }
+
+ /**
+ * @PublicPage
+ *
+ * Sharing needs to be enabled on public pages
*
* @expectedException \OCA\Gallery\Middleware\CheckException
*/
- public function testBeforeControllerWithoutGuestNotation() {
+ public function testBeforeControllerWithSharingDisabled() {
+ $this->mockSharingConfigTo('no');
$this->reflector->reflect(__CLASS__, __FUNCTION__);
$this->middleware->beforeController(__CLASS__, __FUNCTION__);
}
@@ -128,7 +151,11 @@ class SharingCheckMiddlewareTest extends \Test\TestCase {
private function mockSharingConfigTo($state) {
$this->config->expects($this->once())
->method('getAppValue')
- ->with('core', 'shareapi_allow_links', 'yes')
+ ->with(
+ 'core',
+ 'shareapi_allow_links',
+ 'yes'
+ )
->willReturn($state);
}