<?php
use App\User;

class UserTest extends TestCase
{
    /**
     * Test to check get users
     *
     * @return void
     */
    public function testUsers()
    {
       $response = $this->call('GET', 'api/v1/user/');
       $this->assertEquals(200, $response->status());
    }
    
    
    /**
     * Test to check get user
     *
     * @return void
     */
//    public function testUser()
//    {
//       $response = $this->call('GET', 'api/v1/user/4');
//       $this->assertEquals(200, $response->status());
//    }
//   
    
     /**
     * Test to check login
     *
     * @return void
     */
    public function testLogin()
    {
        
       $user = User::where('role', 'ROLE_EMPLOYEE')->first();
       $manager = User::where('role', 'ROLE_MANAGER')->first();
       $this->post('api/v1/user/login', ['email' => ' ', 'password' => '1212'])
             ->seeJson([
                'status' => 'error',
             ]);
       
       $this->post('api/v1/user/login', ['email' => $user->email, 'password' => '123456'])
             ->seeJson([
                'status' => 'fail',
             ]);
       if($manager) {
       $this->post('api/v1/user/login', ['email' => $manager->email, 'password' => '123456'])
             ->seeJson([
                'status' => 'success',
             ]);
       $this->post('api/v1/user/login', ['email' => $manager->email, 'password' => '1212'])
             ->seeJson([
                'status' => 'fail',
             ]);
       
       $manager->status = 2;
       $manager->save();
       $this->post('api/v1/user/login', ['email' => $manager->email, 'password' => '123456'])
             ->seeJson([
                'status' => 'fail',
             ]);
       $manager->status =1;
       $manager->save();
       }
      
    }
    
    
    
    
    /**
     * Test to check changepassword
     *
     * @return void
     */
    public function testChangePassword()
    {
       $this->post('api/v1/user/changepassword', ['user_id' => '12', 'current_password' => '222222222', 'new_password' => '123456'])
             ->seeJson([
                'status' => 'fail',
             ]);
       $this->post('api/v1/user/changepassword', ['user_id' => '5', 'current_password' => '123456', 'new_password' => '123456'])
             ->seeJson([
                'status' => 'success',
             ]);
       $this->post('api/v1/user/changepassword', ['user_id' => '512121', 'current_password' => '123456', 'new_password' => '123456'])
             ->seeJson([
                'status' => 'fail',
             ]);
       $this->post('api/v1/user/changepassword', ['user_id' => '5', 'current_password' => '123456445', 'new_password' => '123456'])
             ->seeJson([
                'status' => 'fail',
             ]);
    }
    
    
    
    /**
     * Test to check forgotpassword
     *
     * @return void
     */
    public function testForgotPassword()
    {
       $last = User::max('id');
       $user = User::find($last);
       
       $this->post('api/v1/user/forgotpass', ['email' => 'check@asx.com'])
             ->seeJson([
                'status' => 'fail',
             ]); 
       $this->post('api/v1/user/forgotpass', ['email' => 'check@asc.com'])
             ->seeJson([
                'status' => 'fail',
             ]);
       $this->post('api/v1/user/forgotpass', ['email' => $user->email])
             ->seeJson([
                'status' => 'success',
             ]);
    }
    
    
    /**
     * Test to check resetpassword
     *
     * @return void
     */
    public function testResetPassword()
    {
       $result = DB::table('users')
                ->select('password_token')
                ->where('password_token','!=', '')->where('expire_on' , '>',new \DateTime())->first();
       $token = $result->password_token; 
     
       $this->post('api/v1/user/reset', ['token' => ' 12', 'password' => '123456'])
             ->seeJson([
                'status' => 'fail',
             ]);
       if($token){
            
       $this->post('api/v1/user/reset', ['token' => $token, 'password' => '123456'])
             ->seeJson([
                'status' => 'success',
             ]);
      
       }
       $this->post('api/v1/user/reset', ['token' => '123', 'password' => '123456'])
             ->seeJson([
                'status' => 'fail',
             ]);
    }
    
    
     /**
     * Test to check delete user
     *
     * @return void
     */
    public function testDeleteUser()
    {
       $email = str_random(5).'@ggg.com'; 
       $response = $this->call('POST', 'api/v1/manager', [
              'name'         => 'sample',
              'email'        => $email,
              'organization_pic'  =>'',
              'password'     => '123456',
              'mobile'       => '1234567',
              'orgname'      => str_random(5),
              'role'         => 'ROLE_MANAGER'
         ]);
        $this->assertEquals(200, $response->status());
        $last = User::max('id');
       
       $this->delete("api/v1/user/$last")
             ->seeJson([
                'status' => 'success',
             ]);
       $this->delete("api/v1/user/0")
             ->seeJson([
                'status' => 'fail',
             ]);
       $this->delete('api/v1/user/32323')
             ->seeJson([
                'status' => 'fail',
             ]);
    }
    
    
}
