using Knoks.Api.Client;
using Knoks.Api.Client.Invoker;
using Knoks.Test.Integrations.Api.Tools;
using System;
using System.Net;
using System.Threading.Tasks;
using Xunit;

namespace Knoks.Test.Integrations.Api
{
    public class TokenControllerTest : ApiTestClient
    {
        [Fact(DisplayName = "Get Api Consumer Token")]
        async Task GetApiConsumerTokenTest()
        {
            var res = await ApiClient.GetApiConsumerToken();
            Assert.NotNull(res.Object);
        }

        [Fact(DisplayName = "Invalid Api Identifier")]
        async Task InvalidBrandIdentifier()
        {
            var tmpClient = new ApiClient(Guid.NewGuid().ToString("N"), ApiClient.HttpClient);
            var res = await tmpClient.GetApiConsumerToken();
            Assert.Equal(ApiErrorType.InvalidApiIdentifier, res.ApiResponse.Error.Id);
        }

        [Fact(DisplayName = "Refresh")]
        async Task Refresh()
        {
            await ApiClient.AuthorizeApiConsumer();
            var res = await ApiClient.InvokeTokenRefresh();

            Assert.Null(res.ApiResponse.Error);
            Assert.NotNull(res.ApiResponse.Result);
        }

        [Fact(DisplayName = "Unotorized Refresh")]
        async Task UnotorizedRefresh()
        {
            var res = await ApiClient.InvokeTokenRefresh();
            Assert.Equal(HttpStatusCode.Unauthorized, res.HttpResponse.StatusCode);
        }

        [Fact(DisplayName = "Heartbit")]
        async Task Heartbit()
        {
            await ApiClient.AuthorizeApiConsumer();
            var res = (await ApiClient.InvokeTokenHeartbit()).Object;

            Assert.True(res.Value);
        }

        [Fact(DisplayName = "Unotorized Heartbit")]
        async Task UnotorizedHeartbit()
        {
            var res = await ApiClient.InvokeTokenHeartbit();
            Assert.Equal(HttpStatusCode.Unauthorized, res.HttpResponse.StatusCode);
        }

        [Fact(DisplayName = "Invalid Username or Password")]
        async Task InvalidUsernameOrPassword()
        {
            var response = await ApiClient.GetApiUserToken(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
            Assert.Equal(ApiErrorType.InvalidUsernameOrPassword, response.ApiResponse.Error.Id);
        }

        [Fact(DisplayName = "Invalid OperatorName or OperatorPassword")]
        async Task InvalidOperatorNameOrOperatorPassword()
        {
            var res = await ApiClient.GetApiOperatorToken(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
            Assert.Equal(ApiErrorType.InvalidUsernameOrPassword, res.ApiResponse.Error.Id);
        }
    }
}
