Skip to content

Instantly share code, notes, and snippets.

@shwujiun
Last active October 6, 2015 08:54
Show Gist options
  • Save shwujiun/2d26eb1a9e7053be6929 to your computer and use it in GitHub Desktop.
Save shwujiun/2d26eb1a9e7053be6929 to your computer and use it in GitHub Desktop.
Test Always Failed
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Common\CommonTool;
use Illuminate\Http\Request;
use App\Models\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use App\Http\Requests\LoginRequest;
class AuthController extends Controller
{
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'getLogout']);
}
public function postLocalLogin(Request $request)
{
$bFailed = true;
$cus_id = $request->input('cus_id');
if (CommonTool::isLocal()) {
if (preg_match(config('define.CUSTOMER_ID'), $cus_id)) {
$user = User::where('DFA001', 'like', $cus_id)->first();
if (isset($user)) {
$bFailed = false;
$data['meta']['notice'][] = [
'level' => 0,
'detail' => config('apimessage.login_success'),
'path' => '/',
];
$data['data']['id'] = null;
$data['data']['type'] = 'customer';
$data['data']['attributes']['full_company'] = urlencode(
$user->DFA003
);
$data['data']['attributes']['short_company'] = urlencode(
$user->DFA002
);
return \Response::json($data);
}
}
}
else{
$data['errors']['detail'] = 'not local IP';
$bFailed = false;
return \Response::json($data);
}
if ($bFailed) {
$data['errors']['detail'] = config('apimessage.login_failed');
$data['errors']['path'] = null;
$data['errors']['fields']['field'] = $cus_id;
$data['errors']['fields']['detail'] = config(
'apimessage.login_without_id'
);
return \Response::json($data);
}
}
}
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Http\Controllers\Auth;
use \Mockery as m;
class LoginTest extends TestCase
{
public function mock($class)
{
$mock = m::mock($class);
$this->app->instance($class, $mock);
return $mock;
}
public function setUp()
{
parent::setUp();
Session::start();
}
public function tearDown()
{
m::close();
parent::tearDown();
}
public function testDeveloperLoginSuccess()
{
$checkLocalIpMock = $this->mock('App\Http\Common\CommonTool');
$checkLocalIpMock
->shouldReceive('isLocal')
->once()
->andReturn(true);
$this->post(
'api/v1/cid_login',
[
'cus_id' => 'AE000001',
'_token' => Session::token(),
])->seeJson(
[
'detail' => config('apimessage.login_success'),
]
);
}
}
<?php
Route::group(
['prefix' => 'api/v1'],
function ()
{
Route::post('cid_login', 'Auth\AuthController@postLocalLogin');
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment