/driver-core/src/test/unit/com/mongodb/internal/connection/PlainAuthenticatorUnitTest.java

https://github.com/jyemin/mongo-java-driver · Java · 93 lines · 62 code · 16 blank · 15 comment · 0 complexity · ac18d98d88a5d32903d8276a06450503 MD5 · raw file

  1. /*
  2. * Copyright 2008-present 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.internal.connection;
  17. import com.mongodb.MongoCredential;
  18. import com.mongodb.ServerAddress;
  19. import com.mongodb.async.FutureResultCallback;
  20. import com.mongodb.connection.ClusterConnectionMode;
  21. import com.mongodb.connection.ClusterId;
  22. import com.mongodb.connection.ConnectionDescription;
  23. import com.mongodb.connection.ServerId;
  24. import org.bson.io.BsonInput;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import java.util.List;
  28. import java.util.concurrent.ExecutionException;
  29. import static com.mongodb.ClusterFixture.getServerApi;
  30. import static com.mongodb.internal.connection.MessageHelper.getApiVersionField;
  31. import static com.mongodb.internal.connection.MessageHelper.getDbField;
  32. import static org.junit.Assert.assertEquals;
  33. public class PlainAuthenticatorUnitTest {
  34. private TestInternalConnection connection;
  35. private ConnectionDescription connectionDescription;
  36. private MongoCredential credential;
  37. private PlainAuthenticator subject;
  38. @Before
  39. public void before() {
  40. connection = new TestInternalConnection(new ServerId(new ClusterId(), new ServerAddress("localhost", 27017)));
  41. connectionDescription = new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress()));
  42. credential = MongoCredential.createPlainCredential("user", "$external", "pencil".toCharArray());
  43. subject = new PlainAuthenticator(new MongoCredentialWithCache(credential), ClusterConnectionMode.MULTIPLE, getServerApi());
  44. }
  45. @Test
  46. public void testSuccessfulAuthentication() {
  47. enqueueSuccessfulReply();
  48. subject.authenticate(connection, connectionDescription);
  49. validateMessages();
  50. }
  51. @Test
  52. public void testSuccessfulAuthenticationAsync() throws ExecutionException, InterruptedException {
  53. enqueueSuccessfulReply();
  54. FutureResultCallback<Void> futureCallback = new FutureResultCallback<Void>();
  55. subject.authenticateAsync(connection, connectionDescription, futureCallback);
  56. futureCallback.get();
  57. validateMessages();
  58. }
  59. private void validateMessages() {
  60. List<BsonInput> sent = connection.getSent();
  61. String command = MessageHelper.decodeCommandAsJson(sent.get(0));
  62. String expectedCommand = "{\"saslStart\": 1, "
  63. + "\"mechanism\": \"PLAIN\", "
  64. + "\"payload\": {\"$binary\": {\"base64\": \"dXNlcgB1c2VyAHBlbmNpbA==\", \"subType\": \"00\"}}"
  65. + getDbField("$external")
  66. + getApiVersionField()
  67. + "}";
  68. assertEquals(expectedCommand, command);
  69. }
  70. private void enqueueSuccessfulReply() {
  71. ResponseBuffers reply = MessageHelper.buildSuccessfulReply(
  72. "{conversationId: 1, "
  73. + "done: true, "
  74. + "ok: 1}");
  75. connection.enqueueReply(reply);
  76. }
  77. }