...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/auth/mongodbcr_test.go

Documentation: go.mongodb.org/mongo-driver/x/mongo/driver/auth

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package auth_test
     8  
     9  import (
    10  	"context"
    11  	"testing"
    12  
    13  	"strings"
    14  
    15  	"go.mongodb.org/mongo-driver/mongo/description"
    16  	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
    17  	. "go.mongodb.org/mongo-driver/x/mongo/driver/auth"
    18  	"go.mongodb.org/mongo-driver/x/mongo/driver/drivertest"
    19  )
    20  
    21  func TestMongoDBCRAuthenticator_Fails(t *testing.T) {
    22  	t.Parallel()
    23  
    24  	authenticator := MongoDBCRAuthenticator{
    25  		DB:       "source",
    26  		Username: "user",
    27  		Password: "pencil",
    28  	}
    29  
    30  	resps := make(chan []byte, 2)
    31  	writeReplies(resps, bsoncore.BuildDocumentFromElements(nil,
    32  		bsoncore.AppendInt32Element(nil, "ok", 1),
    33  		bsoncore.AppendStringElement(nil, "nonce", "2375531c32080ae8"),
    34  	), bsoncore.BuildDocumentFromElements(nil,
    35  		bsoncore.AppendInt32Element(nil, "ok", 0),
    36  	))
    37  
    38  	desc := description.Server{
    39  		WireVersion: &description.VersionRange{
    40  			Max: 6,
    41  		},
    42  	}
    43  	c := &drivertest.ChannelConn{
    44  		Written:  make(chan []byte, 2),
    45  		ReadResp: resps,
    46  		Desc:     desc,
    47  	}
    48  
    49  	err := authenticator.Auth(context.Background(), &Config{Description: desc, Connection: c})
    50  	if err == nil {
    51  		t.Fatalf("expected an error but got none")
    52  	}
    53  
    54  	errPrefix := "unable to authenticate using mechanism \"MONGODB-CR\""
    55  	if !strings.HasPrefix(err.Error(), errPrefix) {
    56  		t.Fatalf("expected an err starting with \"%s\" but got \"%s\"", errPrefix, err)
    57  	}
    58  }
    59  
    60  func TestMongoDBCRAuthenticator_Succeeds(t *testing.T) {
    61  	t.Parallel()
    62  
    63  	authenticator := MongoDBCRAuthenticator{
    64  		DB:       "source",
    65  		Username: "user",
    66  		Password: "pencil",
    67  	}
    68  
    69  	resps := make(chan []byte, 2)
    70  	writeReplies(resps, bsoncore.BuildDocumentFromElements(nil,
    71  		bsoncore.AppendInt32Element(nil, "ok", 1),
    72  		bsoncore.AppendStringElement(nil, "nonce", "2375531c32080ae8"),
    73  	), bsoncore.BuildDocumentFromElements(nil,
    74  		bsoncore.AppendInt32Element(nil, "ok", 1),
    75  	))
    76  
    77  	desc := description.Server{
    78  		WireVersion: &description.VersionRange{
    79  			Max: 6,
    80  		},
    81  	}
    82  	c := &drivertest.ChannelConn{
    83  		Written:  make(chan []byte, 2),
    84  		ReadResp: resps,
    85  		Desc:     desc,
    86  	}
    87  
    88  	err := authenticator.Auth(context.Background(), &Config{Description: desc, Connection: c})
    89  	if err != nil {
    90  		t.Fatalf("expected no error but got \"%s\"", err)
    91  	}
    92  
    93  	if len(c.Written) != 2 {
    94  		t.Fatalf("expected 2 messages to be sent but had %d", len(c.Written))
    95  	}
    96  
    97  	want := bsoncore.BuildDocumentFromElements(nil, bsoncore.AppendInt32Element(nil, "getnonce", 1))
    98  	compareResponses(t, <-c.Written, want, "source")
    99  
   100  	expectedAuthenticateDoc := bsoncore.BuildDocumentFromElements(nil,
   101  		bsoncore.AppendInt32Element(nil, "authenticate", 1),
   102  		bsoncore.AppendStringElement(nil, "user", "user"),
   103  		bsoncore.AppendStringElement(nil, "nonce", "2375531c32080ae8"),
   104  		bsoncore.AppendStringElement(nil, "key", "21742f26431831d5cfca035a08c5bdf6"),
   105  	)
   106  	compareResponses(t, <-c.Written, expectedAuthenticateDoc, "source")
   107  }
   108  
   109  func writeReplies(c chan []byte, docs ...bsoncore.Document) {
   110  	for _, doc := range docs {
   111  		reply := drivertest.MakeReply(doc)
   112  		c <- reply
   113  	}
   114  }
   115  

View as plain text