Esempio n. 1
0
        public async Task ShouldGetTokenIfAutenticateWithQrCodeIsCalledWithoutLoginMade()
        {
            MockDiscoveryRequest();
            MockDiscoveryAppRequest();

            MockLoginRequestWithouEventsUrl();
            var nubankClient = new Nubank(_mockRestClient.Object, "login", "password");

            var code = Guid.NewGuid().ToString();

            MockLiftRequest(code);

            await nubankClient.AutenticateWithQrCode(code);

            var expectedPayload = new
            {
                qr_code_id = code,
                type       = "login-webapp"
            };

            _mockRestClient.Verify(x => x.PostAsync <Dictionary <string, object> >(
                                       "lift_url",
                                       It.Is <object>(o => o.GetHashCode() == expectedPayload.GetHashCode()),
                                       It.Is <Dictionary <string, string> >(dictionary => IsValidAuthorizationHeader(dictionary))
                                       ), Times.Once());
        }
Esempio n. 2
0
        public async Task ShouldGetEvents()
        {
            MockDiscoveryRequest();

            MockLoginRequest();

            var getEventsResponse = new GetEventsResponse
            {
                Events = _events
            };

            _mockRestClient
            .Setup(x => x.GetAsync <GetEventsResponse>(
                       It.IsAny <string>(),
                       It.IsAny <Dictionary <string, string> >()
                       ))
            .ReturnsAsync(getEventsResponse);

            var nubankClient = new Nubank(_mockRestClient.Object, "login", "password");
            await nubankClient.Login();

            var actualEvents = await nubankClient.GetEvents();

            Assert.Equal(_events, actualEvents);
            _mockRestClient.Verify(x => x.GetAsync <GetEventsResponse>(
                                       It.IsAny <string>(),
                                       It.IsAny <Dictionary <string, string> >()
                                       ), Times.Once());
        }
Esempio n. 3
0
        static async Task Main(string[] args)
        {
            Console.WriteLine("Nubank Client");
            Console.WriteLine("Please, type your login (CPF):");
            var login = Console.ReadLine().Trim();

            Console.WriteLine("Type your password:"******"You must authenticate with your phone to be able to access your data.");
                Console.WriteLine("Scan the QRCode below with you Nubank application on the following menu:");
                Console.WriteLine("Nu(Seu Nome) > Perfil > Acesso pelo site");
                Console.WriteLine();
                Console.Write(result.GetQrCodeAsAscii());
                Console.WriteLine();
                Console.ReadKey();

                await nubankClient.AutenticateWithQrCode(result.Code);
            }
            var events = await nubankClient.GetEvents();

            ConsoleTable
            .From <Event>(events)
            .Write(Format.Alternative);

            Console.ReadKey();
        }
Esempio n. 4
0
 private async Task <LoginResponse> LoginOrThrow(Nubank nubankClient)
 {
     try
     {
         return(await nubankClient.LoginAsync());
     }
     catch (Exception e)
     {
         throw new UnauthorizedException();
     }
 }
Esempio n. 5
0
        public async Task ShouldReturnLoginResponseWithNeedsDeviceAuthorizationWhenEventsNotPresentInLoginResponse()
        {
            MockDiscoveryRequest();

            MockLoginRequestWithouEventsUrl();

            var nubankClient  = new Nubank(_mockRestClient.Object, "login", "password");
            var loginResponse = await nubankClient.Login();

            Assert.True(loginResponse.NeedsDeviceAuthorization);
            Assert.NotNull(loginResponse.Code);
        }
Esempio n. 6
0
        public async Task <AuthenticationToken> Login(LoginCredentials login)
        {
            if (!_nubankInstances.TryGetValue(login.CPF, out _))
            {
                var nubankClient = new Nubank(login.CPF, login.Password);
                var loginResult  = await LoginOrThrow(nubankClient);

                if (_nubankInstances.TryAddValue(login.CPF, nubankClient))
                {
                    return(GetToken(login.CPF, loginResult.Code));
                }
            }

            throw new BadRequestException();
        }
Esempio n. 7
0
        static async Task Main()
        {
            Console.WriteLine("Nubank Client");
            Console.WriteLine("Please, type your login (CPF):");
            var login = Console.ReadLine().Trim();

            Console.WriteLine("Type your password:"******"You must authenticate with your phone to be able to access your data.");
                Console.WriteLine("Scan the QRCode below with you Nubank application on the following menu:");
                Console.WriteLine("Nu(Seu Nome) > Perfil > Acesso pelo site");
                Console.WriteLine();

                Console.WriteLine(result.GetQrCodeAsAscii());
                Console.WriteLine($"Use your phone to scan and after this press any key to continue...");
                Console.ReadKey();

                await nubankClient.AutenticateWithQrCodeAsync(result.Code);
            }

            try
            {
                var savings = await nubankClient.GetSavingsAsync();

                ConsoleTable
                .From(savings)
                .Write(Format.Alternative);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            var events = await nubankClient.GetEventsAsync();

            ConsoleTable
            .From(events)
            .Write(Format.Alternative);

            Console.ReadKey();
        }
Esempio n. 8
0
        public async Task ShouldThrowExceptionWithGenericMessageWhenLoginFailedAndErrorIsNotPresent()
        {
            MockDiscoveryRequest();

            const string errorMessage = "Unknow error occurred on trying to do login on Nubank using the entered credentials";

            var loginData = new Dictionary <string, object>();

            _mockRestClient
            .Setup(x => x.PostAsync <Dictionary <string, object> >(It.IsAny <string>(), It.IsAny <object>()))
            .ReturnsAsync(loginData);

            var nubankClient = new Nubank(_mockRestClient.Object, "login", "password");
            var exception    = await Assert.ThrowsAsync <AuthenticationException>(
                async() => await nubankClient.LoginAsync()
                );

            Assert.Equal(errorMessage, exception.Message);
        }
Esempio n. 9
0
        public async Task ShouldGetSavings()
        {
            MockDiscoveryRequest();

            MockLoginRequest();

            var getSavingsResponse = new GetSavingsResponse()
            {
                Data = new DataResponse()
                {
                    Viewer = new ViewerResponse()
                    {
                        SavingsAccount = new SavingsAccount()
                        {
                            Feed = _savings
                        }
                    }
                }
            };

            _mockRestClient
            .Setup(x => x.PostAsync <GetSavingsResponse>(
                       It.IsAny <string>(),
                       It.IsAny <object>(),
                       It.IsAny <Dictionary <string, string> >()
                       ))
            .ReturnsAsync(getSavingsResponse);

            var nubankClient = new Nubank(_mockRestClient.Object, "login", "password");
            await nubankClient.LoginAsync();

            var actualSavings = await nubankClient.GetSavingsAsync();

            Assert.Equal(_savings, actualSavings);

            _mockRestClient.Verify(x => x.PostAsync <GetSavingsResponse>(
                                       It.IsAny <string>(),
                                       It.IsAny <object>(),
                                       It.IsAny <Dictionary <string, string> >()
                                       ), Times.Once());
        }
Esempio n. 10
0
        public async Task ShouldThrowExceptionWhenLoginWasNotCalled()
        {
            MockDiscoveryRequest();

            MockLoginRequest();

            var getEventsResponse = new GetEventsResponse
            {
                Events = _events
            };

            _mockRestClient
            .Setup(x => x.GetAsync <GetEventsResponse>(
                       It.IsAny <string>(),
                       It.IsAny <Dictionary <string, string> >()
                       ))
            .ReturnsAsync(getEventsResponse);

            var nubankClient = new Nubank(_mockRestClient.Object, "login", "password");
            var exception    = await Assert.ThrowsAsync <InvalidOperationException>(() => nubankClient.GetEvents());
        }
Esempio n. 11
0
        public async Task ShouldThrowExceptionWithErrorProvidedWhenLoginFailed()
        {
            MockDiscoveryRequest();

            const string errorMessage = "error message";

            var loginData = new Dictionary <string, object> {
                { "error", errorMessage }
            };

            _mockRestClient
            .Setup(x => x.PostAsync <Dictionary <string, object> >(It.IsAny <string>(), It.IsAny <object>()))
            .ReturnsAsync(loginData);

            var nubankClient = new Nubank(_mockRestClient.Object, "login", "password");
            var exception    = await Assert.ThrowsAsync <AuthenticationException>(
                async() => await nubankClient.LoginAsync()
                );

            Assert.Equal(errorMessage, exception.Message);
        }