-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add test cases for all objects and all operations #41
Open
JiawenZou
wants to merge
2
commits into
casdoor:master
Choose a base branch
from
JiawenZou:zoujiawen
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Casdoor\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Casdoor\Auth\AuthConfig; | ||
use Casdoor\Auth\Jwt; | ||
|
||
class JwtTest extends TestCase | ||
{ | ||
|
||
|
||
public function initConfig() | ||
{ | ||
$endpoint = 'http://127.0.0.1:8000'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this work? Have you put this into test CI? |
||
$clientId = 'c64b12723aefb65a88ce'; | ||
$clientSecret = 'c0c9d483a87332751b2564635765d71c9f6a2e83'; | ||
$jwtSecret = file_get_contents(dirname(__FILE__) . '/public_key.pem'); | ||
$organizationName = 'built-in'; | ||
$applicationName = 'testApp'; | ||
User::initConfig($endpoint, $clientId, $clientSecret, $jwtSecret, $organizationName, $applicationName); | ||
} | ||
|
||
public function testParseJwtToken() | ||
{ | ||
$this->initConfig(); | ||
|
||
$token = $this->createTestJwtToken(); | ||
|
||
$token = new Token(); | ||
$accessToken = $token->getOAuthToken($this->code, ''); | ||
$token = $accessToken->getToken(); | ||
|
||
$jwt = new Jwt(); | ||
|
||
$result = $jwt->parseJwtToken($token, User::$authConfig); | ||
|
||
$this->assertIsArray($result); | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Casdoor\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Casdoor\Auth\AuthConfig; | ||
use Casdoor\Auth\Organization; | ||
|
||
class OrganizationTest extends TestCase | ||
{ | ||
protected $authConfig; | ||
|
||
public function initConfig() | ||
{ | ||
$endpoint = 'http://127.0.0.1:8000'; | ||
$clientId = 'c64b12723aefb65a88ce'; | ||
$clientSecret = 'c0c9d483a87332751b2564635765d71c9f6a2e83'; | ||
$jwtSecret = file_get_contents(dirname(__FILE__) . '/public_key.pem'); | ||
$organizationName = 'built-in'; | ||
$applicationName = 'testApp'; | ||
User::initConfig($endpoint, $clientId, $clientSecret, $jwtSecret, $organizationName, $applicationName); | ||
} | ||
|
||
public function testAddOrganization() | ||
{ | ||
$organization = new Organization('test_owner', 'test_organization'); | ||
|
||
$organizationHandler = new Organization(); | ||
|
||
$result = $organizationHandler->addOrganization($organization, User::$authConfig); | ||
|
||
$this->assertIsBool($result); | ||
} | ||
|
||
public function testDeleteOrganization() | ||
{ | ||
$organizationHandler = new Organization(); | ||
|
||
$result = $organizationHandler->deleteOrganization('test_organization', User::$authConfig); | ||
|
||
$this->assertIsBool($result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Casdoor\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Casdoor\Auth\AuthConfig; | ||
use Casdoor\Auth\Resource; | ||
|
||
class ResourceTest extends TestCase | ||
{ | ||
public function initConfig() | ||
{ | ||
$endpoint = 'http://127.0.0.1:8000'; | ||
$clientId = 'c64b12723aefb65a88ce'; | ||
$clientSecret = 'c0c9d483a87332751b2564635765d71c9f6a2e83'; | ||
$jwtSecret = file_get_contents(dirname(__FILE__) . '/public_key.pem'); | ||
$organizationName = 'built-in'; | ||
$applicationName = 'testApp'; | ||
User::initConfig($endpoint, $clientId, $clientSecret, $jwtSecret, $organizationName, $applicationName); | ||
} | ||
|
||
|
||
public function testUploadResource() | ||
{ | ||
|
||
$this->initConfig(); | ||
|
||
$resourceHandler = new Resource('test_owner', 'test_resource', User::$authConfig); | ||
|
||
// 准备测试数据 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All should be English, remove it |
||
$tag = 'test_tag'; | ||
$parent = 'test_parent'; | ||
$fullFilePath = 'test_file_path'; | ||
$fileBytes = ['test_file_bytes']; | ||
|
||
|
||
$result = $resourceHandler->uploadResource($tag, $parent, $fullFilePath, $fileBytes); | ||
|
||
|
||
$this->assertIsArray($result); | ||
} | ||
|
||
public function testUploadResourceEx() | ||
{ | ||
|
||
$this->initConfig(); | ||
$resourceHandler = new Resource('test_owner', 'test_resource', User::$authConfig); | ||
|
||
$user = 'test_user'; | ||
$tag = 'test_tag'; | ||
$parent = 'test_parent'; | ||
$fullFilePath = 'test_file_path'; | ||
$fileBytes = ['test_file_bytes']; | ||
$createdTime = '2024-03-16'; | ||
$description = 'Test description'; | ||
|
||
$result = $resourceHandler->uploadResourceEx($user, $tag, $parent, $fullFilePath, $fileBytes, $createdTime, $description); | ||
|
||
|
||
$this->assertIsArray($result); | ||
} | ||
|
||
public function testDeleteResource() | ||
{ | ||
|
||
$this->initConfig(); | ||
$resourceHandler = new Resource('test_owner', 'test_resource', User::$authConfig); | ||
|
||
|
||
$result = $resourceHandler->deleteResource(); | ||
|
||
$this->assertIsBool($result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Casdoor\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Casdoor\Auth\AuthConfig; | ||
use Casdoor\Auth\Sms; | ||
|
||
class SmsTest extends TestCase | ||
{ | ||
public function initConfig() | ||
{ | ||
$endpoint = 'http://127.0.0.1:8000'; | ||
$clientId = 'c64b12723aefb65a88ce'; | ||
$clientSecret = 'c0c9d483a87332751b2564635765d71c9f6a2e83'; | ||
$jwtSecret = file_get_contents(dirname(__FILE__) . '/public_key.pem'); | ||
$organizationName = 'built-in'; | ||
$applicationName = 'testApp'; | ||
User::initConfig($endpoint, $clientId, $clientSecret, $jwtSecret, $organizationName, $applicationName); | ||
} | ||
|
||
public function testSendSms() | ||
{ | ||
|
||
$this->initConfig(); | ||
$smsHandler = new Sms( User::$authConfig, 'Content', '838625448'); | ||
|
||
$this->expectException(\Casdoor\Exceptions\CasdoorException::class); | ||
$smsHandler->sendSms(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Casdoor\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Casdoor\Auth\Token; | ||
|
||
class TokenTest extends TestCase | ||
{ | ||
public function testGetOAuthToken() | ||
{ | ||
|
||
$endpoint = 'http://127.0.0.1:8000'; | ||
$clientId = 'c64b12723aefb65a88ce'; | ||
$clientSecret = 'c0c9d483a87332751b2564635765d71c9f6a2e83'; | ||
$code = "e3bc886294f2b43b8a1a"; | ||
|
||
$token = new Token(); | ||
|
||
$accessToken = $token->getOAuthToken($code, ''); | ||
|
||
$this->assertInstanceOf(\League\OAuth2\Client\Token\AccessTokenInterface::class, $accessToken); | ||
|
||
$this->assertIsString($accessToken->getToken()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
namespace Casdoor\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Casdoor\Auth\User; | ||
|
||
|
||
class UserTest extends TestCase | ||
{ | ||
|
||
public function initConfig() | ||
{ | ||
$endpoint = 'http://127.0.0.1:8000'; | ||
$clientId = 'c64b12723aefb65a88ce'; | ||
$clientSecret = 'c0c9d483a87332751b2564635765d71c9f6a2e83'; | ||
$jwtSecret = file_get_contents(dirname(__FILE__) . '/public_key.pem'); | ||
$organizationName = 'built-in'; | ||
$applicationName = 'testApp'; | ||
User::initConfig($endpoint, $clientId, $clientSecret, $jwtSecret, $organizationName, $applicationName); | ||
} | ||
|
||
public function testAddUser() | ||
{ | ||
$this->initConfig(); | ||
|
||
$user = new User(); | ||
$user->name = 'testUser'; | ||
$user->owner = 'testOwner'; | ||
$response = $user->addUser($user); | ||
|
||
$this->assertTrue($response); | ||
} | ||
|
||
public function testGetUser() | ||
{ | ||
|
||
$this->initConfig(); | ||
$user = User::getUser('testUser'); | ||
|
||
$this->assertIsArray($user); | ||
$this->assertEquals('testUser', $user['name']); | ||
} | ||
|
||
public function testUpdateUser() | ||
{ | ||
$this->initConfig(); | ||
$user = User::getUser('testUser'); | ||
$user['displayName'] = 'Updated Test User'; | ||
$response = User::updateUserForColumns($user, ['displayName']); | ||
|
||
$this->assertTrue($response); | ||
} | ||
|
||
public function testDeleteUser() | ||
{ | ||
$this->initConfig(); | ||
$user = new User(); | ||
$user->name = 'testUser'; | ||
$response = $user->deleteUser($user); | ||
|
||
$this->assertTrue($response); | ||
} | ||
|
||
|
||
public function testGetUserByEmail() | ||
{ | ||
$this->initConfig(); | ||
$email = '[email protected]'; | ||
$user = User::getUserByEmail($email); | ||
|
||
$this->assertIsArray($user); | ||
} | ||
|
||
public function testCheckUserPassword() | ||
{ | ||
$this->initConfig(); | ||
$user = new User(); | ||
$user->name = 'test_user'; | ||
$user->password = 'test_password'; | ||
$isValidPassword = $user->checkUserPassword($user); | ||
|
||
$this->assertTrue($isValidPassword); | ||
} | ||
|
||
public function testGetSortedUsers() | ||
{ | ||
$this->initConfig(); | ||
$sorter = 'displayName'; | ||
$limit = 10; | ||
$users = User::getSortedUsers($sorter, $limit); | ||
|
||
$this->assertIsArray($users); | ||
} | ||
|
||
public function testModifyUser() | ||
{ | ||
$this->initConfig(); | ||
$user = new User(); | ||
$user->name = 'test_user'; | ||
|
||
$response = $user->addUser($user); | ||
$this->assertTrue($response); | ||
|
||
$user->displayName = 'Updated Name'; | ||
$response = $user->updateUser($user); | ||
$this->assertTrue($response); | ||
|
||
$response = $user->deleteUser($user); | ||
$this->assertTrue($response); | ||
} | ||
|
||
public function testGetUsers() | ||
{ | ||
$this->initConfig(); | ||
$users = User::getUsers(); | ||
$this->assertIsArray($users); | ||
} | ||
|
||
public function testGetUserCount() | ||
{ | ||
$this->initConfig(); | ||
$isOnline = 1; | ||
$count = User::getUserCount($isOnline); | ||
$this->assertIsInt($count); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initConfig() should be defined only once