Issues (78)

Tests/PinTest.php (1 issue)

Severity
1
<?php
2
3
namespace Ikechukwukalu\Requirepin\Tests;
4
5
use Illuminate\Foundation\Testing\RefreshDatabase;
6
use Illuminate\Foundation\Testing\WithoutMiddleware;
7
use Illuminate\Foundation\Testing\WithFaker;
8
use Illuminate\Support\Str;
9
use Illuminate\Support\Facades\Hash;
10
use Illuminate\Support\Facades\Notification;
11
use Illuminate\Support\Facades\Route;
12
use Ikechukwukalu\Requirepin\Tests\Models\Book;
13
use Ikechukwukalu\Requirepin\Models\TestUser;
14
15
class PinTest extends TestCase
16
{
17
    use WithFaker;
18
19
   /**
20
     * A basic feature test example.
21
     *
22
     * @return void
23
     */
24
25
    public function testErrorValidationForChangePin()
26
    {
27
        Notification::fake();
28
29
        $user = TestUser::create([
30
            'name' => str::random(),
31
            'email' => Str::random(40) . '@example.com',
32
            'password' => Hash::make('password'),
33
            'pin' => Hash::make(config('requirepin.default', '0000'))
34
        ]); // Would still have the default pin
35
36
        $this->actingAs($user);
37
38
        $postData = [
39
            'current_pin' => '9090', //Wrong current pin
40
            'pin' => '1uu4', //Wrong pin format
41
            'pin_confirmation' => '1234' //None matching pins
42
        ];
43
44
        $response = $this->post('/test/change/pin', $postData, ['Accept' => 'application/json']);
45
        $responseArray = json_decode($response->getContent(), true);
46
47
        $this->assertTrue(isset($responseArray['message']));
48
        $this->assertTrue(isset($responseArray['errors']));
49
50
        $response = $this->post('/test/change/pin', $postData);
51
        $responseArray = json_decode($response->getContent(), true);
0 ignored issues
show
The assignment to $responseArray is dead and can be removed.
Loading history...
52
53
        $this->assertEquals(302, $response->status());
54
    }
55
56
    public function testChangePin()
57
    {
58
        $user = TestUser::create([
59
            'name' => str::random(),
60
            'email' => Str::random(40) . '@example.com',
61
            'password' => Hash::make('password'),
62
            'pin' => Hash::make(config('requirepin.default', '0000')),
63
        ]);
64
65
        $this->actingAs($user);
66
67
        $postData = [
68
            'current_pin' => config('requirepin.default', '0000'),
69
            'pin' => '1234',
70
            'pin_confirmation' => '1234'
71
        ];
72
73
        $this->assertTrue(Hash::check($postData['current_pin'], $user->pin));
74
75
        $response = $this->post('/test/change/pin', $postData, ['Accept' => 'application/json']);
76
        $responseArray = json_decode($response->getContent(), true);
77
78
        $this->assertEquals(200, $responseArray['status_code']);
79
        $this->assertEquals( 'success', $responseArray['status']);
80
    }
81
82
    public function testRequirePinMiddleWareForCreateBookWeb()
83
    {
84
        $user = TestUser::create([
85
            'name' => str::random(),
86
            'email' => Str::random(40) . '@example.com',
87
            'password' => Hash::make('password'),
88
            'pin' => Hash::make('1234'),
89
            'default_pin' => 0
90
        ]);
91
92
        $this->actingAs($user);
93
94
        $this->assertTrue(Hash::check('1234', $user->pin));
95
96
        $postData = [
97
            'name' => $this->faker->sentence(rand(1,5)),
98
            'isbn' => $this->faker->unique()->isbn13(),
99
            'authors' => implode(",", [$this->faker->name(), $this->faker->name()]),
100
            'publisher' => $this->faker->name(),
101
            'number_of_pages' => rand(45,1500),
102
            'country' => $this->faker->countryISOAlpha3(),
103
            'release_date' => date('Y-m-d')
104
        ];
105
106
        $response = $this->post(route('createBookTest'), $postData);
107
        $this->assertEquals(302, $response->status());
108
    }
109
110
    public function testRequirePinMiddleWareForDeleteBookWeb()
111
    {
112
        $user = TestUser::create([
113
            'name' => str::random(),
114
            'email' => Str::random(40) . '@example.com',
115
            'password' => Hash::make('password'),
116
            'pin' => Hash::make('1234'),
117
            'default_pin' => 0
118
        ]);
119
120
        $this->actingAs($user);
121
122
        $this->assertTrue(Hash::check('1234', $user->pin));
123
124
        $book = Book::find(1);
125
126
        if (!isset($book->id)) {
127
            $book = Book::create([
128
                'name' => $this->faker->sentence(rand(1,5)),
129
                'isbn' => $this->faker->unique()->isbn13(),
130
                'authors' => implode(",", [$this->faker->name(), $this->faker->name()]),
131
                'publisher' => $this->faker->name(),
132
                'number_of_pages' => rand(45,1500),
133
                'country' => $this->faker->countryISOAlpha3(),
134
                'release_date' => date('Y-m-d')
135
            ]);
136
        }
137
138
        $id = $book->id;
139
140
        $response = $this->json('DELETE', route('deleteBookTest', ['id' => $id]));
141
        $this->assertEquals(200, $response->status());
142
    }
143
144
    public function testRequirePinMiddleWareForDeleteBook()
145
    {
146
        $user = TestUser::create([
147
            'name' => str::random(),
148
            'email' => Str::random(40) . '@example.com',
149
            'password' => Hash::make('password'),
150
            'pin' => Hash::make('1234'),
151
            'default_pin' => 0
152
        ]);
153
154
        $this->actingAs($user);
155
156
        $this->assertTrue(Hash::check('1234', $user->pin));
157
158
        $book = Book::find(1);
159
160
        if (!isset($book->id)) {
161
            $book = Book::create([
162
                'name' => $this->faker->sentence(rand(1,5)),
163
                'isbn' => $this->faker->unique()->isbn13(),
164
                'authors' => implode(",", [$this->faker->name(), $this->faker->name()]),
165
                'publisher' => $this->faker->name(),
166
                'number_of_pages' => rand(45,1500),
167
                'country' => $this->faker->countryISOAlpha3(),
168
                'release_date' => date('Y-m-d')
169
            ]);
170
        }
171
172
        $id = $book->id;
173
174
        $response = $this->json('DELETE', route('deleteBookTest', ['id' => $id]), ['Accept' => 'application/json']);
175
        $responseArray = json_decode($response->getContent(), true);
176
177
        $this->assertEquals(200, $responseArray['status_code']);
178
        $this->assertEquals('success', $responseArray['status']);
179
        $this->assertTrue(isset($responseArray['data']['url']));
180
181
        $postData = [
182
            config('requirepin.input', '_pin') => '1234'
183
        ];
184
        $url = $responseArray['data']['url'];
185
186
        $response = $this->post($url, $postData, ['Accept' => 'application/json']);
187
        $responseArray = json_decode($response->getContent(), true);
188
189
        $this->assertEquals(200, $responseArray['status_code']);
190
        $this->assertEquals('success', $responseArray['status']);
191
    }
192
193
    public function testRequirePinMiddleWareForCreateBook()
194
    {
195
        $user = TestUser::create([
196
            'name' => str::random(),
197
            'email' => Str::random(40) . '@example.com',
198
            'password' => Hash::make('password'),
199
            'pin' => Hash::make('1234'),
200
            'default_pin' => 0
201
        ]);
202
203
        $this->actingAs($user);
204
205
        $this->assertTrue(Hash::check('1234', $user->pin));
206
207
        $postData = [
208
            'name' => $this->faker->sentence(rand(1,5)),
209
            'isbn' => $this->faker->unique()->isbn13(),
210
            'authors' => implode(",", [$this->faker->name(), $this->faker->name()]),
211
            'publisher' => $this->faker->name(),
212
            'number_of_pages' => rand(45,1500),
213
            'country' => $this->faker->countryISOAlpha3(),
214
            'release_date' => date('Y-m-d')
215
        ];
216
217
        $response = $this->post(route('createBookTest'), $postData, ['Accept' => 'application/json']);
218
        $responseArray = json_decode($response->getContent(), true);
219
220
        $this->assertEquals(200, $responseArray['status_code']);
221
        $this->assertEquals('success', $responseArray['status']);
222
        $this->assertTrue(isset($responseArray['data']['url']));
223
224
        $postData = [
225
            config('requirepin.input', '_pin') => '1234'
226
        ];
227
        $url = $responseArray['data']['url'];
228
229
        $response = $this->post($url, $postData, ['Accept' => 'application/json']);
230
        $responseArray = json_decode($response->getContent(), true);
231
232
        $this->assertEquals(200, $responseArray['status_code']);
233
        $this->assertEquals('success', $responseArray['status']);
234
    }
235
}
236