testserver

package
v0.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 26, 2018 License: Apache-2.0 Imports: 15 Imported by: 0

README

testserver - a server to help testing Bankrs OS clients

This is an experimental test server package for testing access to the Bankrs OS API. It is intended to provide basic support for testing the main features of client applications. The test server starts an http server which can be accessed using the bosgo client library or directly via HTTP requests.

Current API coverage is minimal and focussed on the happy path:

  • Create a user
  • User login
  • User logout
  • Add an access to a user
  • List user accesses
  • List user accounts
  • List transactions

Documentation: GoDoc

bosgo testserver requires Go version 1.7 or greater.

Getting started

Ensure you have a working Go installation and then use go get as follows:

go get -u code.bankrs.com/bosgo

Usage

This package contains several unit tests that demonstrate how to use the test server. The basic approach is as follows:

    // Create a test server with some default data
    s := testserver.NewWithDefaults()
    defer s.Close()


    // Create a client using the server-supplied HTTP client which is configured to accept the test server's TLS config
    appClient := bosgo.NewAppClient(s.Client(), s.Addr(), DefaultApplicationID)
 
   // The rest of this code is standard usage of bosgo and does not depend on the test server

    // Login as the default user
    userClient, err := appClient.Users.Login(DefaultUsername, DefaultPassword).Send()
    if err != nil {
         log.Fatalf("failed to login: %v", err)
    }

    // Prepare the request to add the access with challenge answers
    req := userClient.Accesses.Add(DefaultProviderID)
    req.ChallengeAnswer(bosgo.ChallengeAnswer{
        ID:    "login",
        Value: DefaultAccessLogin,
    })
    req.ChallengeAnswer(bosgo.ChallengeAnswer{
        ID:    "pin",
        Value: DefaultAccessPIN,
    })

    // Send the request
    job, err := req.Send()
    if err != nil {
         log.Fatalf("failed to send the add access request: %v", err)
    }

    // Check the status of the job to see if it has finished
    status, err := userClient.Jobs.Get(job.URI).Send()
    if err != nil {
         log.Fatalf("failed to get job status: %v", err)
    }
    if status.Finished != true {
        log.Fatalf("job not finished")
    }
    if len(status.Errors) != 0 {
        log.Fatalf("job status error: %v", status.Errors[0])
    }

    // List accesses for the user
    ac, err := userClient.Accesses.List().Send()
    if err != nil {
        log.Fatalf("failed to retrieve accesses: %v", err)
    }
    if len(ac.Accesses) != 1 {
        log.Fatalf("got %d accesses, wanted 1", len(ac.Accesses))
    }

Documentation

Index

Constants

View Source
const (
	ChallengeLogin = "login"
	ChallengePIN   = "pin"
	ChallengeTAN   = "tan"

	DefaultDeveloperID   = "default-dev"
	DefaultApplicationID = "default-app"
	DefaultUserID        = "default-user"
	DefaultUsername      = "username@example.com"
	DefaultPassword      = "password"
	DefaultProviderID    = "def-provider-id"
	DefaultAccessLogin   = "user"
	DefaultAccessPIN     = "1234"
	DefaultAuthMethod    = "901"
	DefaultAuthMessage   = "tan challenge - (enter 4321 as tan)"
	DefaultAuthAnswer    = "4321"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AccessDetails

type AccessDetails struct {
	Access                bosgo.Access
	Transactions          []bosgo.Transaction
	ScheduledTransactions []bosgo.Transaction
	RepeatedTransactions  []bosgo.RepeatedTransaction
	ChallengeMap          map[string]string
	TransferAuths         []TransferAuth
	StageProblems         map[bosgo.JobStage][]bosgo.Problem
}

type App

type App struct {
	ID          string
	DeveloperID string
}

type Dev

type Dev struct {
	ID string
}

type Job

type Job struct {
	ID              string
	UserID          string
	ProviderID      string
	Stage           bosgo.JobStage
	Error           string
	SuppliedAnswers []bosgo.ChallengeAnswer
	AccessDetails   AccessDetails
	Finished        bool
	NeedsAnswers    bool
	JobAction       JobAction
	Problems        []bosgo.Problem
}

type JobAction

type JobAction int
const (
	JobActionCreate JobAction = iota
	JobActionRefresh
)

type Logger

type Logger interface {
	Logf(format string, args ...interface{})
}

type OrderOp

type OrderOp int
const (
	OrderOpCreate OrderOp = iota
	OrderOpUpdate
	OrderOpDelete
)

type Server

type Server struct {
	Svr *httptest.Server

	Devs               map[string]Dev           // map of developers indexed by ID
	Apps               map[string]App           // map of applications indexed by ID
	Users              map[string]User          // map of users indexed by ID
	UserTokens         map[string]string        // map of user IDs indexed by token
	Jobs               map[string]Job           // map of jobs indexed by ID
	Accesses           map[string]AccessDetails // map of access details indexed by provider ID
	Transfers          map[string]TransferOrder // map of transfer orders indexed by ID
	RecurringTransfers map[string]TransferOrder // map of recurrings transfers orders indexed by ID
	// contains filtered or unexported fields
}

func New

func New() *Server

func NewWithDefaults

func NewWithDefaults() *Server

NewWithDefaults creates a new test server with a default developer, application and user account

func (*Server) AddAccess

func (s *Server) AddAccess(ad AccessDetails)

AddAccess adds configuration for an access with its transactions so it can be added to a user via the server API

func (*Server) Addr

func (s *Server) Addr() string

func (*Server) AssignAccess

func (s *Server) AssignAccess(username string, access *bosgo.Access) error

AssignAccess assigns a known access to a user

func (*Server) AssignRepeatedTransactions

func (s *Server) AssignRepeatedTransactions(username string, txs []bosgo.RepeatedTransaction) error

AssignRepeatedTransactions assigns a set of repeated transactions to a user, overwriting any existing transactions

func (*Server) AssignScheduledTransactions

func (s *Server) AssignScheduledTransactions(username string, txs []bosgo.Transaction) error

AssignScheduledTransactions assigns a set of scheduled transactions to a user, overwriting any existing transactions

func (*Server) AssignTransactions

func (s *Server) AssignTransactions(username string, txs []bosgo.Transaction) error

AssignTransactions assigns a set of transactions to a user, overwriting any existing transactions

func (*Server) Client

func (s *Server) Client() *http.Client

func (*Server) Close

func (s *Server) Close()

func (*Server) GetUser

func (s *Server) GetUser(id string) (User, bool)

func (*Server) GetUserByName

func (s *Server) GetUserByName(name string) (User, bool)

func (*Server) Logf

func (s *Server) Logf(format string, args ...interface{})

func (*Server) MakeAccess

func (s *Server) MakeAccess(providerID, name string) *bosgo.Access

MakeAccess makes an access with an account

func (*Server) ReadState

func (s *Server) ReadState(r io.Reader) error

ReadState reads a series of JSON documents from r and replaces the state of the server with the read data.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Server) SetConfirmSimilar

func (s *Server) SetConfirmSimilar(v bool)

SetConfirmSimilar sets the server to respond with the confirm_similar state for subsequent transfers

func (*Server) SetLogger

func (s *Server) SetLogger(logger Logger)

func (*Server) SetUser

func (s *Server) SetUser(user User)

func (*Server) URL

func (s *Server) URL() string

func (*Server) WriteState

func (s *Server) WriteState(w io.Writer) error

WriteState writes the current state of the server to w as a series of JSON documents.

type TransferAuth

type TransferAuth struct {
	Method  string
	Message string
	Answer  string
}

type TransferOrder

type TransferOrder struct {
	UserID         string
	Operation      OrderOp
	Type           bosgo.TransferType
	Transfer       bosgo.Transfer
	AccessDetails  AccessDetails
	ConfirmSimilar bool
}

type User

type User struct {
	ID                    string
	Username              string
	Password              string
	ApplicationID         string
	Accesses              []bosgo.Access
	Transactions          []bosgo.Transaction
	ScheduledTransactions []bosgo.Transaction
	RepeatedTransactions  []bosgo.RepeatedTransaction
	StoredAnswers         map[string][]bosgo.ChallengeAnswer // map of challenge answers indexed by provider ID
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL