Blame view

sources/apps/files_encryption/tests/helper.php 3.63 KB
31b7f2792   Kload   Upgrade to ownclo...
1
2
3
4
5
6
7
8
9
10
  <?php
  /**
   * Copyright (c) 2013 Bjoern Schiessle <schiessle@owncloud.com>
   * This file is licensed under the Affero General Public License version 3 or
   * later.
   * See the COPYING-README file.
   */
  
  
  require_once __DIR__ . '/../lib/helper.php';
a293d369c   Kload   Update sources to...
11
  require_once __DIR__ . '/util.php';
31b7f2792   Kload   Upgrade to ownclo...
12
13
14
15
16
17
18
  
  use OCA\Encryption;
  
  /**
   * Class Test_Encryption_Helper
   */
  class Test_Encryption_Helper extends \PHPUnit_Framework_TestCase {
a293d369c   Kload   Update sources to...
19
  	const TEST_ENCRYPTION_HELPER_USER1 = "test-helper-user1";
6d9380f96   Cédric Dupont   Update sources OC...
20
  	const TEST_ENCRYPTION_HELPER_USER2 = "test-helper-user2";
a293d369c   Kload   Update sources to...
21
22
23
  
  	public static function setUpBeforeClass() {
  		// create test user
6d9380f96   Cédric Dupont   Update sources OC...
24
  		\Test_Encryption_Util::loginHelper(\Test_Encryption_Helper::TEST_ENCRYPTION_HELPER_USER2, true);
a293d369c   Kload   Update sources to...
25
26
27
28
29
30
  		\Test_Encryption_Util::loginHelper(\Test_Encryption_Helper::TEST_ENCRYPTION_HELPER_USER1, true);
  	}
  
  	public static function tearDownAfterClass() {
  		// cleanup test user
  		\OC_User::deleteUser(\Test_Encryption_Helper::TEST_ENCRYPTION_HELPER_USER1);
6d9380f96   Cédric Dupont   Update sources OC...
31
32
33
  		\OC_User::deleteUser(\Test_Encryption_Helper::TEST_ENCRYPTION_HELPER_USER2);
  		\OC_Hook::clear();
  		\OC_FileProxy::clearProxies();
a293d369c   Kload   Update sources to...
34
  	}
31b7f2792   Kload   Upgrade to ownclo...
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  	/**
  	 * @medium
  	 */
  	function testStripPartialFileExtension() {
  
  		$partFilename = 'testfile.txt.part';
  		$filename = 'testfile.txt';
  
  		$this->assertTrue(Encryption\Helper::isPartialFilePath($partFilename));
  
  		$this->assertEquals('testfile.txt', Encryption\Helper::stripPartialFileExtension($partFilename));
  
  		$this->assertFalse(Encryption\Helper::isPartialFilePath($filename));
  
  		$this->assertEquals('testfile.txt', Encryption\Helper::stripPartialFileExtension($filename));
  	}
  
  
  	/**
  	 * @medium
  	 */
  	function testStripPartialFileExtensionWithTransferIdPath() {
  
  		$partFilename = 'testfile.txt.ocTransferId643653835.part';
  		$filename = 'testfile.txt';
  
  		$this->assertTrue(Encryption\Helper::isPartialFilePath($partFilename));
  
  		$this->assertEquals('testfile.txt', Encryption\Helper::stripPartialFileExtension($partFilename));
  
  		$this->assertFalse(Encryption\Helper::isPartialFilePath($filename));
  
  		$this->assertEquals('testfile.txt', Encryption\Helper::stripPartialFileExtension($filename));
  	}
  
  	function testGetPathToRealFile() {
  
  		// the relative path to /user/files/ that's what we want to get from getPathToRealFile()
  		$relativePath = "foo/bar/test.txt";
  
  		// test paths
  		$versionPath = "/user/files_versions/foo/bar/test.txt.v456756835";
  		$cachePath = "/user/cache/transferid636483/foo/bar/test.txt";
  
  		$this->assertEquals($relativePath, Encryption\Helper::getPathToRealFile($versionPath));
  		$this->assertEquals($relativePath, Encryption\Helper::getPathToRealFile($cachePath));
  	}
a293d369c   Kload   Update sources to...
82
83
84
85
  	function testGetUser() {
  
  		$path1 = "/" . self::TEST_ENCRYPTION_HELPER_USER1 . "/files/foo/bar.txt";
  		$path2 = "/" . self::TEST_ENCRYPTION_HELPER_USER1 . "/cache/foo/bar.txt";
6d9380f96   Cédric Dupont   Update sources OC...
86
  		$path3 = "/" . self::TEST_ENCRYPTION_HELPER_USER2 . "/thumbnails/foo";
a293d369c   Kload   Update sources to...
87
  		$path4 ="/" . "/" . self::TEST_ENCRYPTION_HELPER_USER1;
6d9380f96   Cédric Dupont   Update sources OC...
88
  		\Test_Encryption_Util::loginHelper(self::TEST_ENCRYPTION_HELPER_USER1);
a293d369c   Kload   Update sources to...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  		// if we are logged-in every path should return the currently logged-in user
  		$this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Encryption\Helper::getUser($path3));
  
  		// now log out
  		\Test_Encryption_Util::logoutHelper();
  
  		// now we should only get the user from /user/files and user/cache paths
  		$this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Encryption\Helper::getUser($path1));
  		$this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Encryption\Helper::getUser($path2));
  
  		$this->assertFalse(Encryption\Helper::getUser($path3));
  		$this->assertFalse(Encryption\Helper::getUser($path4));
  
  		// Log-in again
  		\Test_Encryption_Util::loginHelper(\Test_Encryption_Helper::TEST_ENCRYPTION_HELPER_USER1);
  	}
31b7f2792   Kload   Upgrade to ownclo...
105
  }