code.gitea.io/gitea@v1.19.3/modules/auth/openid/discovery_cache_test.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package openid
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  type testDiscoveredInfo struct{}
    12  
    13  func (s *testDiscoveredInfo) ClaimedID() string {
    14  	return "claimedID"
    15  }
    16  
    17  func (s *testDiscoveredInfo) OpEndpoint() string {
    18  	return "opEndpoint"
    19  }
    20  
    21  func (s *testDiscoveredInfo) OpLocalID() string {
    22  	return "opLocalID"
    23  }
    24  
    25  func TestTimedDiscoveryCache(t *testing.T) {
    26  	dc := newTimedDiscoveryCache(1 * time.Second)
    27  
    28  	// Put some initial values
    29  	dc.Put("foo", &testDiscoveredInfo{}) // openid.opEndpoint: "a", openid.opLocalID: "b", openid.claimedID: "c"})
    30  
    31  	// Make sure we can retrieve them
    32  	if di := dc.Get("foo"); di == nil {
    33  		t.Errorf("Expected a result, got nil")
    34  	} else if di.OpEndpoint() != "opEndpoint" || di.OpLocalID() != "opLocalID" || di.ClaimedID() != "claimedID" {
    35  		t.Errorf("Expected opEndpoint opLocalID claimedID, got %v %v %v", di.OpEndpoint(), di.OpLocalID(), di.ClaimedID())
    36  	}
    37  
    38  	// Attempt to get a non-existent value
    39  	if di := dc.Get("bar"); di != nil {
    40  		t.Errorf("Expected nil, got %v", di)
    41  	}
    42  
    43  	// Sleep one second and try retrieve again
    44  	time.Sleep(1 * time.Second)
    45  
    46  	if di := dc.Get("foo"); di != nil {
    47  		t.Errorf("Expected a nil, got a result")
    48  	}
    49  }