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

Start with integration tests for invites #6427

Merged
merged 5 commits into from
Nov 10, 2021
Merged
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
23 changes: 23 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use OCA\Talk\Events\AttendeesRemovedEvent;
use OCA\Talk\Events\ChatEvent;
use OCA\Talk\Events\RoomEvent;
use OCA\Talk\Federation\CloudFederationProviderTalk;
use OCA\Talk\Files\Listener as FilesListener;
use OCA\Talk\Files\TemplateLoader as FilesTemplateLoader;
use OCA\Talk\Flow\RegisterOperationsListener;
Expand Down Expand Up @@ -79,12 +80,16 @@
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\IAppContainer;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Collaboration\Resources\IProviderManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Federation\ICloudFederationProvider;
use OCP\Federation\ICloudFederationProviderManager;
use OCP\Group\Events\GroupDeletedEvent;
use OCP\Group\Events\UserAddedEvent;
use OCP\Group\Events\UserRemovedEvent;
use OCP\IConfig;
use OCP\IServerContainer;
use OCP\IUser;
use OCP\Security\CSP\AddContentSecurityPolicyEvent;
Expand Down Expand Up @@ -166,6 +171,7 @@ public function boot(IBootContext $context): void {

$this->registerRoomActivityHooks($dispatcher);
$this->registerChatHooks($dispatcher);
$context->injectFn(\Closure::fromCallable([$this, 'registerCloudFederationProviderManager']));
}

protected function registerNotifier(IServerContainer $server): void {
Expand Down Expand Up @@ -226,4 +232,21 @@ protected function registerChatHooks(IEventDispatcher $dispatcher): void {
};
$dispatcher->addListener(Room::EVENT_AFTER_ROOM_DELETE, $listener);
}

protected function registerCloudFederationProviderManager(
IConfig $config,
ICloudFederationProviderManager $manager,
IAppContainer $appContainer): void {
if ($config->getAppValue('spreed', 'federation_enabled', 'no') !== 'yes') {
return;
}

$manager->addCloudFederationProvider(
'talk-room',
'Talk Federation',
static function () use ($appContainer): ICloudFederationProvider {
return $appContainer->get(CloudFederationProviderTalk::class);
}
);
}
}
8 changes: 8 additions & 0 deletions lib/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ public function isSIPConfigured(): bool {
&& $this->getDialInInfo() !== '';
}

/**
* Determine if Talk federation is enabled on this instance
*/
public function isFederationEnabled(): bool {
// TODO: Set to default true once implementation is complete
return $this->config->getAppValue('spreed', 'federation_enabled', 'no') === 'yes';
}

public function getDialInInfo(): string {
return $this->config->getAppValue('spreed', 'sip_bridge_dialin_info');
}
Expand Down
20 changes: 13 additions & 7 deletions lib/Controller/RoomController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
use OCA\Talk\Exceptions\ParticipantNotFoundException;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Exceptions\UnauthorizedException;
use OCA\Talk\Federation\FederationManager;
use OCA\Talk\GuestManager;
use OCA\Talk\Manager;
use OCA\Talk\MatterbridgeManager;
Expand Down Expand Up @@ -67,6 +66,7 @@
use OCP\User\Events\UserLiveStatusEvent;
use OCP\UserStatus\IManager as IUserStatusManager;
use OCP\UserStatus\IUserStatus;
use Psr\Log\LoggerInterface;

class RoomController extends AEnvironmentAwareController {
public const EVENT_BEFORE_ROOMS_GET = self::class . '::preGetRooms';
Expand Down Expand Up @@ -109,8 +109,8 @@ class RoomController extends AEnvironmentAwareController {
protected $config;
/** @var Config */
protected $talkConfig;
/** @var FederationManager */
protected $federationManager;
/** @var LoggerInterface */
protected $logger;

/** @var array */
protected $commonReadMessages = [];
Expand All @@ -135,7 +135,8 @@ public function __construct(string $appName,
IL10N $l10n,
IConfig $config,
Config $talkConfig,
ICloudIdManager $cloudIdManager) {
ICloudIdManager $cloudIdManager,
LoggerInterface $logger) {
parent::__construct($appName, $request);
$this->session = $session;
$this->appManager = $appManager;
Expand All @@ -156,6 +157,7 @@ public function __construct(string $appName,
$this->config = $config;
$this->talkConfig = $talkConfig;
$this->cloudIdManager = $cloudIdManager;
$this->logger = $logger;
}

protected function getTalkHashHeader(): array {
Expand Down Expand Up @@ -1129,13 +1131,16 @@ public function addParticipantToRoom(string $newParticipant, string $source = 'u
$this->guestManager->sendEmailInvitation($this->room, $participant);

return new DataResponse($data);
} elseif ($source === 'remote') {
if (!$this->federationManager->isEnabled()) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
} elseif ($source === 'remotes') {
if (!$this->talkConfig->isFederationEnabled()) {
return new DataResponse([], Http::STATUS_NOT_IMPLEMENTED);
}
try {
$newUser = $this->cloudIdManager->resolveCloudId($newParticipant);
} catch (\InvalidArgumentException $e) {
$this->logger->error($e->getMessage(), [
'exception' => $e,
]);
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}

Expand All @@ -1145,6 +1150,7 @@ public function addParticipantToRoom(string $newParticipant, string $source = 'u
'displayName' => $newUser->getDisplayId(),
];
} else {
$this->logger->error('Trying to add participant from unsupported source ' . $source);
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/Federation/FederationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ public function __construct(
/**
* Determine if Talk federation is enabled on this instance
* @return bool
* @deprecated use \OCA\Talk\Config::isFederationEnabled()
*/
public function isEnabled(): bool {
// TODO: Set to default true once implementation is complete
return $this->config->getAppValue(Application::APP_ID, 'federation_enabled', 'false') === 'true';
return $this->config->getAppValue(Application::APP_ID, 'federation_enabled', 'no') === 'yes';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Federation/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public function sendUpdateToRemote(string $remote, ICloudFederationNotification
}

private function prepareRemoteUrl(string $remote): string {
if ($this->addressHandler->urlContainProtocol($remote)) {
if (!$this->addressHandler->urlContainProtocol($remote)) {
return 'https://' . $remote;
}
return $remote;
Expand Down
5 changes: 5 additions & 0 deletions lib/TInitialState.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ protected function publishInitialStateShared(): void {
'grid_videos_limit_enforced',
$this->talkConfig->getGridVideosLimitEnforced()
);

$this->initialState->provideInitialState(
'federation_enabled',
$this->talkConfig->isFederationEnabled()
);
}

protected function publishInitialStateForUser(IUser $user, IRootFolder $rootFolder, IAppManager $appManager): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@
:items="addableCircles"
@click="handleClickParticipant" />
</template>

<template v-if="addableRemotes.length !== 0">
<AppNavigationCaption
:title="t('spreed', 'Add federated users')" />
<ParticipantsList
:items="addableRemotes"
@click="handleClickParticipant" />
</template>
<AppNavigationCaption v-if="sourcesWithoutResults"
:title="sourcesWithoutResultsList" />
<Hint v-if="contactsLoading" :hint="t('spreed', 'Searching …')" />
Expand Down Expand Up @@ -214,6 +222,12 @@ export default {
}
return []
},
addableRemotes() {
if (this.searchResults !== []) {
return this.searchResults.filter((item) => item.source === 'remotes')
}
return []
},
// Determines whether this component is used in the new group conversation creation
// context
isNewGroupConversation() {
Expand Down
5 changes: 5 additions & 0 deletions src/services/conversationsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

import axios from '@nextcloud/axios'
import { loadState } from '@nextcloud/initial-state'
import { generateOcsUrl } from '@nextcloud/router'
import { CONVERSATION, SHARE } from '../constants'

Expand Down Expand Up @@ -80,6 +81,10 @@ const searchPossibleConversations = async function({ searchText, token, onlyUser
shareTypes.push(SHARE.TYPE.CIRCLE)
if (token !== 'new') {
shareTypes.push(SHARE.TYPE.EMAIL)

if (loadState('spreed', 'federation_enabled')) {
shareTypes.push(SHARE.TYPE.REMOTE)
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/services/conversationsService.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import mockAxios from '../__mocks__/axios'
import { generateOcsUrl } from '@nextcloud/router'
import { loadState } from '@nextcloud/initial-state'
import { searchPossibleConversations } from './conversationsService'
import { SHARE } from '../constants'

jest.mock('@nextcloud/initial-state', () => ({
loadState: jest.fn(),
}))

describe('conversationsService', () => {
let loadStateSettings

beforeEach(() => {
loadStateSettings = {
federation_enabled: false,
}

loadState.mockImplementation((app, key) => {
if (app === 'spreed') {
return loadStateSettings[key]
}
return null
})
})

afterEach(() => {
// cleaning up the mess left behind the previous test
mockAxios.reset()
Expand Down
14 changes: 13 additions & 1 deletion tests/integration/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ public function userSeesAttendeesInRoom(string $user, string $identifier, int $s
if (isset($attendee['actorId']) && substr($attendee['actorId'], 0, strlen('"guest')) === '"guest') {
$attendee['actorId'] = sha1(self::$userToSessionId[trim($attendee['actorId'], '"')]);
}

if (isset($attendee['actorId'], $attendee['actorType']) && $attendee['actorType'] === 'federated_users') {
$attendee['actorId'] .= '@' . rtrim($this->baseUrl, '/');
}

if (isset($attendee['participantType'])) {
$attendee['participantType'] = (string)$this->mapParticipantTypeTestInput($attendee['participantType']);
}
Expand Down Expand Up @@ -1069,7 +1074,7 @@ public function userChangesListableScopeOfTheRoom(string $user, string $identifi
}

/**
* @Then /^user "([^"]*)" adds (user|group|email|circle) "([^"]*)" to room "([^"]*)" with (\d+) \((v4)\)$/
* @Then /^user "([^"]*)" adds (user|group|email|circle|remote) "([^"]*)" to room "([^"]*)" with (\d+) \((v4)\)$/
*
* @param string $user
* @param string $newType
Expand All @@ -1080,6 +1085,11 @@ public function userChangesListableScopeOfTheRoom(string $user, string $identifi
*/
public function userAddAttendeeToRoom(string $user, string $newType, string $newId, string $identifier, int $statusCode, string $apiVersion): void {
$this->setCurrentUser($user);

if ($newType === 'remote') {
$newId .= '@' . $this->baseUrl;
}

$this->sendRequest(
'POST', '/apps/spreed/api/' . $apiVersion . '/room/' . self::$identifierToToken[$identifier] . '/participants',
new TableNode([
Expand Down Expand Up @@ -2108,6 +2118,8 @@ public function sendRequest($verb, $url, $body = null, array $headers = []) {
$this->response = $client->{$verb}($fullUrl, $options);
} catch (ClientException $ex) {
$this->response = $ex->getResponse();
} catch (\GuzzleHttp\Exception\ServerException $ex) {
$this->response = $ex->getResponse();
}
}

Expand Down
27 changes: 27 additions & 0 deletions tests/integration/features/federation/invite.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Feature: federation/invite
Background:
Given user "participant1" exists
Given user "participant2" exists

Scenario: federation is disabled
Given the following app config is set
| federation_enabled | no |
Given user "participant1" creates room "room" (v4)
| roomType | 3 |
| roomName | room |
And user "participant1" adds remote "participant2" to room "room" with 501 (v4)
When user "participant1" sees the following attendees in room "room" with 200 (v4)
| actorType | actorId | participantType |
| users | participant1 | 1 |

Scenario: federation is enabled
Given the following app config is set
| federation_enabled | yes |
Given user "participant1" creates room "room" (v4)
| roomType | 3 |
| roomName | room |
And user "participant1" adds remote "participant2" to room "room" with 200 (v4)
When user "participant1" sees the following attendees in room "room" with 200 (v4)
| actorType | actorId | participantType |
| users | participant1 | 1 |
| federated_users | participant2 | 3 |
10 changes: 9 additions & 1 deletion tests/php/Federation/FederationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function testSendRemoteShareWithOwner() {
$providerId = '3';
$roomId = 5;
$token = 'abcdefghijklmno';
$shareWith = '[email protected]';
$shareWith = 'test@https://remote.test.local';
$name = 'abcdefgh';
$owner = 'Owner\'s name';
$ownerId = 'owner';
Expand Down Expand Up @@ -321,6 +321,10 @@ public function testSendAcceptNotification() {
->with($remote, $notification)
->willReturn([]);

$this->addressHandler->method('urlContainProtocol')
->with($remote)
->willReturn(true);

$success = $this->notifications->sendShareAccepted($remote, $id, $token);

$this->assertEquals(true, $success);
Expand Down Expand Up @@ -354,6 +358,10 @@ public function testSendRejectNotification() {
->with($remote, $notification)
->willReturn([]);

$this->addressHandler->method('urlContainProtocol')
->with($remote)
->willReturn(true);

$success = $this->notifications->sendShareDeclined($remote, $id, $token);

$this->assertEquals(true, $success);
Expand Down