/src/test/com/mongodb/MongoCredentialTest.java

http://github.com/mongodb/mongo-java-driver · Java · 164 lines · 113 code · 27 blank · 24 comment · 0 complexity · d195bdbd2ac88949f34e2b3a9c736c0f MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2014 MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.mongodb;
  17. import com.mongodb.util.TestCase;
  18. import org.junit.Test;
  19. import java.util.Arrays;
  20. import java.util.HashSet;
  21. import java.util.List;
  22. import java.util.Set;
  23. import static org.junit.Assert.assertArrayEquals;
  24. import static org.junit.Assert.assertEquals;
  25. import static org.junit.Assert.assertNull;
  26. import static org.junit.Assert.assertTrue;
  27. import static org.junit.Assert.fail;
  28. public class MongoCredentialTest extends TestCase {
  29. @Test
  30. public void testCredentials() {
  31. MongoCredential credentials;
  32. final String mechanism = MongoCredential.MONGODB_CR_MECHANISM;
  33. final String userName = "user";
  34. final String database = "test";
  35. final char[] password = "pwd".toCharArray();
  36. credentials = MongoCredential.createMongoCRCredential(userName, database, password);
  37. assertEquals(mechanism, credentials.getMechanism());
  38. assertEquals(userName, credentials.getUserName());
  39. assertEquals(database, credentials.getSource());
  40. assertArrayEquals(password, credentials.getPassword());
  41. assertEquals(MongoCredential.MONGODB_CR_MECHANISM, credentials.getMechanism());
  42. try {
  43. MongoCredential.createMongoCRCredential(userName, database, null);
  44. fail("MONGO-CR must have a password");
  45. } catch (IllegalArgumentException e) {
  46. // all good
  47. }
  48. }
  49. @Test
  50. public void testCredentialsStore() {
  51. char[] password = "pwd".toCharArray();
  52. MongoCredentialsStore store;
  53. store = new MongoCredentialsStore();
  54. assertTrue(store.getDatabases().isEmpty());
  55. assertNull(store.get("test"));
  56. store = new MongoCredentialsStore((MongoCredential) null);
  57. assertTrue(store.getDatabases().isEmpty());
  58. assertNull(store.get("test"));
  59. MongoCredential credentials = MongoCredential.createMongoCRCredential("user", "admin", password);
  60. store = new MongoCredentialsStore(credentials);
  61. Set<String> expected;
  62. expected = new HashSet<String>();
  63. expected.add("admin");
  64. assertEquals(expected, store.getDatabases());
  65. assertEquals(credentials, store.get("admin"));
  66. assertNull(store.get("test"));
  67. List<MongoCredential> credentialsList;
  68. final MongoCredential credentials1 = MongoCredential.createMongoCRCredential("user", "db1", password);
  69. final MongoCredential credentials2 = MongoCredential.createMongoCRCredential("user", "db2", password);
  70. credentialsList = Arrays.asList(credentials1, credentials2);
  71. store = new MongoCredentialsStore(credentialsList);
  72. expected = new HashSet<String>();
  73. expected.add("db1");
  74. expected.add("db2");
  75. assertEquals(expected, store.getDatabases());
  76. assertEquals(credentials1, store.get("db1"));
  77. assertEquals(credentials2, store.get("db2"));
  78. assertNull(store.get("db3"));
  79. assertEquals(credentialsList, store.asList());
  80. credentialsList = Arrays.asList(credentials1, MongoCredential.createMongoCRCredential("user2", "db1", password));
  81. try {
  82. new MongoCredentialsStore(credentialsList);
  83. fail("should throw");
  84. } catch (IllegalArgumentException e) {
  85. // expected
  86. }
  87. }
  88. @Test
  89. public void testMechanismPropertyDefaulting() {
  90. // given
  91. String firstKey = "firstKey";
  92. MongoCredential credential = MongoCredential.createGSSAPICredential("user");
  93. // then
  94. assertEquals("mongodb", credential.getMechanismProperty(firstKey, "mongodb"));
  95. }
  96. @Test
  97. public void testMechanismPropertyMapping() {
  98. // given
  99. String firstKey = "firstKey";
  100. String firstValue = "firstValue";
  101. String secondKey = "secondKey";
  102. Integer secondValue = 2;
  103. // when
  104. MongoCredential credential = MongoCredential.createGSSAPICredential("user").withMechanismProperty(firstKey, firstValue);
  105. // then
  106. assertEquals(firstValue, credential.getMechanismProperty(firstKey, "default"));
  107. // when
  108. credential = credential.withMechanismProperty(secondKey, secondValue);
  109. // then
  110. assertEquals(firstValue, credential.getMechanismProperty(firstKey, "default"));
  111. assertEquals(secondValue, credential.getMechanismProperty(secondKey, 1));
  112. }
  113. @Test
  114. public void testPlainMechanism() {
  115. MongoCredential credential;
  116. final String mechanism = MongoCredential.PLAIN_MECHANISM;
  117. final String userName = "user";
  118. final char[] password = "pwd".toCharArray();
  119. final String source = "$external";
  120. credential = MongoCredential.createPlainCredential(userName, source, password);
  121. assertEquals(mechanism, credential.getMechanism());
  122. assertEquals(userName, credential.getUserName());
  123. assertEquals("$external", credential.getSource());
  124. assertArrayEquals(password, credential.getPassword());
  125. }
  126. @Test
  127. public void testX509Mechanism() {
  128. MongoCredential credential;
  129. final String mechanism = MongoCredential.MONGODB_X509_MECHANISM;
  130. final String userName = "user";
  131. credential = MongoCredential.createMongoX509Credential(userName);
  132. assertEquals(mechanism, credential.getMechanism());
  133. assertEquals(userName, credential.getUserName());
  134. assertEquals("$external", credential.getSource());
  135. assertArrayEquals(null, credential.getPassword());
  136. }
  137. }