1
1
package fr .brouillard .gitbucket .h2 .controller
2
2
3
+ import gitbucket .core .controller .Context
3
4
import gitbucket .core .model .Account
4
5
import gitbucket .core .servlet .ApiAuthenticationFilter
5
6
import org .apache .commons .io .FileSystemUtils
6
7
import org .h2 .Driver
7
8
import org .h2 .engine .Database
9
+ import org .mockito .Mockito ._
8
10
import org .scalatest .funsuite .AnyFunSuite
9
11
import org .scalatest .matchers .should .Matchers .{convertToAnyShouldWrapper , equal }
10
12
import org .scalatra .{Ok , Params , ScalatraParams }
@@ -15,30 +17,76 @@ import java.nio.file.{Files, Path, Paths}
15
17
import java .util .{Date , Properties }
16
18
import scala .util .Using
17
19
18
- class H2BackupControllerTests extends ScalatraFunSuite {
20
+ import H2BackupControllerTests ._
21
+ import gitbucket .core .service .SystemSettingsService
22
+
23
+ class H2BackupControllerWithAdminTests extends ScalatraFunSuite {
19
24
addFilter(classOf [ApiAuthenticationFilter ], path= " /api/*" )
20
- addFilter(classOf [H2BackupController ], " /*" )
25
+ addFilter(new H2BackupController () {
26
+ override implicit val context = buildContext(isAdmin = true )
27
+ }, " /*" )
21
28
22
- test(" get database backup api" ) {
29
+ test(" get database backup api with admin " ) {
23
30
get(" /api/v3/plugins/database/backup" ) {
24
31
status should equal (405 )
25
32
body should include (" This has moved" )
26
33
}
27
34
}
28
35
29
- test(" get database backup legacy" ) {
36
+ test(" get database backup legacy with admin " ) {
30
37
get(" /database/backup" ) {
31
38
status should equal (405 )
32
39
body should include (" This has moved" )
33
40
}
34
41
}
42
+ }
35
43
36
- test(" post database backup without credentials is unauthorized" ) {
44
+ class H2BackupControllerWithNonAdminTests extends ScalatraFunSuite {
45
+ addFilter(classOf [ApiAuthenticationFilter ], path= " /api/*" )
46
+ addFilter(new H2BackupController () {
47
+ override implicit val context = buildContext(isAdmin = false )
48
+ }, " /*" )
49
+
50
+ test(" get database backup api with non-admin" ) {
51
+ get(" /api/v3/plugins/database/backup" ) {
52
+ status should equal (401 )
53
+ }
54
+ }
55
+
56
+ test(" get database backup legacy with non-admin" ) {
57
+ get(" /database/backup" ) {
58
+ status should equal (401 )
59
+ }
60
+ }
61
+
62
+ test(" post database backup with non-admin" ) {
37
63
post(" /api/v3/plugins/database/backup" ) {
38
64
status should equal (401 )
39
65
}
40
66
}
67
+ }
68
+
69
+ class H2BackupControllerWithoutLoginTests extends ScalatraFunSuite {
70
+ addFilter(classOf [ApiAuthenticationFilter ], path= " /api/*" )
71
+ addFilter(classOf [H2BackupController ], " /*" )
72
+
73
+ test(" get database backup api without login" ) {
74
+ get(" /api/v3/plugins/database/backup" ) {
75
+ status should equal (401 )
76
+ }
77
+ }
41
78
79
+ test(" get database backup legacy without login" ) {
80
+ get(" /database/backup" ) {
81
+ status should equal (401 )
82
+ }
83
+ }
84
+
85
+ test(" post database backup without login" ) {
86
+ post(" /api/v3/plugins/database/backup" ) {
87
+ status should equal (401 )
88
+ }
89
+ }
42
90
}
43
91
44
92
class H2BackupControllerObjectTests extends AnyFunSuite {
@@ -47,23 +95,6 @@ class H2BackupControllerObjectTests extends AnyFunSuite {
47
95
assert(name.endsWith(" .zip" ))
48
96
}
49
97
50
- private def buildAccount (isAdmin : Boolean ) = {
51
- Account (
52
- userName = " a" ,
53
- fullName = " b" ,
54
- mailAddress = " c" ,
55
- password = " d" ,
56
- isAdmin = isAdmin,
57
- url = None ,
58
- registeredDate = new Date (),
59
- updatedDate = new Date (),
60
- lastLoginDate = None ,
61
- image = None ,
62
- isGroupAccount = false ,
63
- isRemoved = false ,
64
- description = None )
65
- }
66
-
67
98
private def h2Url (file : File ): String = {
68
99
" jdbc:h2:file:" + file + " ;DATABASE_TO_UPPER=false"
69
100
}
@@ -110,62 +141,34 @@ class H2BackupControllerObjectTests extends AnyFunSuite {
110
141
test(" generates default file name" ) {
111
142
assertDefaultFileName(H2BackupController .defaultBackupFileName())
112
143
}
144
+ }
113
145
114
- test(" post database backup with admin credentials is executed with default file name" ) {
115
- val account = buildAccount(true )
116
- val params : Params = new ScalatraParams (Map ())
117
-
118
- var executed = false ;
119
-
120
- val exportDatabase = (file : File ) => {
121
- assert(! executed)
122
- assertDefaultFileName(file.getName)
123
-
124
- executed = true
125
- }
126
-
127
- val action = H2BackupController .doBackup(exportDatabase, Some (account), params)
128
-
129
- assert(executed)
130
- assert(action.status == 200 )
131
-
132
- // Not JSON and not HTML
133
- assert(action.headers.get(" Content-Type" ).contains(" text/plain" ))
134
- }
135
-
136
- test(" post database backup with admin credentials is executed with specific file name" ) {
137
- val fileName = " foo.zip"
138
- val account = buildAccount(true )
139
- val params : Params = new ScalatraParams (Map (" dest" -> Seq (fileName)))
140
-
141
- var executed = false ;
142
-
143
- val exportDatabase = (file : File ) => {
144
- assert(! executed)
145
- assert(file.getName.equals(fileName))
146
-
147
- executed = true
148
- }
149
-
150
- val action = H2BackupController .doBackup(exportDatabase, Some (account), params)
151
-
152
- assert(executed)
153
- assert(action.status == 200 )
146
+ object H2BackupControllerTests {
147
+ val systemSetting = mock(classOf [SystemSettingsService .SystemSettings ])
148
+ when(systemSetting.sshAddress).thenReturn(None )
154
149
155
- // Not JSON and not HTML
156
- assert(action.headers.get(" Content-Type" ).contains(" text/plain" ))
150
+ def buildContext (isAdmin : Boolean ) = {
151
+ val context = mock(classOf [Context ])
152
+ when(context.baseUrl).thenReturn(" http://localhost:8080" )
153
+ when(context.loginAccount).thenReturn(Some (buildAccount(isAdmin)))
154
+ when(context.settings).thenReturn(systemSetting)
155
+ context
157
156
}
158
157
159
- test(" post database backup with unprivileged credentials is unauthorized" ) {
160
- val account = buildAccount(false )
161
- val params : Params = new ScalatraParams (Map ())
162
-
163
- val exportDatabase = (file : File ) => {
164
- fail()
165
- }
166
-
167
- val action = H2BackupController .doBackup(exportDatabase, Some (account), params)
168
- assert(action.status == 401 )
158
+ def buildAccount (isAdmin : Boolean ) = {
159
+ Account (
160
+ userName = " a" ,
161
+ fullName = " b" ,
162
+ mailAddress = " c" ,
163
+ password = " d" ,
164
+ isAdmin = isAdmin,
165
+ url = None ,
166
+ registeredDate = new Date (),
167
+ updatedDate = new Date (),
168
+ lastLoginDate = None ,
169
+ image = None ,
170
+ isGroupAccount = false ,
171
+ isRemoved = false ,
172
+ description = None )
169
173
}
170
-
171
174
}
0 commit comments