Passed
Push — main ( 0cfbf7...d65d7b )
by ikechukwu
02:46
created

testRequirePinMiddleWareForCreateBookWeb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 26
rs 9.6666
cc 1
nc 1
nop 0
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
Unused Code introduced by
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 testRequirePinMiddleWareForCreateBook()
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, ['Accept' => 'application/json']);
107
        $responseArray = json_decode($response->getContent(), true);
108
109
        $this->assertEquals(200, $responseArray['status_code']);
110
        $this->assertEquals('success', $responseArray['status']);
111
        $this->assertTrue(isset($responseArray['data']['url']));
112
113
        $postData = [
114
            config('requirepin.input', '_pin') => '1234'
115
        ];
116
        $url = $responseArray['data']['url'];
117
118
        $response = $this->post($url, $postData, ['Accept' => 'application/json']);
119
        $responseArray = json_decode($response->getContent(), true);
120
121
        $this->assertEquals(200, $responseArray['status_code']);
122
        $this->assertEquals('success', $responseArray['status']);
123
    }
124
125
    public function testRequirePinMiddleWareForDeleteBook()
126
    {
127
        $user = TestUser::create([
128
            'name' => str::random(),
129
            'email' => Str::random(40) . '@example.com',
130
            'password' => Hash::make('password'),
131
            'pin' => Hash::make('1234'),
132
            'default_pin' => 0
133
        ]);
134
135
        $this->actingAs($user);
136
137
        $this->assertTrue(Hash::check('1234', $user->pin));
138
139
        $book = Book::find(1);
140
141
        if (!isset($book->id)) {
142
            $book = Book::create([
143
                'name' => $this->faker->sentence(rand(1,5)),
144
                'isbn' => $this->faker->unique()->isbn13(),
145
                'authors' => implode(",", [$this->faker->name(), $this->faker->name()]),
146
                'publisher' => $this->faker->name(),
147
                'number_of_pages' => rand(45,1500),
148
                'country' => $this->faker->countryISOAlpha3(),
149
                'release_date' => date('Y-m-d')
150
            ]);
151
        }
152
153
        $id = $book->id;
154
155
        $response = $this->json('DELETE', route('deleteBookTest', ['id' => $id]), ['Accept' => 'application/json']);
156
        $responseArray = json_decode($response->getContent(), true);
157
158
        $this->assertEquals(200, $responseArray['status_code']);
159
        $this->assertEquals('success', $responseArray['status']);
160
        $this->assertTrue(isset($responseArray['data']['url']));
161
162
        $postData = [
163
            config('requirepin.input', '_pin') => '1234'
164
        ];
165
        $url = $responseArray['data']['url'];
166
167
        $response = $this->post($url, $postData, ['Accept' => 'application/json']);
168
        $responseArray = json_decode($response->getContent(), true);
169
170
        $this->assertEquals(200, $responseArray['status_code']);
171
        $this->assertEquals('success', $responseArray['status']);
172
    }
173
174
    public function testRequirePinMiddleWareForCreateBookWeb()
175
    {
176
        $user = TestUser::create([
177
            'name' => str::random(),
178
            'email' => Str::random(40) . '@example.com',
179
            'password' => Hash::make('password'),
180
            'pin' => Hash::make('1234'),
181
            'default_pin' => 0
182
        ]);
183
184
        $this->actingAs($user);
185
186
        $this->assertTrue(Hash::check('1234', $user->pin));
187
188
        $postData = [
189
            'name' => $this->faker->sentence(rand(1,5)),
190
            'isbn' => $this->faker->unique()->isbn13(),
191
            'authors' => implode(",", [$this->faker->name(), $this->faker->name()]),
192
            'publisher' => $this->faker->name(),
193
            'number_of_pages' => rand(45,1500),
194
            'country' => $this->faker->countryISOAlpha3(),
195
            'release_date' => date('Y-m-d')
196
        ];
197
198
        $response = $this->post(route('createBookTest'), $postData);
199
        $this->assertEquals(302, $response->status());
200
    }
201
202
    public function testRequirePinMiddleWareForDeleteBookWeb()
203
    {
204
        $user = TestUser::create([
205
            'name' => str::random(),
206
            'email' => Str::random(40) . '@example.com',
207
            'password' => Hash::make('password'),
208
            'pin' => Hash::make('1234'),
209
            'default_pin' => 0
210
        ]);
211
212
        $this->actingAs($user);
213
214
        $this->assertTrue(Hash::check('1234', $user->pin));
215
216
        $book = Book::find(1);
217
218
        if (!isset($book->id)) {
219
            $book = Book::create([
220
                'name' => $this->faker->sentence(rand(1,5)),
221
                'isbn' => $this->faker->unique()->isbn13(),
222
                'authors' => implode(",", [$this->faker->name(), $this->faker->name()]),
223
                'publisher' => $this->faker->name(),
224
                'number_of_pages' => rand(45,1500),
225
                'country' => $this->faker->countryISOAlpha3(),
226
                'release_date' => date('Y-m-d')
227
            ]);
228
        }
229
230
        $id = $book->id;
231
232
        $response = $this->json('DELETE', route('deleteBookTest', ['id' => $id]));
233
        $this->assertEquals(200, $response->status());
234
    }
235
}
236