# Copyright 2017-2022 The Wazo Authors  (see the AUTHORS file)
# SPDX-License-Identifier: GPL-3.0-or-later

import unittest

from uuid import uuid4
from unittest.mock import Mock

from xivo_bus.resources.paging_user.event import (
    PagingMemberUsersAssociatedEvent,
    PagingCallerUsersAssociatedEvent,
)
from xivo_dao.alchemy.userfeatures import UserFeatures as User
from xivo_dao.alchemy.paging import Paging

from ..notifier import PagingUserNotifier


class TestPagingUserNotifier(unittest.TestCase):
    def setUp(self):
        self.bus = Mock()
        self.user1 = Mock(User, uuid='abcd-1234')
        self.user2 = Mock(User, uuid='efgh-5678')
        self.paging = Mock(Paging, id=3, tenant_uuid=uuid4())
        self.expected_headers = {'tenant_uuid': str(self.paging.tenant_uuid)}

        self.notifier = PagingUserNotifier(self.bus)

    def test_associate_caller_then_bus_event(self):
        expected_event = PagingCallerUsersAssociatedEvent(
            self.paging.id, [self.user1.uuid, self.user2.uuid]
        )

        self.notifier.callers_associated(self.paging, [self.user1, self.user2])

        self.bus.send_bus_event.assert_called_once_with(
            expected_event, headers=self.expected_headers
        )

    def test_associate_member_then_bus_event(self):
        expected_event = PagingMemberUsersAssociatedEvent(
            self.paging.id, [self.user1.uuid, self.user2.uuid]
        )

        self.notifier.members_associated(self.paging, [self.user1, self.user2])

        self.bus.send_bus_event.assert_called_once_with(
            expected_event, headers=self.expected_headers
        )
