Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace V1\User\Authentication;
- use \ApiTester;
- use EmasGogo\GoDompet\Domain\Gold\Entities\GoDompetGoldAccount;
- use EmasGogo\GoDompet\Domain\Payment\Entities\GodompetPaymentAccount;
- use EmasGogo\GoDompet\Domain\Doku\Entities\GoDompetDokuWallet;
- use EmasGogo\User\Domain\Entities\User;
- use EmasGogo\User\Domain\Entities\Device;
- use EmasGogo\Infrastructure\Hasher\SHATwoFiveSixHasher;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Cache;
- class EmailAuthenticationTestCest
- {
- protected $location;
- public function _before(ApiTester $I)
- {
- $I->callArtisan('cache:clear');
- $this->location = json_encode(["id" => "Kota Malang, Jawa Timur", "en" => "Malang City, East Java"]);
- }
- public function _after(ApiTester $I)
- {
- }
- /**
- * @param ApiTester $I
- */
- public function UserSuccesLoginWithSameDeviceTest(ApiTester $I)
- {
- $user = factory(User::class)->create([
- 'email' => 'john@mail.com',
- 'password' => bcrypt('secret'),
- 'status' => 'active',
- ]);
- $user->devices()->save(new Device([
- 'user_id' => $user->id,
- 'name' => 'samsung',
- 'device_id' => 'qwertyuiop',
- 'latitude' => '-7.966620',
- 'longitude' => '112.632632'
- ]));
- $user->godompetPaymentAccount()->save(new GodompetPaymentAccount([
- 'balance' => 0.00,
- 'status' => 'active'
- ]));
- $user->GodompetGoldAccount()->save(new GoDompetGoldAccount([
- 'balance' => 0.00,
- 'status' => 'active'
- ]));
- $user->GodompetDoku()->save(new GoDompetDokuWallet([
- 'balance' => 0.00,
- 'status' => 'active'
- ]));
- $signature = (new SHATwoFiveSixHasher())->hash('v1authenticationemailjohn@mail.comsecretqwerty123');
- $I->wantToTest('User successfully login using email with same device');
- $I->haveHttpHeader('Content-type', 'application/json');
- $I->sendPOST('api/v1/authentication', json_encode([
- 'auth_type' => 'email',
- 'email' => 'john@mail.com',
- 'password' => 'secret',
- 'device' => 'samsung',
- 'device_id' => 'qwertyuiop',
- 'longitude' => '112.632632',
- 'latitude' => '-7.966620',
- 'signature' => $signature
- ]));
- $I->seeResponseCodeIs(200);
- $I->seeResponseContainsJson([
- 'data' => [
- 'fullname' => $user->name,
- 'email' => $user->email,
- 'identity' => null,
- 'user_devices' => [
- [
- 'name' => 'samsung',
- 'latitude' => '-7.966620',
- 'longitude' => '112.632632',
- 'device_id' => 'qwertyuiop'
- ]
- ]
- ]
- ]);
- $user = User::where('email', 'john@mail.com')->first();
- $device = $user->devices()->where('device_id', 'qwertyuiop')->first();
- $I->assertNotNull($device);
- $I->seeRecord('devices', ['user_id' => $user->id, 'last_logged_in_location' => $this->location ]);
- }
- public function UserSuccesLoginUsingBrowser(ApiTester $I)
- {
- $user = factory(User::class)->create([
- 'email' => 'john@mail.com',
- 'password' => bcrypt('secret'),
- 'status' => 'active',
- ]);
- $user->godompetPaymentAccount()->save(new GodompetPaymentAccount([
- 'balance' => 0.00,
- 'status' => 'active'
- ]));
- $user->GodompetGoldAccount()->save(new GoDompetGoldAccount([
- 'balance' => 0.00,
- 'status' => 'active'
- ]));
- $user->GodompetDoku()->save(new GoDompetDokuWallet([
- 'balance' => 0.00,
- 'status' => 'active'
- ]));
- $signature = (new SHATwoFiveSixHasher())->hash('v1authenticationemailjohn@mail.comsecretqwerty123');
- $I->wantToTest('User successfully login using email with same device');
- $I->haveHttpHeader('Content-type', 'application/json');
- $I->sendPOST('api/v1/authentication', json_encode([
- 'auth_type' => 'email',
- 'email' => 'john@mail.com',
- 'password' => 'secret',
- 'device' => 'Chrome#Win32',
- 'device_id' => 'newBrowserAccess',
- 'longitude' => '112.632632',
- 'latitude' => '-7.966620',
- 'signature' => $signature
- ]));
- $I->seeResponseCodeIs(200);
- codecept_debug($I->grabResponse());
- }
- /**
- * @param ApiTester $I
- */
- public function UserSuccesLoginWithOneDeviceCountTest(ApiTester $I)
- {
- $user = factory(User::class)->create([
- 'email' => 'john@mail.com',
- 'password' => bcrypt('secret'),
- 'status' => 'active',
- ]);
- $user->devices()->save(new Device([
- 'user_id' => $user->id,
- 'name' => 'samsung',
- 'device_id' => 'qwertyuiop',
- 'latitude' => '-7.966620',
- 'longitude' => '112.632632'
- ]));
- $user->godompetPaymentAccount()->save(new GodompetPaymentAccount([
- 'balance' => 0.00,
- 'status' => 'active'
- ]));
- $user->GodompetGoldAccount()->save(new GoDompetGoldAccount([
- 'balance' => 0.00,
- 'status' => 'active'
- ]));
- $user->GodompetDoku()->save(new GoDompetDokuWallet([
- 'balance' => 0.00,
- 'status' => 'active'
- ]));
- $I->wantToTest('User successfully login using one device count');
- $signature = (new SHATwoFiveSixHasher())->hash('v1authenticationemailjohn@mail.comsecretqwerty123');
- $I->assertEquals(1, $user->devices->count());
- $I->haveHttpHeader('Content-type', 'application/json');
- $I->sendPOST('api/v1/authentication', json_encode([
- 'auth_type' => 'email',
- 'email' => 'john@mail.com',
- 'password' => 'secret',
- 'device' => 'nexian',
- 'device_id' => 'qwertyuiop1',
- 'longitude' => '112.632632',
- 'latitude' => '-7.966620',
- 'signature' => $signature
- ]));
- $I->seeResponseCodeIs(200);
- $fetchUser = User::where('email', 'john@mail.com')->first();
- $device = $fetchUser->devices()->where('device_id', 'qwertyuiop1')->first();
- $I->assertNotNull($device);
- $I->assertEquals(2, $fetchUser->devices()->count());
- $I->seeRecord('devices', ['user_id' => $fetchUser->id, 'name' => 'samsung', 'longitude' => '112.632632', 'latitude' => '-7.966620']);
- $I->seeRecord('devices', ['user_id' => $fetchUser->id, 'name' => 'nexian', 'longitude' => '112.632632', 'latitude' => '-7.966620', 'last_logged_in_location' => $this->location]);
- }
- /**
- * @param ApiTester $I
- */
- public function UserSuccesLoginWithTwoDeviceCountTest(ApiTester $I)
- {
- $user = factory(User::class)->create([
- 'email' => 'john@mail.com',
- 'password' => bcrypt('secret'),
- 'status' => 'active',
- ]);
- $user->devices()->save(new Device([
- 'user_id' => $user->id,
- 'name' => 'samsung',
- 'device_id' => 'qwertyuiop1',
- 'latitude' => '-7.966620',
- 'longitude' => '112.632632'
- ]));
- $user->devices()->save(new Device([
- 'user_id' => $user->id,
- 'name' => 'nexian',
- 'device_id' => 'qwertyuiop2',
- 'latitude' => '-7.966620',
- 'longitude' => '112.632632'
- ]));
- $user->godompetPaymentAccount()->save(new GodompetPaymentAccount([
- 'balance' => 0.00,
- 'status' => 'active'
- ]));
- $user->GodompetGoldAccount()->save(new GoDompetGoldAccount([
- 'balance' => 0.00,
- 'status' => 'active'
- ]));
- $user->GodompetDoku()->save(new GoDompetDokuWallet([
- 'balance' => 0.00,
- 'status' => 'active'
- ]));
- $I->wantToTest('User successfully login using 2 device count');
- $signature = (new SHATwoFiveSixHasher())->hash('v1authenticationemailjohn@mail.comsecretqwerty123');
- $I->assertEquals(2, $user->devices->count());
- $I->haveHttpHeader('Content-type', 'application/json');
- $I->sendPOST('api/v1/authentication', json_encode([
- 'auth_type' => 'email',
- 'email' => 'john@mail.com',
- 'password' => 'secret',
- 'device' => 'oppo',
- 'device_id' => 'qwertyuiop3',
- 'longitude' => '112.632632',
- 'latitude' => '-7.966620',
- 'signature' => $signature
- ]));
- $I->seeResponseCodeIs(200);
- $fetchUser = User::where('email', 'john@mail.com')->first();
- $device = $fetchUser->devices()->where('device_id', 'qwertyuiop3')->first();
- $I->assertNotNull($device);
- $I->assertEquals(3, $fetchUser->devices()->count());
- $I->seeRecord('devices', ['user_id' => $fetchUser->id, 'name' => 'oppo', 'longitude' => '112.632632', 'latitude' => '-7.966620', 'device_id' => 'qwertyuiop3', 'last_logged_in_location' => $this->location]);
- }
- /**
- * @param ApiTester $I
- */
- // public function UserNotAllowedLoginHasMaximumDeviceCountTest(ApiTester $I)
- // {
- // $user = factory(User::class)->create([
- // 'email' => 'john@mail.com',
- // 'password' => bcrypt('secret'),
- // 'status' => 'active',
- // ]);
- //
- // $user->devices()->save(new Device([
- // 'user_id' => $user->id,
- // 'name' => 'samsung',
- // 'device_id' => 'qwertyuiop1',
- // 'latitude' => '-7.966620',
- // 'longitude' => '112.632632'
- // ]));
- //
- // $user->devices()->save(new Device([
- // 'user_id' => $user->id,
- // 'name' => 'nexian',
- // 'device_id' => 'qwertyuiop2',
- // 'latitude' => '-7.966620',
- // 'longitude' => '112.632632'
- // ]));
- //
- // $user->devices()->save(new Device([
- // 'user_id' => $user->id,
- // 'name' => 'oppo',
- // 'device_id' => 'qwertyuiop3',
- // 'latitude' => '-7.966620',
- // 'longitude' => '112.632632'
- // ]));
- //
- // $user->godompetPaymentAccount()->save(new GodompetPaymentAccount([
- // 'balance' => 0.00,
- // 'status' => 'active'
- // ]));
- //
- // $user->GodompetGoldAccount()->save(new GoDompetGoldAccount([
- // 'balance' => 0.00,
- // 'status' => 'active'
- // ]));
- //
- // $user->GodompetDoku()->save(new GoDompetDokuWallet([
- // 'balance' => 0.00,
- // 'status' => 'active'
- // ]));
- //
- // $I->wantToTest('User is not allowed login using invalid device count');
- //
- // $signature = (new SHATwoFiveSixHasher())->hash('v1authenticationemailjohn@mail.comsecretqwerty123');
- //
- // $I->assertEquals(3, $user->devices->count());
- //
- // $I->haveHttpHeader('Content-type', 'application/json');
- // $I->sendPOST('api/v1/authentication', json_encode([
- // 'auth_type' => 'email',
- // 'email' => 'john@mail.com',
- // 'password' => 'secret',
- // 'device' => 'xiaomi',
- // 'device_id' => 'qwertyuiop4',
- // 'longitude' => '112.632632',
- // 'latitude' => '-7.966620',
- // 'signature' => $signature
- // ]));
- // $I->seeResponseCodeIs(400);
- // $I->seeResponseContainsJson(['errors' => ['code' => '004']]);
- // }
- /**
- * @param ApiTester $I
- */
- public function UserFailedLoginUsingInvalidEmailOrPasswordTest(ApiTester $I)
- {
- $I->wantToTest('User failed login using invalid email or password');
- $signature = (new SHATwoFiveSixHasher())->hash('v1authenticationemailjohn@mail.comsecretqwerty123');
- // EN
- $I->haveHttpHeader('Content-type', 'application/json');
- $I->sendPOST('api/v1/authentication', json_encode([
- 'auth_type' => 'email',
- 'email' => 'john@mail.com',
- 'password' => 'secret',
- 'device' => 'xiaomi',
- 'device_id' => 'qwertyuiop1',
- 'longitude' => '112.632632',
- 'latitude' => '-7.966620',
- 'signature' => $signature
- ]));
- $I->seeResponseCodeIs(401);
- $I->seeResponseContainsJson(['messages' => ['Wrong email / password or your account has been banned.']]);
- }
- /**
- * @param ApiTester $I
- */
- public function UserFailedLoginUsingInvalidSignatureTest(ApiTester $I)
- {
- $I->wantToTest('User failed login using invalid signature');
- // EN
- $I->haveHttpHeader('Content-type', 'application/json');
- $I->sendPOST('api/v1/authentication', json_encode([
- 'auth_type' => 'email',
- 'email' => 'john@mail.com',
- 'password' => 'secret',
- 'device' => 'xiaomi',
- 'device_id' => 'qwertyuiop1',
- 'longitude' => '112.632632',
- 'latitude' => '-7.966620',
- 'signature' => 'ngawursignature'
- ]));
- $I->seeResponseCodeIs(422);
- $I->seeResponseContainsJson(['messages' => ['The signature is invalid.']]);
- }
- /**
- * @param ApiTester $I
- */
- public function UserFailedLoginUsingInvalidEmailOrPasswordAttemptTest(ApiTester $I)
- {
- $I->wantToTest('User failed login using invalid email or password attempt');
- $signature = (new SHATwoFiveSixHasher())->hash('v1authenticationemailjohn@mail.comsecretqwerty123');
- // EN
- $expiresAt = Carbon::now()->addMinutes(1);
- Cache::put('passwordLimitCountjohn@mail.com', 1, $expiresAt);
- $I->haveHttpHeader('Content-type', 'application/json');
- $I->sendPOST('api/v1/authentication', json_encode([
- 'auth_type' => 'email',
- 'email' => 'john@mail.com',
- 'password' => 'secret',
- 'device' => 'xiaomi',
- 'device_id' => 'qwertyuiop1',
- 'longitude' => '112.632632',
- 'latitude' => '-7.966620',
- 'signature' => $signature
- ]));
- $I->assertEquals(2, Cache::get('passwordLimitCountjohn@mail.com'));
- $I->seeResponseCodeIs(401);
- $I->seeResponseContainsJson(['messages' => ['Wrong email / password or your account has been banned.']]);
- }
- /**
- * @param ApiTester $I
- */
- public function UserFailedLoginWithAttemptLimitTest(ApiTester $I)
- {
- $I->callArtisan('cache:clear');
- $I->wantToTest('User failed login with attempt limit');
- $signature = (new SHATwoFiveSixHasher())->hash('v1authenticationemailjohn@mail.comsecretqwerty123');
- // EN
- $expiresAt = Carbon::now()->addMinutes(1);
- Cache::put('passwordLimitCountjohn@mail.com', 4, $expiresAt);
- $I->haveHttpHeader('Content-type', 'application/json');
- $I->sendPOST('api/v1/authentication', json_encode([
- 'auth_type' => 'email',
- 'email' => 'john@mail.com',
- 'password' => 'secret',
- 'device' => 'xiaomi',
- 'device_id' => 'qwertyuiop1',
- 'longitude' => '112.632632',
- 'latitude' => '-7.966620',
- 'signature' => $signature
- ]));
- $I->seeResponseCodeIs(422);
- $I->seeResponseContainsJson([
- 'status_code' => 422,
- 'data' => [
- 'errors' => [
- 'code' => '013',
- ]
- ]
- ]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement