DATAMONGO-1216 - Skip authentication via AuthDB for MongoClient.

We now skip authentication via an explicit AuthDB when requesting a DB via a MongoClient instance.

Related ticket: DATACMNS-1218
Original pull request: #296.
This commit is contained in:
Christoph Strobl
2015-05-19 10:37:25 +02:00
committed by Oliver Gierke
parent 949833a7db
commit d4792cd680
2 changed files with 44 additions and 9 deletions

View File

@@ -123,7 +123,7 @@ public abstract class MongoDbUtils {
DB db = mongo.getDB(databaseName);
if (requiresAuthDbAuthentication(credentials)) {
if (!(mongo instanceof MongoClient) && requiresAuthDbAuthentication(credentials)) {
ReflectiveDbInvoker.authenticate(mongo, db, credentials, authenticationDatabaseName);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@ package org.springframework.data.mongodb.core;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
@@ -27,35 +28,37 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import org.springframework.data.authentication.UserCredentials;
import org.springframework.data.mongodb.CannotGetMongoDbConnectionException;
import org.springframework.data.mongodb.util.MongoClientVersion;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.transaction.support.TransactionSynchronizationUtils;
import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
/**
* Unit tests for {@link MongoDbUtils}.
*
* @author Oliver Gierke
* @author Randy Watler
* @author Christoph Strobl
*/
@RunWith(MockitoJUnitRunner.class)
public class MongoDbUtilsUnitTests {
@Mock Mongo mongo;
@Mock MongoClient mongoClientMock;
@Mock DB dbMock;
@Before
public void setUp() throws Exception {
when(mongo.getDB(anyString())).then(new Answer<DB>() {
public DB answer(InvocationOnMock invocation) throws Throwable {
return mock(DB.class);
}
});
when(mongo.getDB(anyString())).thenReturn(dbMock).thenReturn(mock(DB.class));
when(mongoClientMock.getDB(anyString())).thenReturn(dbMock);
TransactionSynchronizationManager.initSynchronization();
}
@@ -151,6 +154,38 @@ public class MongoDbUtilsUnitTests {
assertThat(TransactionSynchronizationManager.getResourceMap().isEmpty(), is(true));
}
/**
* @see DATAMONGO-1218
*/
@Test
@SuppressWarnings("deprecation")
public void getDBDAuthenticateViaAuthDbWhenCalledWithMongoInstance() {
assumeThat(MongoClientVersion.isMongo3Driver(), is(false));
when(dbMock.getName()).thenReturn("db");
try {
MongoDbUtils.getDB(mongo, "db", new UserCredentials("shallan", "davar"), "authdb");
} catch (CannotGetMongoDbConnectionException e) {
// need to catch that one since we cannot answer the reflective call sufficiently
}
verify(mongo, times(1)).getDB("authdb");
}
/**
* @see DATAMONGO-1218
*/
@Test
@SuppressWarnings("deprecation")
public void getDBDShouldSkipAuthenticationViaAuthDbWhenCalledWithMongoClientInstance() {
MongoDbUtils.getDB(mongoClientMock, "db", new UserCredentials("dalinar", "kholin"), "authdb");
verify(mongoClientMock, never()).getDB("authdb");
}
/**
* Simulate transaction rollback/commit completion protocol on managed transaction synchronizations which will unbind
* managed transaction resources. Does not swallow exceptions for testing purposes.