github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/crossmodelrelations/macarooncache_test.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package crossmodelrelations_test
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/clock/testclock"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  	"gopkg.in/macaroon.v2-unstable"
    13  
    14  	"github.com/juju/juju/api/crossmodelrelations"
    15  	apitesting "github.com/juju/juju/api/testing"
    16  	coretesting "github.com/juju/juju/testing"
    17  	"gopkg.in/macaroon-bakery.v2-unstable/bakery/checkers"
    18  )
    19  
    20  const longerThanExpiryTime = 11 * time.Minute
    21  
    22  var _ = gc.Suite(&MacaroonCacheSuite{})
    23  
    24  type MacaroonCacheSuite struct {
    25  	coretesting.BaseSuite
    26  }
    27  
    28  func (s *MacaroonCacheSuite) TestGetMacaroonMissing(c *gc.C) {
    29  	cache := crossmodelrelations.NewMacaroonCache(testclock.NewClock(time.Now()))
    30  	_, ok := cache.Get("missing")
    31  	c.Assert(ok, jc.IsFalse)
    32  }
    33  
    34  func (s *MacaroonCacheSuite) TestGetMacaroon(c *gc.C) {
    35  	cache := crossmodelrelations.NewMacaroonCache(testclock.NewClock(time.Now()))
    36  	mac, err := apitesting.NewMacaroon("id")
    37  	c.Assert(err, jc.ErrorIsNil)
    38  	cache.Upsert("token", macaroon.Slice{mac})
    39  	ms, ok := cache.Get("token")
    40  	c.Assert(ok, jc.IsTrue)
    41  	c.Assert(ms, jc.DeepEquals, macaroon.Slice{mac})
    42  }
    43  
    44  func (s *MacaroonCacheSuite) TestGetMacaroonNotExpired(c *gc.C) {
    45  	clock := testclock.NewClock(time.Now())
    46  	cache := crossmodelrelations.NewMacaroonCache(clock)
    47  
    48  	mac, err := apitesting.NewMacaroon("id")
    49  	cav := checkers.TimeBeforeCaveat(clock.Now().Add(10 * time.Second))
    50  	mac.AddFirstPartyCaveat(cav.Condition)
    51  	c.Assert(err, jc.ErrorIsNil)
    52  
    53  	cache.Upsert("token", macaroon.Slice{mac})
    54  	clock.WaitAdvance(9*time.Second, coretesting.ShortWait, 1)
    55  
    56  	ms, ok := cache.Get("token")
    57  	c.Assert(ok, jc.IsTrue)
    58  	c.Assert(ms, jc.DeepEquals, macaroon.Slice{mac})
    59  }
    60  
    61  func (s *MacaroonCacheSuite) TestGetMacaroonExpiredBeforeCleanup(c *gc.C) {
    62  	clock := testclock.NewClock(time.Now())
    63  	cache := crossmodelrelations.NewMacaroonCache(clock)
    64  
    65  	mac, err := apitesting.NewMacaroon("id")
    66  	cav := checkers.TimeBeforeCaveat(clock.Now().Add(10 * time.Second))
    67  	mac.AddFirstPartyCaveat(cav.Condition)
    68  	c.Assert(err, jc.ErrorIsNil)
    69  
    70  	cache.Upsert("token", macaroon.Slice{mac})
    71  	clock.WaitAdvance(20*time.Second, coretesting.ShortWait, 1)
    72  
    73  	_, ok := cache.Get("token")
    74  	c.Assert(ok, jc.IsFalse)
    75  }
    76  
    77  func (s *MacaroonCacheSuite) TestGetMacaroonAfterCleanup(c *gc.C) {
    78  	clock := testclock.NewClock(time.Now())
    79  	cache := crossmodelrelations.NewMacaroonCache(clock)
    80  
    81  	mac, err := apitesting.NewMacaroon("id")
    82  	cav := checkers.TimeBeforeCaveat(clock.Now().Add(60 * time.Minute))
    83  	mac.AddFirstPartyCaveat(cav.Condition)
    84  	c.Assert(err, jc.ErrorIsNil)
    85  
    86  	cache.Upsert("token", macaroon.Slice{mac})
    87  	clock.WaitAdvance(longerThanExpiryTime, coretesting.ShortWait, 1)
    88  
    89  	ms, ok := cache.Get("token")
    90  	c.Assert(ok, jc.IsTrue)
    91  	c.Assert(ms, jc.DeepEquals, macaroon.Slice{mac})
    92  }
    93  
    94  func (s *MacaroonCacheSuite) TestMacaroonRemovedByCleanup(c *gc.C) {
    95  	clock := testclock.NewClock(time.Now())
    96  	cache := crossmodelrelations.NewMacaroonCache(clock)
    97  
    98  	mac, err := apitesting.NewMacaroon("id")
    99  	cav := checkers.TimeBeforeCaveat(clock.Now().Add(2 * time.Minute))
   100  	mac.AddFirstPartyCaveat(cav.Condition)
   101  	c.Assert(err, jc.ErrorIsNil)
   102  
   103  	cache.Upsert("token", macaroon.Slice{mac})
   104  	clock.WaitAdvance(longerThanExpiryTime, coretesting.ShortWait, 1)
   105  
   106  	_, ok := cache.Get("token")
   107  	c.Assert(ok, jc.IsFalse)
   108  }
   109  
   110  func (s *MacaroonCacheSuite) TestCleanupIgnoresMacaroonsWithoutTimeBefore(c *gc.C) {
   111  	clock := testclock.NewClock(time.Now())
   112  	cache := crossmodelrelations.NewMacaroonCache(clock)
   113  
   114  	mac, err := apitesting.NewMacaroon("id")
   115  	c.Assert(err, jc.ErrorIsNil)
   116  
   117  	cache.Upsert("token", macaroon.Slice{mac})
   118  	clock.WaitAdvance(longerThanExpiryTime, coretesting.ShortWait, 1)
   119  
   120  	ms, ok := cache.Get("token")
   121  	c.Assert(ok, jc.IsTrue)
   122  	c.Assert(ms, jc.DeepEquals, macaroon.Slice{mac})
   123  }