11package fr .brouillard .gitbucket .h2 .controller
22
3+ import gitbucket .core .controller .Context
34import gitbucket .core .model .Account
45import gitbucket .core .servlet .ApiAuthenticationFilter
56import org .apache .commons .io .FileSystemUtils
67import org .h2 .Driver
78import org .h2 .engine .Database
9+ import org .mockito .Mockito ._
810import org .scalatest .funsuite .AnyFunSuite
911import org .scalatest .matchers .should .Matchers .{convertToAnyShouldWrapper , equal }
1012import org .scalatra .{Ok , Params , ScalatraParams }
@@ -15,30 +17,76 @@ import java.nio.file.{Files, Path, Paths}
1517import java .util .{Date , Properties }
1618import scala .util .Using
1719
18- class H2BackupControllerTests extends ScalatraFunSuite {
20+ import H2BackupControllerTests ._
21+ import gitbucket .core .service .SystemSettingsService
22+
23+ class H2BackupControllerWithAdminTests extends ScalatraFunSuite {
1924 addFilter(classOf [ApiAuthenticationFilter ], path= " /api/*" )
20- addFilter(classOf [H2BackupController ], " /*" )
25+ addFilter(new H2BackupController () {
26+ override implicit val context = buildContext(isAdmin = true )
27+ }, " /*" )
2128
22- test(" get database backup api" ) {
29+ test(" get database backup api with admin " ) {
2330 get(" /api/v3/plugins/database/backup" ) {
2431 status should equal (405 )
2532 body should include (" This has moved" )
2633 }
2734 }
2835
29- test(" get database backup legacy" ) {
36+ test(" get database backup legacy with admin " ) {
3037 get(" /database/backup" ) {
3138 status should equal (405 )
3239 body should include (" This has moved" )
3340 }
3441 }
42+ }
3543
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" ) {
3763 post(" /api/v3/plugins/database/backup" ) {
3864 status should equal (401 )
3965 }
4066 }
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+ }
4178
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+ }
4290}
4391
4492class H2BackupControllerObjectTests extends AnyFunSuite {
@@ -47,23 +95,6 @@ class H2BackupControllerObjectTests extends AnyFunSuite {
4795 assert(name.endsWith(" .zip" ))
4896 }
4997
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-
6798 private def h2Url (file : File ): String = {
6899 " jdbc:h2:file:" + file + " ;DATABASE_TO_UPPER=false"
69100 }
@@ -110,62 +141,34 @@ class H2BackupControllerObjectTests extends AnyFunSuite {
110141 test(" generates default file name" ) {
111142 assertDefaultFileName(H2BackupController .defaultBackupFileName())
112143 }
144+ }
113145
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 )
154149
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
157156 }
158157
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 )
169173 }
170-
171174}
0 commit comments