-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathDownloadSecurityLogTest.php
More file actions
134 lines (119 loc) · 4.89 KB
/
Copy pathDownloadSecurityLogTest.php
File metadata and controls
134 lines (119 loc) · 4.89 KB
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
namespace Tests\Feature\Jobs;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Storage;
use ProcessMaker\Events\SecurityLogDownloadJobCompleted;
use ProcessMaker\Jobs\DownloadSecurityLog;
use ProcessMaker\Models\SecurityLog;
use ReflectionMethod;
use Tests\Feature\Shared\RequestHelper;
use Tests\TestCase;
class DownloadSecurityLogTest extends TestCase
{
use RequestHelper;
protected $simpleCollection;
protected function withUserSetup(): void
{
SecurityLog::query()->delete();
$this->simpleCollection = [
['id' => 1, 'event' => 'login'],
['id' => 2, 'event' => 'logout'],
];
SecurityLog::factory()->create(['event' => 'login', 'user_id' => $this->user->id]);
SecurityLog::factory()->create(['event' => 'logout', 'user_id' => $this->user->id]);
SecurityLog::factory()->create(['event' => 'attempt']);
SecurityLog::factory()->create(['event' => 'attempt']);
}
public function testCreateTemporaryFilename()
{
$job = new DownloadSecurityLog($this->user, DownloadSecurityLog::FORMAT_CSV);
$method = new ReflectionMethod($job, 'createTemporaryFilename');
$filename = $method->invoke($job);
$this->assertStringContainsString('.csv', $filename);
$job = new DownloadSecurityLog($this->user, DownloadSecurityLog::FORMAT_XML);
$method = new ReflectionMethod($job, 'createTemporaryFilename');
$filename = $method->invoke($job);
$this->assertStringContainsString('.xml', $filename);
}
public function testExpires()
{
$job = new DownloadSecurityLog($this->user, DownloadSecurityLog::FORMAT_CSV);
$method = new ReflectionMethod($job, 'getExpires');
$expires = $method->invoke($job);
$this->assertLessThan($expires, now());
$job = new DownloadSecurityLog($this->user, DownloadSecurityLog::FORMAT_XML);
$method = new ReflectionMethod($job, 'getExpires');
$expires = $method->invoke($job);
$this->assertLessThan($expires, now());
}
/**
* @covers DownloadSecurityLog::toCSV
*/
public function testWriteContentCSV()
{
$stream = fopen('php://temp', 'w+');
$job = new DownloadSecurityLog($this->user, DownloadSecurityLog::FORMAT_CSV);
$csv = (new ReflectionMethod($job, 'writeContent'))->invoke($job, $stream);
$this->assertNotEmpty($csv);
$this->assertTrue(rewind($stream));
$this->assertTrue(fclose($stream));
}
/**
* @covers DownloadSecurityLog::initialTagsXML
* @covers DownloadSecurityLog::toXML
* @covers DownloadSecurityLog::endTagsXML
*/
public function testWriteContentXML()
{
$stream = fopen('php://temp', 'w+');
$job = new DownloadSecurityLog($this->user, DownloadSecurityLog::FORMAT_XML);
$xml = (new ReflectionMethod($job, 'writeContent'))->invoke($job, $stream);
$this->assertNotEmpty($xml);
$this->assertTrue(rewind($stream));
$this->assertTrue(fclose($stream));
}
public function testHandleWithSuccess()
{
if (
!config('filesystems.disks.s3.key')
&& !config('filesystems.disks.s3.secret')
&& !config('filesystems.disks.s3.region')
&& !config('filesystems.disks.s3.bucket')
) {
$this->markTestSkipped(
'AWS S3 service is not available.'
);
} else {
Event::fake([SecurityLogDownloadJobCompleted::class]);
$job = new DownloadSecurityLog($this->user, DownloadSecurityLog::FORMAT_CSV);
$url = (new ReflectionMethod($job, 'handle'))->invoke($job);
$this->assertNotEmpty($url);
$data = file_get_contents($url);
$this->assertNotEmpty($data);
Event::assertDispatched(SecurityLogDownloadJobCompleted::class);
}
}
public function testExport()
{
if (
!config('filesystems.disks.s3.key')
&& !config('filesystems.disks.s3.secret')
&& !config('filesystems.disks.s3.region')
&& !config('filesystems.disks.s3.bucket')
) {
$this->markTestSkipped(
'AWS S3 service is not available.'
);
} else {
Event::fake([SecurityLogDownloadJobCompleted::class]);
$job = new DownloadSecurityLog($this->user, DownloadSecurityLog::FORMAT_CSV);
$filename = (new ReflectionMethod($job, 'createTemporaryFilename'))->invoke($job);
$expires = (new ReflectionMethod($job, 'getExpires'))->invoke($job);
$url = (new ReflectionMethod($job, 'export'))->invoke($job, $filename, $expires);
$this->assertNotEmpty($url);
$data = file_get_contents($url);
$this->assertNotEmpty($data);
Event::assertDispatched(SecurityLogDownloadJobCompleted::class);
}
}
}