summaryrefslogtreecommitdiffstats
path: root/integrations
diff options
context:
space:
mode:
authorguillep2k <18600385+guillep2k@users.noreply.github.com>2020-01-20 12:45:14 -0300
committerAntoine GIRARD <sapk@users.noreply.github.com>2020-01-20 16:45:14 +0100
commitad1b6d439fe0e0875e54227e0bc23a74411f490e (patch)
tree8e16e25a913d167ebb772ad2e0e92cbba9c56b66 /integrations
parent6d6f1d568ec36786b1020f4b43cbd872228c6633 (diff)
downloadgitea-ad1b6d439fe0e0875e54227e0bc23a74411f490e.tar.gz
gitea-ad1b6d439fe0e0875e54227e0bc23a74411f490e.zip
Add support for database schema in PostgreSQL (#8819)
* Add support for database schema * Require setting search_path for the db user * Add schema setting to admin/config.tmpl * Use a schema different from default for psql tests * Update postgres scripts to use custom schema * Update to xorm/core 0.7.3 and xorm/xorm c37aff9b3a * Fix migration test Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'integrations')
-rw-r--r--integrations/integration_test.go45
-rw-r--r--integrations/migration-test/gitea-v1.5.3.postgres.sql.gzbin16612 -> 16783 bytes
-rw-r--r--integrations/migration-test/gitea-v1.6.4.postgres.sql.gzbin17346 -> 17517 bytes
-rw-r--r--integrations/migration-test/gitea-v1.7.0.postgres.sql.gzbin17659 -> 17831 bytes
-rw-r--r--integrations/migration-test/migration_test.go26
-rw-r--r--integrations/pgsql.ini.tmpl1
6 files changed, 67 insertions, 5 deletions
diff --git a/integrations/integration_test.go b/integrations/integration_test.go
index bf363f3b4d..138d751859 100644
--- a/integrations/integration_test.go
+++ b/integrations/integration_test.go
@@ -153,18 +153,53 @@ func initIntegrationTest() {
if err != nil {
log.Fatalf("sql.Open: %v", err)
}
- rows, err := db.Query(fmt.Sprintf("SELECT 1 FROM pg_database WHERE datname = '%s'", setting.Database.Name))
+ dbrows, err := db.Query(fmt.Sprintf("SELECT 1 FROM pg_database WHERE datname = '%s'", setting.Database.Name))
if err != nil {
log.Fatalf("db.Query: %v", err)
}
- defer rows.Close()
+ defer dbrows.Close()
- if rows.Next() {
+ if !dbrows.Next() {
+ if _, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s", setting.Database.Name)); err != nil {
+ log.Fatalf("db.Exec: CREATE DATABASE: %v", err)
+ }
+ }
+ // Check if we need to setup a specific schema
+ if len(setting.Database.Schema) == 0 {
break
}
- if _, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s", setting.Database.Name)); err != nil {
- log.Fatalf("db.Exec: %v", err)
+ db.Close()
+
+ db, err = sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s",
+ setting.Database.User, setting.Database.Passwd, setting.Database.Host, setting.Database.Name, setting.Database.SSLMode))
+ // This is a different db object; requires a different Close()
+ defer db.Close()
+ if err != nil {
+ log.Fatalf("sql.Open: %v", err)
+ }
+ schrows, err := db.Query(fmt.Sprintf("SELECT 1 FROM information_schema.schemata WHERE schema_name = '%s'", setting.Database.Schema))
+ if err != nil {
+ log.Fatalf("db.Query: %v", err)
+ }
+ defer schrows.Close()
+
+ if !schrows.Next() {
+ // Create and setup a DB schema
+ if _, err = db.Exec(fmt.Sprintf("CREATE SCHEMA %s", setting.Database.Schema)); err != nil {
+ log.Fatalf("db.Exec: CREATE SCHEMA: %v", err)
+ }
+ }
+
+ // Make the user's default search path the created schema; this will affect new connections
+ if _, err = db.Exec(fmt.Sprintf(`ALTER USER "%s" SET search_path = %s`, setting.Database.User, setting.Database.Schema)); err != nil {
+ log.Fatalf("db.Exec: ALTER USER SET search_path: %v", err)
+ }
+
+ // Make the current connection's search the created schema
+ if _, err = db.Exec(fmt.Sprintf(`SET search_path = %s`, setting.Database.Schema)); err != nil {
+ log.Fatalf("db.Exec: ALTER USER SET search_path: %v", err)
}
+
case setting.Database.UseMSSQL:
host, port := setting.ParseMSSQLHostPort(setting.Database.Host)
db, err := sql.Open("mssql", fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;",
diff --git a/integrations/migration-test/gitea-v1.5.3.postgres.sql.gz b/integrations/migration-test/gitea-v1.5.3.postgres.sql.gz
index 2fcad82111..2380f8dd7f 100644
--- a/integrations/migration-test/gitea-v1.5.3.postgres.sql.gz
+++ b/integrations/migration-test/gitea-v1.5.3.postgres.sql.gz
Binary files differ
diff --git a/integrations/migration-test/gitea-v1.6.4.postgres.sql.gz b/integrations/migration-test/gitea-v1.6.4.postgres.sql.gz
index 8db9022d1c..bd66f6ba4f 100644
--- a/integrations/migration-test/gitea-v1.6.4.postgres.sql.gz
+++ b/integrations/migration-test/gitea-v1.6.4.postgres.sql.gz
Binary files differ
diff --git a/integrations/migration-test/gitea-v1.7.0.postgres.sql.gz b/integrations/migration-test/gitea-v1.7.0.postgres.sql.gz
index ed66d41b89..e4716c6b43 100644
--- a/integrations/migration-test/gitea-v1.7.0.postgres.sql.gz
+++ b/integrations/migration-test/gitea-v1.7.0.postgres.sql.gz
Binary files differ
diff --git a/integrations/migration-test/migration_test.go b/integrations/migration-test/migration_test.go
index c274d482da..6cdfbf7d7d 100644
--- a/integrations/migration-test/migration_test.go
+++ b/integrations/migration-test/migration_test.go
@@ -168,6 +168,32 @@ func restoreOldDB(t *testing.T, version string) bool {
assert.NoError(t, err)
db.Close()
+ // Check if we need to setup a specific schema
+ if len(setting.Database.Schema) != 0 {
+ db, err = sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s",
+ setting.Database.User, setting.Database.Passwd, setting.Database.Host, setting.Database.Name, setting.Database.SSLMode))
+ if !assert.NoError(t, err) {
+ return false
+ }
+ schrows, err := db.Query(fmt.Sprintf("SELECT 1 FROM information_schema.schemata WHERE schema_name = '%s'", setting.Database.Schema))
+ if !assert.NoError(t, err) || !assert.NotEmpty(t, schrows) {
+ return false
+ }
+
+ if !schrows.Next() {
+ // Create and setup a DB schema
+ _, err = db.Exec(fmt.Sprintf("CREATE SCHEMA %s", setting.Database.Schema))
+ assert.NoError(t, err)
+ }
+ schrows.Close()
+
+ // Make the user's default search path the created schema; this will affect new connections
+ _, err = db.Exec(fmt.Sprintf(`ALTER USER "%s" SET search_path = %s`, setting.Database.User, setting.Database.Schema))
+ assert.NoError(t, err)
+
+ db.Close()
+ }
+
db, err = sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s",
setting.Database.User, setting.Database.Passwd, setting.Database.Host, setting.Database.Name, setting.Database.SSLMode))
assert.NoError(t, err)
diff --git a/integrations/pgsql.ini.tmpl b/integrations/pgsql.ini.tmpl
index 6265e0d98e..f337d98fb4 100644
--- a/integrations/pgsql.ini.tmpl
+++ b/integrations/pgsql.ini.tmpl
@@ -7,6 +7,7 @@ HOST = {{TEST_PGSQL_HOST}}
NAME = {{TEST_PGSQL_DBNAME}}
USER = {{TEST_PGSQL_USERNAME}}
PASSWD = {{TEST_PGSQL_PASSWORD}}
+SCHEMA = {{TEST_PGSQL_SCHEMA}}
SSL_MODE = disable
[indexer]
>backport/47933/stable29 Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
summaryrefslogtreecommitdiffstats
path: root/tests/lib/Settings/ManagerTest.php
blob: 6f9af39d5919117484b6c4f515f3ed054a884036 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/**
 * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
 *
 * @author Lukas Reschke <lukas@statuscode.ch>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace Tests\Settings;

use OC\Accounts\AccountManager;
use OC\Settings\Admin\Sharing;
use OC\Settings\Manager;
use OC\Settings\Mapper;
use OC\Settings\Personal\Security;
use OC\Settings\Section;
use OCP\App\IAppManager;
use OCP\Encryption\IManager;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\Lock\ILockingProvider;
use Test\TestCase;

class ManagerTest extends TestCase {
	/** @var Manager|\PHPUnit_Framework_MockObject_MockObject */
	private $manager;
	/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
	private $logger;
	/** @var IDBConnection|\PHPUnit_Framework_MockObject_MockObject */
	private $dbConnection;
	/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
	private $l10n;
	/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
	private $config;
	/** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
	private $encryptionManager;
	/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
	private $userManager;
	/** @var ILockingProvider|\PHPUnit_Framework_MockObject_MockObject */
	private $lockingProvider;
	/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
	private $request;
	/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
	private $url;
	/** @var AccountManager|\PHPUnit_Framework_MockObject_MockObject */
	private $accountManager;
	/** @var  IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
	private $groupManager;
	/** @var  IFactory|\PHPUnit_Framework_MockObject_MockObject */
	private $l10nFactory;
	/** @var IAppManager */
	private $appManager;

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

		$this->logger = $this->createMock(ILogger::class);
		$this->dbConnection = $this->createMock(IDBConnection::class);
		$this->l10n = $this->createMock(IL10N::class);
		$this->config = $this->createMock(IConfig::class);
		$this->encryptionManager = $this->createMock(IManager::class);
		$this->userManager = $this->createMock(IUserManager::class);
		$this->lockingProvider = $this->createMock(ILockingProvider::class);
		$this->request = $this->createMock(IRequest::class);
		$this->url = $this->createMock(IURLGenerator::class);
		$this->accountManager = $this->createMock(AccountManager::class);
		$this->groupManager = $this->createMock(IGroupManager::class);
		$this->l10nFactory = $this->createMock(IFactory::class);
		$this->appManager = $this->createMock(IAppManager::class);

		$this->manager = new Manager(
			$this->logger,
			$this->dbConnection,
			$this->l10n,
			$this->config,
			$this->encryptionManager,
			$this->userManager,
			$this->lockingProvider,
			$this->request,
			$this->url,
			$this->accountManager,
			$this->groupManager,
			$this->l10nFactory,
			$this->appManager
		);
	}

	public function testGetAdminSections() {
		$this->l10n
			->expects($this->any())
			->method('t')
			->will($this->returnArgument(0));

		$this->manager->registerSection('admin', \OCA\WorkflowEngine\Settings\Section::class);

		$this->url->expects($this->exactly(6))
			->method('imagePath')
			->willReturnMap([
				['settings', 'admin.svg', '0'],
				['core', 'actions/settings-dark.svg', '1'],
				['core', 'actions/share.svg', '2'],
				['core', 'actions/password.svg', '3'],
				['core', 'places/contacts.svg', '5'],
				['settings', 'help.svg', '4'],
			]);

		$this->assertEquals([
			0 => [new Section('overview', 'Overview', 0, '0')],
			1 => [new Section('server', 'Basic settings', 0, '1')],
			5 => [new Section('sharing', 'Sharing', 0, '2')],
			10 => [new Section('security', 'Security', 0, '3')],
			50 => [new Section('groupware', 'Groupware', 0, '5')],
			55 => [\OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class)],
			98 => [new Section('additional', 'Additional settings', 0, '1')],
		], $this->manager->getAdminSections());
	}

	public function testGetPersonalSections() {
		$this->l10n
			->expects($this->any())
			->method('t')
			->will($this->returnArgument(0));

		$this->manager->registerSection('personal', \OCA\WorkflowEngine\Settings\Section::class);

		$this->url->expects($this->exactly(3))
			->method('imagePath')
			->willReturnMap([
				['core', 'actions/info.svg', '1'],
				['settings', 'password.svg', '2'],
				['core', 'clients/phone.svg', '3'],
			]);

		$this->assertEquals([
			0 => [new Section('personal-info', 'Personal info', 0, '1')],
			5 => [new Section('security', 'Security', 0, '2')],
			15 => [new Section('sync-clients', 'Mobile & desktop', 0, '3')],
			55 => [\OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class)],
		], $this->manager->getPersonalSections());
	}

	public function testGetAdminSectionsEmptySection() {
		$this->l10n
			->expects($this->any())
			->method('t')
			->will($this->returnArgument(0));

		$this->url->expects($this->exactly(6))
			->method('imagePath')
			->willReturnMap([
				['settings', 'admin.svg', '0'],
				['core', 'actions/settings-dark.svg', '1'],
				['core', 'actions/share.svg', '2'],
				['core', 'actions/password.svg', '3'],
				['core', 'places/contacts.svg', '5'],
				['settings', 'help.svg', '4'],
			]);

		$this->assertEquals([
			0 => [new Section('overview', 'Overview', 0, '0')],
			1 => [new Section('server', 'Basic settings', 0, '1')],
			5 => [new Section('sharing', 'Sharing', 0, '2')],
			10 => [new Section('security', 'Security', 0, '3')],
			50 => [new Section('groupware', 'Groupware', 0, '5')],
			98 => [new Section('additional', 'Additional settings', 0, '1')],
		], $this->manager->getAdminSections());
	}

	public function testGetPersonalSectionsEmptySection() {
		$this->l10n
			->expects($this->any())
			->method('t')
			->will($this->returnArgument(0));

		$this->url->expects($this->exactly(3))
			->method('imagePath')
			->willReturnMap([
				['core', 'actions/info.svg', '1'],
				['settings', 'password.svg', '2'],
				['core', 'clients/phone.svg', '3'],
			]);

		$this->assertArraySubset([
			0 => [new Section('personal-info', 'Personal info', 0, '1')],
			5 => [new Section('security', 'Security', 0, '2')],
			15 => [new Section('sync-clients', 'Mobile & desktop', 0, '3')],
		], $this->manager->getPersonalSections());
	}

	public function testGetAdminSettings() {
		$this->assertEquals([
			0 => [new Sharing($this->config, $this->l10n)],
		], $this->manager->getAdminSettings('sharing'));
	}

	public function testGetPersonalSettings() {
		$this->assertEquals([
			10 => [new Security($this->userManager)],
		], $this->manager->getPersonalSettings('security'));
	}

	public function testSameSectionAsPersonalAndAdmin() {
		$this->l10n
			->expects($this->any())
			->method('t')
			->will($this->returnArgument(0));

		$this->manager->registerSection('personal', \OCA\WorkflowEngine\Settings\Section::class);
		$this->manager->registerSection('admin', \OCA\WorkflowEngine\Settings\Section::class);

		$this->url->expects($this->exactly(9))
			->method('imagePath')
			->willReturnMap([
				['core', 'actions/info.svg', '1'],
				['settings', 'password.svg', '2'],
				['core', 'clients/phone.svg', '3'],
				['settings', 'admin.svg', '0'],
				['core', 'actions/settings-dark.svg', '1'],
				['core', 'actions/share.svg', '2'],
				['core', 'actions/password.svg', '3'],
				['core', 'places/contacts.svg', '5'],
				['settings', 'help.svg', '4'],
			]);

		$this->assertEquals([
			0 => [new Section('personal-info', 'Personal info', 0, '1')],
			5 => [new Section('security', 'Security', 0, '2')],
			15 => [new Section('sync-clients', 'Mobile & desktop', 0, '3')],
			55 => [\OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class)],
		], $this->manager->getPersonalSections());

		$this->assertEquals([
			0 => [new Section('overview', 'Overview', 0, '0')],
			1 => [new Section('server', 'Basic settings', 0, '1')],
			5 => [new Section('sharing', 'Sharing', 0, '2')],
			10 => [new Section('security', 'Security', 0, '3')],
			50 => [new Section('groupware', 'Groupware', 0, '5')],
			55 => [\OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class)],
			98 => [new Section('additional', 'Additional settings', 0, '1')],
		], $this->manager->getAdminSections());
	}
}