From e6e487f312023cf765048921c6cd2497bd5bb9af Mon Sep 17 00:00:00 2001 From: GotPPay Date: Tue, 11 Sep 2018 10:46:16 +0200 Subject: [PATCH] add unit tests --- .../unit-tests/test-wiaas-rest-user-api.php | 83 ++++++++++++++++++- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-rest-user-api.php b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-rest-user-api.php index 578026a..80b7d85 100644 --- a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-rest-user-api.php +++ b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-rest-user-api.php @@ -7,17 +7,78 @@ class Wiass_REST_User_Api_Test extends Wiaas_Unit_Test_Case { + protected $server; + function setUp() { parent::setUp(); + + /** @var WP_REST_Server $wp_rest_server */ + global $wp_rest_server; + $this->server = $wp_rest_server = new \WP_REST_Server; + do_action( 'rest_api_init' ); } /** * @covers Wiass_REST_User_API::validate_token */ - function test_validate_token() { + function test_validate_token_as_guest() { + wp_set_current_user(0); + + $request = new WP_REST_Request( 'POST', '/wiaas/user/validate-token'); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertTrue($response->is_error()); + $this->assertEquals($response->get_status(), 401); + + $error_data = $response->as_error(); + $this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.'); + } + + /** + * @covers Wiass_REST_User_API::validate_token + */ + function test_validate_token_with_valid_user() { wp_set_current_user(1); - $this->assertTrue(false); + $request = new WP_REST_Request( 'POST', '/wiaas/user/validate-token'); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $user_info = $response->get_data()['userInfo']; + $this->assertNotNull($user_info); + $this->assertTrue(is_array($user_info)); + $this->assertArrayHasKey('wiaas_id_user', $user_info); + $this->assertArrayHasKey('wiaas_is_company_admin', $user_info); + $this->assertArrayHasKey('wiaas_user_full_name', $user_info); + $this->assertArrayHasKey('wiaas_user_type', $user_info); + $this->assertArrayHasKey('wiaas_username', $user_info); + $this->assertEquals($user_info['wiaas_id_user'], '1'); + $this->assertEquals($user_info['wiaas_is_company_admin'], '1'); + $this->assertEquals($user_info['wiaas_username'], 'admin'); + } + + /** + * @covers Wiass_REST_User_API::get_countries + */ + function test_get_countries_as_guest() { + wp_set_current_user(0); + + $request = new WP_REST_Request( 'GET', '/wiaas/user/get-countries'); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertTrue($response->is_error()); + $this->assertEquals($response->get_status(), 401); + + $error_data = $response->as_error(); + $this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.'); } /** @@ -26,6 +87,22 @@ class Wiass_REST_User_Api_Test extends Wiaas_Unit_Test_Case { function test_get_countries() { wp_set_current_user(1); - $this->assertTrue(false); + $request = new WP_REST_Request( 'GET', '/wiaas/user/get-countries'); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + $this->assertNotNull($data); + $this->assertTrue(is_array($data)); + + $country = $data[0]; + $this->assertNotNull($country); + $this->assertTrue(is_array($country)); + $this->assertArrayHasKey('idCountry', $country); + $this->assertArrayHasKey('countryName', $country); } }