Skip to content

Commit

Permalink
auth: export mock mint so we can use it elsewhere
Browse files Browse the repository at this point in the history
  • Loading branch information
orbitalturtle committed Mar 6, 2023
1 parent c43f4a8 commit 1b0479b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 66 deletions.
30 changes: 4 additions & 26 deletions auth/authenticator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,15 @@ import (

"github.com/lightninglabs/aperture/auth"
"github.com/lightninglabs/aperture/lsat"
"gopkg.in/macaroon.v2"
)

// createDummyMacHex creates a valid macaroon with dummy content for our tests.
func createDummyMacHex(preimage string) string {
dummyMac, err := macaroon.New(
[]byte("aabbccddeeff00112233445566778899"), []byte("AA=="),
"aperture", macaroon.LatestVersion,
)
if err != nil {
panic(err)
}
preimageCaveat := lsat.Caveat{Condition: lsat.PreimageKey, Value: preimage}
err = lsat.AddFirstPartyCaveats(dummyMac, preimageCaveat)
if err != nil {
panic(err)
}
macBytes, err := dummyMac.MarshalBinary()
if err != nil {
panic(err)
}
return hex.EncodeToString(macBytes)
}

// TestLsatAuthenticator tests that the authenticator properly handles auth
// headers and the tokens contained in them.
func TestLsatAuthenticator(t *testing.T) {
var (
testPreimage = "49349dfea4abed3cd14f6d356afa83de" +
"9787b609f088c8df09bacc7b4bd21b39"
testMacHex = createDummyMacHex(testPreimage)
testMacHex = auth.CreateDummyMacHex(testPreimage)
testMacBytes, _ = hex.DecodeString(testMacHex)
testMacBase64 = base64.StdEncoding.EncodeToString(
testMacBytes,
Expand Down Expand Up @@ -139,10 +117,10 @@ func TestLsatAuthenticator(t *testing.T) {
}
)

c := &mockChecker{}
a := auth.NewLsatAuthenticator(&mockMint{}, c)
c := &auth.MockChecker{}
a := auth.NewLsatAuthenticator(&auth.MockMint{}, c)
for _, testCase := range headerTests {
c.err = testCase.checkErr
c.Err = testCase.checkErr
result := a.Accept(testCase.header, "test")
if result != testCase.result {
t.Fatalf("test case %s failed. got %v expected %v",
Expand Down
40 changes: 0 additions & 40 deletions auth/mock_test.go

This file was deleted.

61 changes: 61 additions & 0 deletions auth/test_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package auth

import (
"context"
"encoding/hex"
"time"

"github.com/lightninglabs/aperture/lsat"
"github.com/lightninglabs/aperture/mint"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lntypes"
"gopkg.in/macaroon.v2"
)

// CreateDummyMacHex creates a valid macaroon with dummy content for our tests.
func CreateDummyMacHex(preimage string) string {
dummyMac, err := macaroon.New(
[]byte("aabbccddeeff00112233445566778899"), []byte("AA=="),
"aperture", macaroon.LatestVersion,
)
if err != nil {
panic(err)
}
preimageCaveat := lsat.Caveat{Condition: lsat.PreimageKey, Value: preimage}
err = lsat.AddFirstPartyCaveats(dummyMac, preimageCaveat)
if err != nil {
panic(err)
}
macBytes, err := dummyMac.MarshalBinary()
if err != nil {
panic(err)
}
return hex.EncodeToString(macBytes)
}

type MockMint struct {
}

var _ Minter = (*MockMint)(nil)

func (m *MockMint) MintLSAT(_ context.Context,
services ...lsat.Service) (*macaroon.Macaroon, string, error) {

return nil, "", nil
}

func (m *MockMint) VerifyLSAT(_ context.Context, p *mint.VerificationParams) error {
return nil
}

type MockChecker struct {
Err error
}

var _ InvoiceChecker = (*MockChecker)(nil)

func (m *MockChecker) VerifyInvoiceStatus(lntypes.Hash,
lnrpc.Invoice_InvoiceState, time.Duration) error {

return m.Err
}

0 comments on commit 1b0479b

Please sign in to comment.