Welcome to mirror list, hosted at ThFree Co, Russian Federation.

StateChangeActivityTest.php « Listener « Unit « tests - github.com/nextcloud/twofactor_u2f.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 46585b66871713e0e1fb73cc44c5e44260596f8e (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
<?php

namespace OCA\TwoFactorU2F\Tests\Unit\Listener;

use OCA\TwoFactorU2F\Event\DisabledByAdmin;
use OCA\TwoFactorU2F\Event\StateChanged;
use OCA\TwoFactorU2F\Listener\StateChangeActivity;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\EventDispatcher\Event;
use OCP\IUser;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;

class StateChangeActivityTest extends TestCase {

	/** @var IManager|PHPUnit_Framework_MockObject_MockObject */
	private $activityManager;

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

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

		$this->activityManager = $this->createMock(IManager::class);

		$this->listener = new StateChangeActivity($this->activityManager);
	}

	public function testHandleGenericEvent() {
		$event = new Event();
		$this->activityManager->expects($this->never())
			->method('publish');

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

	public function testHandleEnableEvent() {
		$user = $this->createMock(IUser::class);
		$event = new StateChanged($user, true);
		$activityEvent = $this->createMock(IEvent::class);
		$this->activityManager->expects($this->once())
			->method('generateEvent')
			->willReturn($activityEvent);
		$activityEvent->expects($this->once())
			->method('setApp')
			->with($this->equalTo('twofactor_u2f'))
			->willReturnSelf();
		$activityEvent->expects($this->once())
			->method('setType')
			->with($this->equalTo('security'))
			->willReturnSelf();
		$user->expects($this->any())
			->method('getUID')
			->willReturn('ursula');
		$activityEvent->expects($this->once())
			->method('setAuthor')
			->with($this->equalTo('ursula'))
			->willReturnSelf();
		$activityEvent->expects($this->once())
			->method('setAffectedUser')
			->with($this->equalTo('ursula'))
			->willReturnSelf();
		$activityEvent->expects($this->once())
			->method('setSubject')
			->with($this->equalTo('u2f_device_added'))
			->willReturnSelf();
		$this->activityManager->expects($this->once())
			->method('publish')
			->with($this->equalTo($activityEvent));

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

	public function testHandleDisableEvent() {
		$user = $this->createMock(IUser::class);
		$event = new StateChanged($user, false);
		$activityEvent = $this->createMock(IEvent::class);
		$this->activityManager->expects($this->once())
			->method('generateEvent')
			->willReturn($activityEvent);
		$activityEvent->expects($this->once())
			->method('setApp')
			->with($this->equalTo('twofactor_u2f'))
			->willReturnSelf();
		$activityEvent->expects($this->once())
			->method('setType')
			->with($this->equalTo('security'))
			->willReturnSelf();
		$user->expects($this->any())
			->method('getUID')
			->willReturn('ursula');
		$activityEvent->expects($this->once())
			->method('setAuthor')
			->with($this->equalTo('ursula'))
			->willReturnSelf();
		$activityEvent->expects($this->once())
			->method('setAffectedUser')
			->with($this->equalTo('ursula'))
			->willReturnSelf();
		$activityEvent->expects($this->once())
			->method('setSubject')
			->with($this->equalTo('u2f_device_removed'))
			->willReturnSelf();
		$this->activityManager->expects($this->once())
			->method('publish')
			->with($this->equalTo($activityEvent));

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

	public function testHandleDisabledByAdminEvent() {
		$uid = 'user234';
		$user = $this->createMock(IUser::class);
		$user->method('getUID')->willReturn($uid);
		$event = new DisabledByAdmin($user);
		$activityEvent = $this->createMock(IEvent::class);
		$this->activityManager->expects($this->once())
			->method('generateEvent')
			->willReturn($activityEvent);
		$activityEvent->expects($this->once())
			->method('setApp')
			->with('twofactor_u2f')
			->willReturnSelf();
		$activityEvent->expects($this->once())
			->method('setType')
			->with('security')
			->willReturnSelf();
		$activityEvent->expects($this->once())
			->method('setAuthor')
			->with($uid)
			->willReturnSelf();
		$activityEvent->expects($this->once())
			->method('setAffectedUser')
			->with($uid)
			->willReturnSelf();
		$activityEvent->expects($this->once())
			->method('setSubject')
			->with($this->equalTo('u2f_disabled_by_admin'))
			->willReturnSelf();
		$this->activityManager->expects($this->once())
			->method('publish')
			->with($activityEvent);

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