github.com/segmentio/kafka-go@v0.4.48-0.20240318174348-3f6244eb34fd/describeuserscramcredentials_test.go (about)

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  
     8  	ktesting "github.com/segmentio/kafka-go/testing"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestDescribeUserScramCredentials(t *testing.T) {
    13  	// https://issues.apache.org/jira/browse/KAFKA-10259
    14  	if !ktesting.KafkaIsAtLeast("2.7.0") {
    15  		return
    16  	}
    17  
    18  	client, shutdown := newLocalClient()
    19  	defer shutdown()
    20  
    21  	name := makeTopic()
    22  
    23  	createRes, err := client.AlterUserScramCredentials(context.Background(), &AlterUserScramCredentialsRequest{
    24  		Upsertions: []UserScramCredentialsUpsertion{
    25  			{
    26  				Name:           name,
    27  				Mechanism:      ScramMechanismSha512,
    28  				Iterations:     15000,
    29  				Salt:           []byte("my-salt"),
    30  				SaltedPassword: []byte("my-salted-password"),
    31  			},
    32  		},
    33  	})
    34  
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  
    39  	if len(createRes.Results) != 1 {
    40  		t.Fatalf("expected 1 createResult; got %d", len(createRes.Results))
    41  	}
    42  
    43  	if createRes.Results[0].User != name {
    44  		t.Fatalf("expected createResult with user: %s, got %s", name, createRes.Results[0].User)
    45  	}
    46  
    47  	if createRes.Results[0].Error != nil {
    48  		t.Fatalf("didn't expect an error in createResult, got %v", createRes.Results[0].Error)
    49  	}
    50  
    51  	describeCreationRes, err := client.DescribeUserScramCredentials(context.Background(), &DescribeUserScramCredentialsRequest{
    52  		Users: []UserScramCredentialsUser{
    53  			{
    54  				Name: name,
    55  			},
    56  		},
    57  	})
    58  
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  
    63  	expectedCreation := DescribeUserScramCredentialsResponse{
    64  		Throttle: makeDuration(0),
    65  		Error:    makeError(0, ""),
    66  		Results: []DescribeUserScramCredentialsResponseResult{
    67  			{
    68  				User: name,
    69  				CredentialInfos: []DescribeUserScramCredentialsCredentialInfo{
    70  					{
    71  						Mechanism:  ScramMechanismSha512,
    72  						Iterations: 15000,
    73  					},
    74  				},
    75  				Error: makeError(0, ""),
    76  			},
    77  		},
    78  	}
    79  
    80  	assert.Equal(t, expectedCreation, *describeCreationRes)
    81  
    82  	deleteRes, err := client.AlterUserScramCredentials(context.Background(), &AlterUserScramCredentialsRequest{
    83  		Deletions: []UserScramCredentialsDeletion{
    84  			{
    85  				Name:      name,
    86  				Mechanism: ScramMechanismSha512,
    87  			},
    88  		},
    89  	})
    90  
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  
    95  	if len(deleteRes.Results) != 1 {
    96  		t.Fatalf("expected 1 deleteResult; got %d", len(deleteRes.Results))
    97  	}
    98  
    99  	if deleteRes.Results[0].User != name {
   100  		t.Fatalf("expected deleteResult with user: %s, got %s", name, deleteRes.Results[0].User)
   101  	}
   102  
   103  	if deleteRes.Results[0].Error != nil {
   104  		t.Fatalf("didn't expect an error in deleteResult, got %v", deleteRes.Results[0].Error)
   105  	}
   106  
   107  	describeDeletionRes, err := client.DescribeUserScramCredentials(context.Background(), &DescribeUserScramCredentialsRequest{
   108  		Users: []UserScramCredentialsUser{
   109  			{
   110  				Name: name,
   111  			},
   112  		},
   113  	})
   114  
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  
   119  	if !errors.Is(describeDeletionRes.Error, makeError(0, "")) {
   120  		t.Fatalf("didn't expect a top level error on describe results after deletion, got %v", describeDeletionRes.Error)
   121  	}
   122  
   123  	if len(describeDeletionRes.Results) != 1 {
   124  		t.Fatalf("expected one describe results after deletion, got %d describe results", len(describeDeletionRes.Results))
   125  	}
   126  
   127  	result := describeDeletionRes.Results[0]
   128  
   129  	if result.User != name {
   130  		t.Fatalf("expected describeResult with user: %s, got %s", name, result.User)
   131  	}
   132  
   133  	if len(result.CredentialInfos) != 0 {
   134  		t.Fatalf("didn't expect describeResult credential infos, got %v", result.CredentialInfos)
   135  	}
   136  
   137  	if !errors.Is(result.Error, ResourceNotFound) {
   138  		t.Fatalf("expected describeResult resourcenotfound error, got %s", result.Error)
   139  	}
   140  }