/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jackrabbit.oak.security.authentication.token;

import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.jackrabbit.oak.api.Root;
import org.apache.jackrabbit.oak.namepath.NamePathMapper;
import org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo;
import org.apache.jackrabbit.oak.spi.security.user.UserConfiguration;
import org.junit.Before;
import org.junit.Test;

import javax.jcr.RepositoryException;
import javax.jcr.SimpleCredentials;

import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class TokenProviderImplExceptionTest extends AbstractTokenTest  {

    private TokenProviderImpl tp;
    private UserManager userManager;

    @Before
    @Override
    public void before() throws Exception {
        super.before();

        userManager = mock(UserManager.class);
        UserConfiguration uc = mock(UserConfiguration.class);
        when(uc.getUserManager(any(Root.class), any(NamePathMapper.class))).thenReturn(userManager);

        tp = createTokenProvider(root, uc);
    }

    @Test
    public void testCreateToken() throws Exception {
        when(userManager.getAuthorizable(anyString())).thenThrow(new RepositoryException());

        assertNull(tp.createToken(new SimpleCredentials("uid", new char[0])));
        verify(userManager, times(1)).getAuthorizable("uid");
    }

    @Test
    public void testCreateTokenUserWithoutPath() throws Exception {
        User u = when(mock(User.class).getPath()).thenThrow(new RepositoryException()).getMock();
        when(userManager.getAuthorizable("uid")).thenReturn(u);

        assertNull(tp.createToken(new SimpleCredentials("uid", new char[0])));
        verify(userManager, times(1)).getAuthorizable("uid");
        verify(u, times(1)).getPath();
    }

    @Test
    public void testGetTokenInfo() throws Exception {
        // generate valid token
        Authorizable user = getTestUser();
        TokenInfo valid = createTokenInfo(tokenProvider, user.getID());

        when(userManager.getAuthorizableByPath(anyString())).thenThrow(new RepositoryException());

        assertNull(tp.getTokenInfo(valid.getToken()));
        verify(userManager, times(1)).getAuthorizableByPath(user.getPath());
    }

}