示例#1
0
        public async Task WhenConnectedMessageContainsClientId_AuthClientIdShouldBeTheSame()
        {
            // Arrange
            var options = new ClientOptions(ValidKey)
            {
                TransportFactory = new FakeTransportFactory(), SkipInternetCheck = true
            };
            var          mobileDevice = new FakeMobileDevice();
            var          realtime     = new AblyRealtime(options, mobileDevice: mobileDevice);
            const string newClientId  = "testId";

            var localDevice = realtime.Device;

            localDevice.ClientId.Should().BeNull();

            // Act
            realtime.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Connected)
            {
                ConnectionDetails = new ConnectionDetails {
                    ClientId = newClientId
                },
            });

            await realtime.WaitForState(ConnectionState.Connected);

            // Assert
            realtime.Auth.ClientId.Should().Be(newClientId);
            localDevice.ClientId.Should().Be(newClientId);
            mobileDevice.GetPreference(PersistKeys.Device.ClientId, PersistKeys.Device.SharedName).Should().Be(newClientId);
        }
示例#2
0
        internal async Task WhenClientIdChangesAfterRegisteringDevice_StateMachineShouldReceive_GotPushDeviceDetailsEvent()
        {
            // Arrange
            const string newClientId = "testId";

            var options = new ClientOptions(ValidKey)
            {
                TransportFactory = new FakeTransportFactory(), SkipInternetCheck = true
            };
            var mobileDevice = new FakeMobileDevice();

            async Task <AblyResponse> HandleRequestFunc(AblyRequest request)
            {
                if (request.Url.Contains("/push/deviceRegistrations"))
                {
                    return(new AblyResponse()
                    {
                        TextResponse = JObject.FromObject(new { clientId = newClientId, deviceIdentityToken = new { token = "token" } }).ToString()
                    });
                }

                return(new AblyResponse()
                {
                    TextResponse = "{}"
                });
            }

            var realtime = new AblyRealtime(options, (clientOptions, device) => GetRestClient(HandleRequestFunc, options, device), mobileDevice);

            // Setup the local device
            var localDevice = PushTestHelpers.GetRegisteredLocalDevice(realtime.RestClient);

            realtime.RestClient.Device = localDevice;
            localDevice.ClientId.Should().BeNull();

            realtime.Push.InitialiseStateMachine();

            var taskAwaiter  = new TaskCompletionAwaiter();
            var stateMachine = realtime.Push.StateMachine;

            stateMachine.CurrentState = new ActivationStateMachine.WaitingForPushDeviceDetails(stateMachine);

            // We trigger the GotPushDeviceDetails event
            await stateMachine.HandleEvent(new ActivationStateMachine.GotPushDeviceDetails());

            // From here we expect the stateMachine to move to WaitingForDeviceRegistration and try to register the Device
            // The registration will hit our mocked rest client above and return a localDevice with a new clientId.
            // Once the clientId is received we should expect to receive GotPushDeviceDetails event and the new clientId to be persisted
            realtime.Push.StateMachine.ProcessingEventCallback = @event =>
            {
                // Check we received the correct event
                @event.Should().BeOfType <ActivationStateMachine.GotPushDeviceDetails>();
                taskAwaiter.Done();
            };

            (await taskAwaiter).Should().BeTrue();
            mobileDevice.GetPreference(PersistKeys.Device.ClientId, PersistKeys.Device.SharedName).Should().Be(newClientId);
        }
示例#3
0
        internal async Task WhenClientIdChangesAfterInitialisation_StateMachineShouldReceive_GotPushDeviceDetailsEvent(Func <ActivationStateMachine, ActivationStateMachine.State> createCurrentState)
        {
            // Arrange
            const string initialClientId = "123";
            var          options         = new ClientOptions(ValidKey)
            {
                TransportFactory = new FakeTransportFactory(), SkipInternetCheck = true, ClientId = initialClientId
            };
            var          mobileDevice = new FakeMobileDevice();
            var          realtime     = new AblyRealtime(options, mobileDevice: mobileDevice);
            const string newClientId  = "testId";

            var localDevice = realtime.Device;

            // Make sure the LocalDevice is registered
            realtime.Device.DeviceIdentityToken = "token";
            localDevice.ClientId.Should().Be(initialClientId);
            // Initialise the activation statemachine and set a fake state to record the next event.
            realtime.Push.InitialiseStateMachine();
            var taskAwaiter = new TaskCompletionAwaiter();

            realtime.Push.StateMachine.CurrentState =
                createCurrentState(realtime.Push.StateMachine);
            realtime.Push.StateMachine.ProcessingEventCallback = @event =>
            {
                // Check we received the correct event
                @event.Should().BeOfType <ActivationStateMachine.GotPushDeviceDetails>();
                taskAwaiter.Done();
            };

            // Pretend we are connected and change the ClientId
            realtime.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Connected)
            {
                ConnectionDetails = new ConnectionDetails {
                    ClientId = newClientId
                },
            });

            await realtime.WaitForState(ConnectionState.Connected);

            // Check the clientId is set correctly
            realtime.Auth.ClientId.Should().Be(newClientId);
            localDevice.ClientId.Should().Be(newClientId);

            // It's necessary to pause the current thread and let the background action to complete which fires the event.
            await Task.Delay(100);

            (await taskAwaiter).Should().BeTrue();
            mobileDevice.GetPreference(PersistKeys.Device.ClientId, PersistKeys.Device.SharedName).Should().Be(newClientId);
        }
示例#4
0
        public async Task WithoutClientId_WhenAuthorizedWithTokenParamsWithClientId_ShouldUpdateLocalDeviceClientId()
        {
            const string newClientId  = "123";
            var          mobileDevice = new FakeMobileDevice();
            var          ably         = GetRestClient(
                handleRequestFunc: async request => new AblyResponse()
            {
                TextResponse = new TokenDetails("token").ToJson()
            },
                mobileDevice: mobileDevice);

            var localDevice = ably.Device;

            localDevice.ClientId.Should().BeNull();

            _ = await ably.Auth.AuthorizeAsync(new TokenParams { ClientId = newClientId });

            localDevice.ClientId.Should().Be(newClientId);
            mobileDevice.GetPreference(PersistKeys.Device.ClientId, PersistKeys.Device.SharedName).Should().Be(newClientId);
        }