FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
UploadPermissionDaoTest.php
1 <?php
2 /*
3 Copyright (C) 2015, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 namespace Fossology\Lib\Dao;
20 
24 use Mockery as M;
25 
26 require_once __DIR__.'/../../Plugin/FO_Plugin.php';
27 
28 class UploadPermissionDaoTest extends \PHPUnit\Framework\TestCase
29 {
31  private $testDb;
33  private $dbManager;
35  private $uploadPermissionDao;
36 
37  protected function setUp()
38  {
39  $this->testDb = new TestPgDb();
40  $this->dbManager = &$this->testDb->getDbManager();
41 
42  $this->testDb->createPlainTables(array('upload','uploadtree'));
43 
44  $this->dbManager->prepare($stmt = 'insert.upload',
45  "INSERT INTO upload (upload_pk, uploadtree_tablename) VALUES ($1, $2)");
46  $uploadArray = array(array(1, 'uploadtree'), array(2, 'uploadtree_a'));
47  foreach ($uploadArray as $uploadEntry) {
48  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
49  }
50  $logger = M::mock('Monolog\Logger'); // new Logger("UploadDaoTest");
51  $logger->shouldReceive('debug');
52  $this->uploadPermissionDao = new UploadPermissionDao($this->dbManager, $logger);
53 
54  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
55  }
56 
57  protected function tearDown()
58  {
59  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
60  $this->testDb = null;
61  $this->dbManager = null;
62  }
63 
64  public function testmakeAccessibleToGroup()
65  {
66  $this->testDb->createPlainTables(array('perm_upload','group_user_member'));
67  $userId = 501;
68  $groupId = 601;
69  $groupIdAlternative = 602;
70  $this->dbManager->insertTableRow('group_user_member', array('group_fk'=>$groupId,'user_fk'=>$userId,'group_perm'=>Auth::PERM_READ));
71  $this->dbManager->insertTableRow('group_user_member', array('group_fk'=>$groupIdAlternative,'user_fk'=>$userId,'group_perm'=>Auth::PERM_READ));
72 
73  $unaccessibleIsAccessible = $this->uploadPermissionDao->isAccessible($uploadId=1, $groupId);
74  assertThat($unaccessibleIsAccessible,equalTo(false));
75 
76  $this->uploadPermissionDao->makeAccessibleToGroup($uploadId, $groupId, Auth::PERM_WRITE);
77  $accessibleIsAccessible = $this->uploadPermissionDao->isAccessible($uploadId, $groupId);
78  assertThat($accessibleIsAccessible,equalTo(true));
79  $stillUnaccessibleIsAccessible = $this->uploadPermissionDao->isAccessible($uploadId, $groupIdAlternative);
80  assertThat($stillUnaccessibleIsAccessible,equalTo(false));
81 
82  $this->uploadPermissionDao->makeAccessibleToAllGroupsOf($uploadId, $userId);
83  $nowAccessibleIsAccessible = $this->uploadPermissionDao->isAccessible($uploadId, $groupIdAlternative);
84  assertThat($nowAccessibleIsAccessible,equalTo(true));
85  }
86 
87  public function testDeletePermissionId()
88  {
89  $this->testDb->createPlainTables(array('perm_upload'));
90  $this->testDb->insertData(array('perm_upload'));
91  $accessibleBefore = $this->uploadPermissionDao->isAccessible($uploadId=1, $groupId=2);
92  assertThat($accessibleBefore,equalTo(true));
93  $this->uploadPermissionDao->updatePermissionId(1,0);
94  $accessibleAfter = $this->uploadPermissionDao->isAccessible($uploadId, $groupId);
95  assertThat($accessibleAfter,equalTo(false));
96  }
97 
98  public function testUpdatePermissionId()
99  {
100  $this->testDb->createPlainTables(array('perm_upload'));
101  $this->testDb->insertData(array('perm_upload'));
102  $_SESSION[Auth::USER_LEVEL] = PLUGIN_DB_READ;
103  $adminBefore = $this->uploadPermissionDao->isEditable($uploadId=1, $groupId=2);
104  assertThat($adminBefore,equalTo(true));
105  $this->uploadPermissionDao->updatePermissionId(1,Auth::PERM_READ);
106  $adminNomore = $this->uploadPermissionDao->isEditable($uploadId, $groupId);
107  assertThat($adminNomore,equalTo(false));
108  $this->uploadPermissionDao->updatePermissionId(1,Auth::PERM_WRITE);
109  $adminAgain = $this->uploadPermissionDao->isEditable($uploadId, $groupId);
110  assertThat($adminAgain,equalTo(true));
111  }
112 
113  public function testInsertPermission()
114  {
115  $this->testDb->createPlainTables(array('perm_upload'));
116  $accessibleBefore = $this->uploadPermissionDao->isAccessible($uploadId=1, $groupId=2);
117  assertThat($accessibleBefore,equalTo(false));
118  $this->uploadPermissionDao->insertPermission($uploadId, $groupId, Auth::PERM_READ);
119  $accessibleAfter = $this->uploadPermissionDao->isAccessible($uploadId, $groupId);
120  assertThat($accessibleAfter,equalTo(true));
121  $this->uploadPermissionDao->insertPermission($uploadId, $groupId, Auth::PERM_NONE);
122  $accessibleNomore = $this->uploadPermissionDao->isAccessible($uploadId, $groupId);
123  assertThat($accessibleNomore,equalTo(false));
124  }
125 
126  public function testGetPublicPermission()
127  {
128  $this->testDb->insertData(array('upload'));
129  $perm = $this->uploadPermissionDao->getPublicPermission(3);
130  assertThat($perm,equalTo(0));
131  }
132 
133  public function testGetPermissionGroups()
134  {
135  $this->testDb->createPlainTables(array('perm_upload','groups'));
136  $this->testDb->insertData(array('perm_upload','groups'));
137  $permissionGroups = $this->uploadPermissionDao->getPermissionGroups(1);
138  assertThat($permissionGroups,is(array(2=>array('perm_upload_pk'=>1, 'perm'=>10, 'group_pk'=>2, 'group_name'=>'fossy'))));
139  }
140 
141  public function testAccessibilityViaNone()
142  {
143  $this->testDb->createPlainTables(array('perm_upload','groups'));
144  $this->testDb->insertData(array('groups'));
145  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_NONE;
146  $accessibilityWithBadGroup = $this->uploadPermissionDao->isAccessible($uploadId=2, $groupId=2);
147  assertThat($accessibilityWithBadGroup, equalTo(false));
148  }
149 
150  public function testAccessibilityViaGroup()
151  {
152  $this->testDb->createPlainTables(array('perm_upload','groups'));
153  $this->testDb->insertData(array('groups','perm_upload'));
154  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_NONE;
155  $accessibilityByGroup = $this->uploadPermissionDao->isAccessible($uploadId=2, $groupId=2);
156  assertThat($accessibilityByGroup, equalTo(true));
157  }
158 
159 
160  public function testAccessibilityViaPublicForUnqualifiedUser()
161  {
162  $this->testDb->createPlainTables(array('perm_upload','groups'));
163  $this->testDb->insertData(array('groups'));
164  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_NONE;
165  $accessibilityByNone = $this->uploadPermissionDao->isAccessible($uploadId=2, $groupId=2);
166  assertThat($accessibilityByNone, equalTo(false));
167 
168  $this->uploadPermissionDao->setPublicPermission($uploadId, Auth::PERM_READ);
169  $accessibilityByPublic = $this->uploadPermissionDao->isAccessible($uploadId, $groupId);
170  assertThat($accessibilityByPublic, equalTo(false));
171  }
172 
173  public function testAccessibilityViaPublicForQualifiedUser()
174  {
175  $this->testDb->createPlainTables(array('perm_upload','groups'));
176  $this->testDb->insertData(array('groups'));
177  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_READ;
178  $accessibilityByNone = $this->uploadPermissionDao->isAccessible($uploadId=2, $groupId=2);
179  assertThat($accessibilityByNone, equalTo(false));
180 
181  $this->uploadPermissionDao->setPublicPermission($uploadId, Auth::PERM_READ);
182  $accessibilityByPublic = $this->uploadPermissionDao->isAccessible($uploadId, $groupId);
183  assertThat($accessibilityByPublic, equalTo(true));
184  }
185 }
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:49
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28