3131use OCA \DAV \CalDAV \Calendar ;
3232use OCA \DAV \CalDAV \CalendarHome ;
3333use OCP \IConfig ;
34+ use OCP \IUser ;
3435use Test \TestCase ;
3536
3637class EnablePluginTest extends TestCase {
@@ -44,6 +45,9 @@ class EnablePluginTest extends TestCase {
4445 /** @var BirthdayService |\PHPUnit\Framework\MockObject\MockObject */
4546 protected $ birthdayService ;
4647
48+ /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
49+ protected $ user ;
50+
4751 /** @var \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin $plugin */
4852 protected $ plugin ;
4953
@@ -61,8 +65,9 @@ protected function setUp(): void {
6165
6266 $ this ->config = $ this ->createMock (IConfig::class);
6367 $ this ->birthdayService = $ this ->createMock (BirthdayService::class);
68+ $ this ->user = $ this ->createMock (IUser::class);
6469
65- $ this ->plugin = new EnablePlugin ($ this ->config , $ this ->birthdayService );
70+ $ this ->plugin = new EnablePlugin ($ this ->config , $ this ->birthdayService , $ this -> user );
6671 $ this ->plugin ->initialize ($ this ->server );
6772
6873 $ this ->request = $ this ->createMock (\Sabre \HTTP \RequestInterface::class);
@@ -80,7 +85,7 @@ public function testGetName(): void {
8085 public function testInitialize (): void {
8186 $ server = $ this ->createMock (\Sabre \DAV \Server::class);
8287
83- $ plugin = new EnablePlugin ($ this ->config , $ this ->birthdayService );
88+ $ plugin = new EnablePlugin ($ this ->config , $ this ->birthdayService , $ this -> user );
8489
8590 $ server ->expects ($ this ->once ())
8691 ->method ('on ' )
@@ -143,6 +148,55 @@ public function testHttpPostWrongRequest(): void {
143148 $ this ->plugin ->httpPost ($ this ->request , $ this ->response );
144149 }
145150
151+ public function testHttpPostNotAuthorized (): void {
152+ $ calendarHome = $ this ->createMock (CalendarHome::class);
153+
154+ $ this ->server ->expects ($ this ->once ())
155+ ->method ('getRequestUri ' )
156+ ->willReturn ('/bar/foo ' );
157+ $ this ->server ->tree ->expects ($ this ->once ())
158+ ->method ('getNodeForPath ' )
159+ ->with ('/bar/foo ' )
160+ ->willReturn ($ calendarHome );
161+
162+ $ calendarHome ->expects ($ this ->once ())
163+ ->method ('getOwner ' )
164+ ->willReturn ('principals/users/BlaBlub ' );
165+
166+ $ this ->request ->expects ($ this ->once ())
167+ ->method ('getBodyAsString ' )
168+ ->willReturn ('<nc:enable-birthday-calendar xmlns:nc="http://nextcloud.com/ns"/> ' );
169+
170+ $ this ->request ->expects ($ this ->once ())
171+ ->method ('getUrl ' )
172+ ->willReturn ('url_abc ' );
173+
174+ $ this ->server ->xml ->expects ($ this ->once ())
175+ ->method ('parse ' )
176+ ->willReturnCallback (function ($ requestBody , $ url , &$ documentType ): void {
177+ $ documentType = '{http://nextcloud.com/ns}enable-birthday-calendar ' ;
178+ });
179+
180+ $ this ->user ->expects (self ::once ())
181+ ->method ('getUID ' )
182+ ->willReturn ('admin ' );
183+
184+ $ this ->server ->httpResponse ->expects ($ this ->once ())
185+ ->method ('setStatus ' )
186+ ->with (403 );
187+
188+ $ this ->config ->expects ($ this ->never ())
189+ ->method ('setUserValue ' );
190+
191+ $ this ->birthdayService ->expects ($ this ->never ())
192+ ->method ('syncUser ' );
193+
194+
195+ $ result = $ this ->plugin ->httpPost ($ this ->request , $ this ->response );
196+
197+ $ this ->assertEquals (false , $ result );
198+ }
199+
146200 public function testHttpPost (): void {
147201 $ calendarHome = $ this ->createMock (CalendarHome::class);
148202
@@ -172,6 +226,10 @@ public function testHttpPost(): void {
172226 $ documentType = '{http://nextcloud.com/ns}enable-birthday-calendar ' ;
173227 });
174228
229+ $ this ->user ->expects (self ::exactly (3 ))
230+ ->method ('getUID ' )
231+ ->willReturn ('BlaBlub ' );
232+
175233 $ this ->config ->expects ($ this ->once ())
176234 ->method ('setUserValue ' )
177235 ->with ('BlaBlub ' , 'dav ' , 'generateBirthdayCalendar ' , 'yes ' );
0 commit comments