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

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	ktesting "github.com/segmentio/kafka-go/testing"
     8  )
     9  
    10  func TestAlterUserScramCredentials(t *testing.T) {
    11  	// https://issues.apache.org/jira/browse/KAFKA-10259
    12  	if !ktesting.KafkaIsAtLeast("2.7.0") {
    13  		return
    14  	}
    15  
    16  	client, shutdown := newLocalClient()
    17  	defer shutdown()
    18  
    19  	name := makeTopic()
    20  
    21  	createRes, err := client.AlterUserScramCredentials(context.Background(), &AlterUserScramCredentialsRequest{
    22  		Upsertions: []UserScramCredentialsUpsertion{
    23  			{
    24  				Name:           name,
    25  				Mechanism:      ScramMechanismSha512,
    26  				Iterations:     15000,
    27  				Salt:           []byte("my-salt"),
    28  				SaltedPassword: []byte("my-salted-password"),
    29  			},
    30  		},
    31  	})
    32  
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	if len(createRes.Results) != 1 {
    38  		t.Fatalf("expected 1 createResult; got %d", len(createRes.Results))
    39  	}
    40  
    41  	if createRes.Results[0].User != name {
    42  		t.Fatalf("expected createResult with user: %s, got %s", name, createRes.Results[0].User)
    43  	}
    44  
    45  	if createRes.Results[0].Error != nil {
    46  		t.Fatalf("didn't expect an error in createResult, got %v", createRes.Results[0].Error)
    47  	}
    48  
    49  	deleteRes, err := client.AlterUserScramCredentials(context.Background(), &AlterUserScramCredentialsRequest{
    50  		Deletions: []UserScramCredentialsDeletion{
    51  			{
    52  				Name:      name,
    53  				Mechanism: ScramMechanismSha512,
    54  			},
    55  		},
    56  	})
    57  
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  
    62  	if len(deleteRes.Results) != 1 {
    63  		t.Fatalf("expected 1 deleteResult; got %d", len(deleteRes.Results))
    64  	}
    65  
    66  	if deleteRes.Results[0].User != name {
    67  		t.Fatalf("expected deleteResult with user: %s, got %s", name, deleteRes.Results[0].User)
    68  	}
    69  
    70  	if deleteRes.Results[0].Error != nil {
    71  		t.Fatalf("didn't expect an error in deleteResult, got %v", deleteRes.Results[0].Error)
    72  	}
    73  }