aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Authentication/Listeners/RemoteWipeNotificationsListenerTest.php
blob: 53fa502b1999f7a29d3c97d0e6dac7aebc2c9c49 (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
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace Test\Authentication\Events;

use DateTime;
use OC\Authentication\Events\RemoteWipeFinished;
use OC\Authentication\Events\RemoteWipeStarted;
use OC\Authentication\Listeners\RemoteWipeNotificationsListener;
use OC\Authentication\Token\IToken;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Notification\IManager as INotificationManager;
use OCP\Notification\INotification;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class RemoteWipeNotificationsListenerTest extends TestCase {
	/** @var INotificationManager|MockObject */
	private $notificationManager;

	/** @var ITimeFactory|MockObject */
	private $timeFactory;

	/** @var IEventListener */
	private $listener;

	protected function setUp(): void {
		parent::setUp();

		$this->notificationManager = $this->createMock(INotificationManager::class);
		$this->timeFactory = $this->createMock(ITimeFactory::class);

		$this->listener = new RemoteWipeNotificationsListener(
			$this->notificationManager,
			$this->timeFactory
		);
	}

	public function testHandleUnrelated(): void {
		$event = new Event();

		$this->listener->handle($event);

		$this->addToAssertionCount(1);
	}

	public function testHandleRemoteWipeStarted(): void {
		$token = $this->createMock(IToken::class);
		$event = new RemoteWipeStarted($token);
		$notification = $this->createMock(INotification::class);
		$this->notificationManager->expects($this->once())
			->method('createNotification')
			->willReturn($notification);
		$notification->expects($this->once())
			->method('setApp')
			->with('auth')
			->willReturnSelf();
		$token->method('getUID')->willReturn('user123');
		$notification->expects($this->once())
			->method('setUser')
			->with('user123')
			->willReturnSelf();
		$now = new DateTime();
		$this->timeFactory->method('getDateTime')->willReturn($now);
		$notification->expects($this->once())
			->method('setDateTime')
			->with($now)
			->willReturnSelf();
		$token->method('getId')->willReturn(123);
		$notification->expects($this->once())
			->method('setObject')
			->with('token', '123')
			->willReturnSelf();
		$token->method('getName')->willReturn('Token 1');
		$notification->expects($this->once())
			->method('setSubject')
			->with('remote_wipe_start', [
				'name' => 'Token 1'
			])
			->willReturnSelf();
		$this->notificationManager->expects($this->once())
			->method('notify');

		$this->listener->handle($event);
	}

	public function testHandleRemoteWipeFinished(): void {
		$token = $this->createMock(IToken::class);
		$event = new RemoteWipeFinished($token);
		$notification = $this->createMock(INotification::class);
		$this->notificationManager->expects($this->once())
			->method('createNotification')
			->willReturn($notification);
		$notification->expects($this->once())
			->method('setApp')
			->with('auth')
			->willReturnSelf();
		$token->method('getUID')->willReturn('user123');
		$notification->expects($this->once())
			->method('setUser')
			->with('user123')
			->willReturnSelf();
		$now = new DateTime();
		$this->timeFactory->method('getDateTime')->willReturn($now);
		$notification->expects($this->once())
			->method('setDateTime')
			->with($now)
			->willReturnSelf();
		$token->method('getId')->willReturn(123);
		$notification->expects($this->once())
			->method('setObject')
			->with('token', '123')
			->willReturnSelf();
		$token->method('getName')->willReturn('Token 1');
		$notification->expects($this->once())
			->method('setSubject')
			->with('remote_wipe_finish', [
				'name' => 'Token 1'
			])
			->willReturnSelf();
		$this->notificationManager->expects($this->once())
			->method('notify');

		$this->listener->handle($event);
	}
}