package com.auth0.exception;

import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class APIExceptionTest {

    private Map<String, Object> values;

    @Before
    public void setUp() {
        values = new HashMap<>();
    }

    @Test
    public void shouldBeMfaRequiredWithMfaTokenError() {
        values.put("error", "mfa_required");
        values.put("mfa_token", "some-mfa-token");
        APIException apiException = new APIException(values, 403);
        assertThat(apiException.isMultifactorRequired(), is(true));
        assertThat(apiException.getValue("mfa_token"), is("some-mfa-token"));
    }

    @Test
    public void shouldBeInvalidCredentialsError() {
        values.put("error", "invalid_grant");
        values.put("error_description", "Wrong email or password.");
        APIException apiException = new APIException(values, 403);
        assertThat(apiException.isInvalidCredentials(), is(true));
    }

    @Test
    public void shouldBeAccessDeniedError() {
        values.put("error", "access_denied");
        APIException apiException = new APIException(values, 401);
        assertThat(apiException.isAccessDenied(), is(true));
    }

    @Test
    public void shouldBeMfaEnrollementRequiredError() {
        values.put("error", "unsupported_challenge_type");
        APIException apiException = new APIException(values, 403);
        assertThat(apiException.isMultifactorEnrollRequired(), is(true));
    }

    @Test
    public void shouldBeMfaTokenInvalidErrorForMalformedToken() {
        values.put("error", "invalid_grant");
        values.put("error_description", "Malformed mfa_token");
        APIException apiException = new APIException(values, 1);
        assertThat(apiException.isMultifactorTokenInvalid(), is(true));
    }

    @Test
    public void shouldBeMfaTokenInvalidErrorForExpiredToken() {
        values.put("error", "expired_token");
        values.put("error_description", "mfa_token is expired");
        APIException apiException = new APIException(values, 1);
        assertThat(apiException.isMultifactorTokenInvalid(), is(true));
    }

    @Test
    public void shouldBeMfaEnrollementRequired() {
        values.put("error", "unsupported_challenge_type");
        APIException apiException = new APIException(values, 401);
        assertThat(apiException.isMultifactorEnrollRequired(), is(true));
    }

    @Test
    public void checkErrorTypesShouldHandleNullError() {
        APIException apiException = new APIException(values, 403);
        assertThat(apiException.isAccessDenied(), is(false));
        assertThat(apiException.isInvalidCredentials(), is(false));
        assertThat(apiException.isMultifactorRequired(), is(false));
        assertThat(apiException.isMultifactorEnrollRequired(), is(false));
        assertThat(apiException.isMultifactorTokenInvalid(), is(false));
        assertThat(apiException.isVerificationRequired(), is(false));

    }
}

@@ -589,34 +589,33 @@
 eption(values, 4
-03
+2
 );%0A        asser
@@ -807,24 +807,40 @@
 entialsError
+WhenInvalidGrant
 () %7B%0A       
@@ -1001,34 +1001,381 @@
 eption(values, 4
-03
+2);%0A        assertThat(apiException.isInvalidCredentials(), is(true));%0A    %7D%0A%0A    @Test%0A    public void shouldBeInvalidCredentialsErrorWhenInvalidUserPassword() %7B%0A        values.put(%22error%22, %22invalid_user_password%22);%0A        values.put(%22error_description%22, %22Wrong email or password.%22);%0A        APIException apiException = new APIException(values, 42
 );%0A        asser
@@ -1591,34 +1591,33 @@
 eption(values, 4
-01
+2
 );%0A        asser
@@ -1850,34 +1850,33 @@
 eption(values, 4
-03
+2
 );%0A        asser
@@ -2182,33 +2182,34 @@
 ception(values, 
-1
+42
 );%0A        asser
@@ -3004,10 +3004,9 @@
 s, 4
-03
+2
 );%0A 

Update tests