...

Source file src/edge-infra.dev/test/f2/x/bslauth/bslauth.go

Documentation: edge-infra.dev/test/f2/x/bslauth

     1  package bslauth
     2  
     3  import (
     4  	"encoding/json"
     5  	"flag"
     6  	"fmt"
     7  	"os"
     8  	"testing"
     9  
    10  	"edge-infra.dev/test/f2"
    11  	"edge-infra.dev/test/f2/fctx"
    12  )
    13  
    14  // Identifier is a consistent way of referring to users across multiple edge
    15  // environments. A user with a given Identifier will have the same roles across
    16  // multiple environments, even if the login details change.
    17  type Identifier string
    18  
    19  // All currently configured users
    20  const (
    21  	BannerAdmin Identifier = "banner-admin" // Identifies an user with the banner admin role
    22  )
    23  
    24  type BSLAuth struct {
    25  	APIEndpoint string
    26  	users       map[string]User
    27  }
    28  
    29  // Finds the User login details in the current env given an identifier
    30  func (b *BSLAuth) User(id Identifier) User {
    31  	return b.users[string(id)]
    32  }
    33  
    34  // User holds the Edge Login details for an user in the given environment
    35  type User struct {
    36  	Username string
    37  	Password string
    38  	Org      string `json:"organization"`
    39  }
    40  
    41  func FromContext(ctx fctx.Context) (*BSLAuth, error) {
    42  	v := fctx.ValueFrom[BSLAuth](ctx)
    43  	if v == nil {
    44  		return nil, fmt.Errorf("%w: bslAuth extension", fctx.ErrNotFound)
    45  	}
    46  	return v, nil
    47  }
    48  
    49  func FromContextT(ctx fctx.Context, t *testing.T) *BSLAuth {
    50  	return fctx.ValueFromT[BSLAuth](ctx, t)
    51  }
    52  
    53  func New() *BSLAuth {
    54  	bslAuth := &BSLAuth{}
    55  	return bslAuth
    56  }
    57  
    58  func (b *BSLAuth) RegisterFns(f f2.Framework) {
    59  	f.Setup(func(ctx f2.Context) (f2.Context, error) {
    60  		creds, err := os.ReadFile("/mnt/bslcreds/bslcreds")
    61  
    62  		if err != nil {
    63  			return ctx, fmt.Errorf("Unable to read BSL credentials: %w", err)
    64  		}
    65  
    66  		err = json.Unmarshal(creds, &b.users)
    67  		return ctx, err
    68  	})
    69  }
    70  
    71  // BindFlags registers test flags for the framework extension.
    72  func (b *BSLAuth) BindFlags(fs *flag.FlagSet) {
    73  	fs.StringVar(&b.APIEndpoint,
    74  		"api-endpoint", "", "edge api endpoint",
    75  	)
    76  }
    77  
    78  // IntoContext stores the framework extension in the test context.
    79  func (b *BSLAuth) IntoContext(ctx fctx.Context) fctx.Context {
    80  	return fctx.ValueInto(ctx, b)
    81  }
    82  

View as plain text