Skip to content
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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions tests/JwtTest.php
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()
Copy link
Member

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

{
$endpoint = 'http://127.0.0.1:8000';
Copy link
Member

Choose a reason for hiding this comment

The 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);

}


}
43 changes: 43 additions & 0 deletions tests/OrganizationTest.php
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);
}
}
74 changes: 74 additions & 0 deletions tests/ResourceTest.php
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);

// 准备测试数据
Copy link
Member

Choose a reason for hiding this comment

The 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);
}
}
32 changes: 32 additions & 0 deletions tests/SmsTest.php
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();

}
}
28 changes: 28 additions & 0 deletions tests/TokenTest.php
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());
}
}
127 changes: 127 additions & 0 deletions tests/UserTest.php
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);
}
}
Loading