Added some Link Invite Tests

This commit is contained in:
Lucas Bartholemy 2023-02-13 23:05:46 +01:00
parent 955c80b1b6
commit dbf39aa497
4 changed files with 198 additions and 1 deletions

View File

@ -166,6 +166,10 @@ class RegistrationController extends Controller
*/
protected function handleInviteByLinkRegistration($inviteToken, $spaceId, $setSpaceIdInSession = false)
{
if (empty(Yii::$app->getModule('user')->settings->get('auth.internalUsersCanInviteByLink'))) {
throw new HttpException(400, 'Invite by link is disabled!');
}
// If invited by link from a space
if ($spaceId !== null) {
$space = Space::findOne($spaceId);

View File

@ -622,7 +622,12 @@ class User extends ContentContainerActiveRecord implements IdentityInterface, Se
private function setUpApproved()
{
$spaceInviteId = Yii::$app->session->get(InviteForm::SESSION_SPACE_INVITE_ID);
$spaceInviteId = null;
if (!Yii::$app->request->isConsoleRequest) {
Yii::$app->session->get(InviteForm::SESSION_SPACE_INVITE_ID);
}
if ($spaceInviteId !== null) {
Yii::$app->session->remove(InviteForm::SESSION_SPACE_INVITE_ID);
} else {

View File

@ -0,0 +1,89 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2018 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace user\acceptance;
use user\AcceptanceTester;
class InviteLinkCest
{
/**
* @param AcceptanceTester $I
* @throws \Exception
*/
public function testLinkInvite(AcceptanceTester $I)
{
$I->wantTo('ensure that test link invite is working.');
// Enable Link Invite
$I->amAdmin();
$I->amOnPage('/admin/authentication');
$I->checkOption('#authenticationsettingsform-internaluserscaninvitebyemail');
$I->checkOption('#authenticationsettingsform-internaluserscaninvitebylink');
$I->click('Save');
// See Invite On People Page
$I->amUser2(true);
$I->amOnPage('/people');
$I->click('Invite');
$I->waitForText('Invite by link');
$I->click('Invite by link');
$link = $I->grabValueFrom('secureLink');
$I->logout();
$I->amOnUrl($link);
$I->waitForText('registration');
// See Invite on Space Page
// Dont See on Spaces
$I->amUser1();
$I->amOnSpace2();
$I->click('Invite');
$I->waitForText('Invite by link');
}
/**
* @param AcceptanceTester $I
* @throws \Exception
*/
public function testDisable(AcceptanceTester $I)
{
$I->wantTo('ensure that link invite is not shown when disabled.');
// Disable Link Invite
$I->amAdmin();
$I->amOnPage('/admin/authentication');
$I->checkOption('#authenticationsettingsform-internaluserscaninvitebyemail');
$I->uncheckOption('#authenticationsettingsform-internaluserscaninvitebylink');
$I->click('Save');
$I->amUser2(true);
$I->amOnPage('/people');
$I->click('Invite');
$I->waitForText('Send invite');
$I->dontSee('Invite by email');
// Dont See on Spaces
$I->amUser1(true);
$I->amOnSpace2();
$I->click('Invite');
$I->waitForText('Pick users');
$I->dontSee('Invite by link');
}
}

View File

@ -0,0 +1,99 @@
<?php
use user\FunctionalTester;
class LinkInviteCest
{
public function testDisabledLinkInvite(FunctionalTester $I)
{
$I->wantTo('ensure that invite by link is correctly disabled');
Yii::$app->getModule('user')->settings->set('auth.internalUsersCanInviteByLink', 0);
$inviteForm = new \humhub\modules\space\models\forms\InviteForm();
$inviteForm->space = \humhub\modules\space\models\Space::findOne(['name' => 'Space 2']);
$inviteUrl = $inviteForm->getInviteLink();
$I->amOnPage($inviteUrl);
$I->seeResponseCodeIs(400);
}
public function testInvalidToken(FunctionalTester $I)
{
$I->wantTo('ensure that invite by link is without valid token');
Yii::$app->getModule('user')->settings->set('auth.internalUsersCanInviteByLink', 1);
$I->amOnRoute('/user/registration/by-link', ['token' => 'abcd', 'spaceId' => 1]);
$I->seeResponseCodeIs(404);
}
public function testValidTokenDifferentSpaceId(FunctionalTester $I)
{
$I->wantTo('ensure that invite by link is with valid token and different space ID');
Yii::$app->getModule('user')->settings->set('auth.internalUsersCanInviteByLink', 1);
// Generate Token
$space = \humhub\modules\space\models\Space::findOne(['name' => 'Space 2']);
$inviteForm = new \humhub\modules\space\models\forms\InviteForm();
$inviteForm->space = $space;
$inviteUrl = $inviteForm->getInviteLink();
$I->amOnRoute('/user/registration/by-link', ['token' => $space->settings->get('inviteToken'), 'spaceId' => $space->id]);
$I->seeResponseCodeIs(200);
$I->amOnRoute('/user/registration/by-link', ['token' => $space->settings->get('inviteToken'), 'spaceId' => 1]);
$I->seeResponseCodeIs(404);
}
public function testSpaceInvite(FunctionalTester $I)
{
$I->wantTo('ensure that invited users become member of the space');
Yii::$app->getModule('user')->settings->set('auth.internalUsersCanInviteByLink', 1);
$inviteForm = new \humhub\modules\space\models\forms\InviteForm();
$inviteForm->space = \humhub\modules\space\models\Space::findOne(['name' => 'Space 2']);
$inviteUrl = $inviteForm->getInviteLink();
$I->amOnPage($inviteUrl);
$I->see('registration');
$I->fillField('#register-email', 'text@example.com');
$I->click('Register');
$I->see('successful');
$messages = $I->grabSentEmails();
if (!array_key_exists('text@example.com', $messages[0]->getTo())) {
$I->see('text@example.com not in mails');
}
$token = $I->fetchInviteToken($messages[0]);
$I->amOnRoute('/user/registration', ['token' => $token]);
$I->see('Account registration');
$I->fillField('User[username]', 'NewUser');
$I->fillField('Password[newPassword]', 'NewUser123');
$I->fillField('Password[newPasswordConfirm]', 'NewUser123');
$I->fillField('Profile[firstname]', 'New');
$I->fillField('Profile[lastname]', 'User');
$I->click('#registration-form [type="submit"]');
$I->see('Dashboard');
$userId = \humhub\modules\user\models\User::findOne(['username' => 'NewUser']);
$space = \humhub\modules\space\models\Space::findOne(['name' => 'Space 2']);
if (!$space->isMember($userId)) {
$I->see('User is not member of invited Space!');
}
}
}