...

Source file src/go.etcd.io/etcd/server/v3/auth/simple_token_test.go

Documentation: go.etcd.io/etcd/server/v3/auth

     1  // Copyright 2017 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package auth
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"go.uber.org/zap"
    22  )
    23  
    24  // TestSimpleTokenDisabled ensures that TokenProviderSimple behaves correctly when
    25  // disabled.
    26  func TestSimpleTokenDisabled(t *testing.T) {
    27  	initialState := newTokenProviderSimple(zap.NewExample(), dummyIndexWaiter, simpleTokenTTLDefault)
    28  
    29  	explicitlyDisabled := newTokenProviderSimple(zap.NewExample(), dummyIndexWaiter, simpleTokenTTLDefault)
    30  	explicitlyDisabled.enable()
    31  	explicitlyDisabled.disable()
    32  
    33  	for _, tp := range []*tokenSimple{initialState, explicitlyDisabled} {
    34  		ctx := context.WithValue(context.WithValue(context.TODO(), AuthenticateParamIndex{}, uint64(1)), AuthenticateParamSimpleTokenPrefix{}, "dummy")
    35  		token, err := tp.assign(ctx, "user1", 0)
    36  		if err != nil {
    37  			t.Fatal(err)
    38  		}
    39  		authInfo, ok := tp.info(ctx, token, 0)
    40  		if ok {
    41  			t.Errorf("expected (true, \"user1\") got (%t, %s)", ok, authInfo.Username)
    42  		}
    43  
    44  		tp.invalidateUser("user1") // should be no-op
    45  	}
    46  }
    47  
    48  // TestSimpleTokenAssign ensures that TokenProviderSimple can correctly assign a
    49  // token, look it up with info, and invalidate it by user.
    50  func TestSimpleTokenAssign(t *testing.T) {
    51  	tp := newTokenProviderSimple(zap.NewExample(), dummyIndexWaiter, simpleTokenTTLDefault)
    52  	tp.enable()
    53  	defer tp.disable()
    54  	ctx := context.WithValue(context.WithValue(context.TODO(), AuthenticateParamIndex{}, uint64(1)), AuthenticateParamSimpleTokenPrefix{}, "dummy")
    55  	token, err := tp.assign(ctx, "user1", 0)
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	authInfo, ok := tp.info(ctx, token, 0)
    60  	if !ok || authInfo.Username != "user1" {
    61  		t.Errorf("expected (true, \"token2\") got (%t, %s)", ok, authInfo.Username)
    62  	}
    63  
    64  	tp.invalidateUser("user1")
    65  
    66  	_, ok = tp.info(context.TODO(), token, 0)
    67  	if ok {
    68  		t.Errorf("expected ok == false after user is invalidated")
    69  	}
    70  }
    71  

View as plain text