From 40aa5d9737a518dac9fd56cdf916ed60846316e5 Mon Sep 17 00:00:00 2001 From: romanetar Date: Thu, 23 Jan 2025 16:07:23 +0100 Subject: [PATCH 1/2] feat: user creation endpoint Signed-off-by: romanetar --- .../Api/OAuth2/OAuth2UserApiController.php | 37 +++++++++++++++++++ database/seeds/ApiEndpointSeeder.php | 9 +++++ routes/api.php | 1 + tests/OAuth2UserUpdateApiTest.php | 35 ++++++++++++++++++ 4 files changed, 82 insertions(+) diff --git a/app/Http/Controllers/Api/OAuth2/OAuth2UserApiController.php b/app/Http/Controllers/Api/OAuth2/OAuth2UserApiController.php index 92c48241..2d7fb053 100644 --- a/app/Http/Controllers/Api/OAuth2/OAuth2UserApiController.php +++ b/app/Http/Controllers/Api/OAuth2/OAuth2UserApiController.php @@ -160,6 +160,39 @@ protected function curateUpdatePayload(array $payload): array ]); } + private function _create(){ + try { + + if(!Request::isJson()) return $this->error400(); + + $payload = Request::json()->all(); + // Creates a Validator instance and validates the data. + $validation = Validator::make($payload, UserValidationRulesFactory::build($payload)); + if ($validation->fails()) { + $ex = new ValidationException(); + throw $ex->setMessages($validation->messages()->toArray()); + } + + $user = $this->openid_user_service->create($payload); + + return $this->created(SerializerRegistry::getInstance()->getSerializer($user, SerializerRegistry::SerializerType_Private)->serialize()); + } + catch (ValidationException $ex1) + { + Log::warning($ex1); + return $this->error412($ex1->getMessages()); + } + catch (EntityNotFoundException $ex2) + { + Log::warning($ex2); + return $this->error404(['message' => $ex2->getMessage()]); + } + catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } + private function _update($id){ try { @@ -193,6 +226,10 @@ private function _update($id){ } } + public function create(){ + return $this->_create(); + } + public function updateMe(){ return $this->_update($this->resource_server_context->getCurrentUserId()); } diff --git a/database/seeds/ApiEndpointSeeder.php b/database/seeds/ApiEndpointSeeder.php index d3a9203e..bcc91bef 100644 --- a/database/seeds/ApiEndpointSeeder.php +++ b/database/seeds/ApiEndpointSeeder.php @@ -92,6 +92,15 @@ private function seedUsersEndpoints() \App\libs\OAuth2\IUserScopes::MeWrite ], ], + [ + 'name' => 'create-user', + 'active' => true, + 'route' => '/api/v1/users', + 'http_method' => 'POST', + 'scopes' => [ + \App\libs\OAuth2\IUserScopes::Write + ], + ], [ 'name' => 'update-user', 'active' => true, diff --git a/routes/api.php b/routes/api.php index cc1de425..0fc90ef4 100644 --- a/routes/api.php +++ b/routes/api.php @@ -27,6 +27,7 @@ Route::group(['prefix' => 'users'], function () { Route::get('', 'OAuth2UserApiController@getAll'); + Route::post('', 'OAuth2UserApiController@create'); Route::group(['prefix' => '{id}'], function () { Route::get('', 'OAuth2UserApiController@get'); Route::put('', 'OAuth2UserApiController@update'); diff --git a/tests/OAuth2UserUpdateApiTest.php b/tests/OAuth2UserUpdateApiTest.php index 9a3cefa4..becbf700 100644 --- a/tests/OAuth2UserUpdateApiTest.php +++ b/tests/OAuth2UserUpdateApiTest.php @@ -22,6 +22,41 @@ final class OAuth2UserUpdateApiTest extends OAuth2ProtectedApiTest { + public function testUserCreate() + { + $first_name = 'test_'. str_random(16); + + $data = [ + 'first_name' => $first_name, + 'last_name' => 'test_'. str_random(16), + 'email' => 'test_'. str_random(16) . '@test.com', + 'company' => 'test_'. str_random(16) + ]; + + $headers = [ + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ]; + + $response = $this->action + ( + "POST", + "Api\\OAuth2\\OAuth2UserApiController@create", + [], + [], + [], + [], + $headers, + json_encode($data) + ); + + $this->assertResponseStatus(201); + + $content = $response->getContent(); + $response = json_decode($content); + $this->assertTrue($response->first_name == $first_name); + } + public function testUserUpdate() { $user = EntityManager::getRepository(User::class)->findOneBy(['identifier' => 'sebastian.marcet']); From 2cc1ad5eb64072c979e3eaec2be12ae199a67c5e Mon Sep 17 00:00:00 2001 From: romanetar Date: Thu, 23 Jan 2025 16:54:40 +0100 Subject: [PATCH 2/2] fix: group test Signed-off-by: romanetar --- tests/OAuth2UserUpdateApiTest.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/OAuth2UserUpdateApiTest.php b/tests/OAuth2UserUpdateApiTest.php index becbf700..f3a95804 100644 --- a/tests/OAuth2UserUpdateApiTest.php +++ b/tests/OAuth2UserUpdateApiTest.php @@ -11,7 +11,10 @@ * See the License for the specific language governing permissions and * limitations under the License. **/ + +use App\libs\Auth\Models\IGroupSlugs; use App\libs\OAuth2\IUserScopes; +use Auth\Group; use Auth\User; use LaravelDoctrine\ORM\Facades\EntityManager; @@ -24,13 +27,16 @@ final class OAuth2UserUpdateApiTest extends OAuth2ProtectedApiTest { public function testUserCreate() { + $group_name = IGroupSlugs::RawUsersGroup; + $group = EntityManager::getRepository(Group::class)->findOneBy(['name' => $group_name]); $first_name = 'test_'. str_random(16); $data = [ 'first_name' => $first_name, 'last_name' => 'test_'. str_random(16), 'email' => 'test_'. str_random(16) . '@test.com', - 'company' => 'test_'. str_random(16) + 'company' => 'test_'. str_random(16), + 'groups' => [$group->getId()], ]; $headers = [ @@ -55,6 +61,8 @@ public function testUserCreate() $content = $response->getContent(); $response = json_decode($content); $this->assertTrue($response->first_name == $first_name); + $this->assertCount(1, $response->groups); + $this->assertEquals($group_name, $response->groups[0]); } public function testUserUpdate()