github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/pvl/api_stub.go (about)

     1  // Copyright 2016 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  //go:build !production
     5  // +build !production
     6  
     7  package pvl
     8  
     9  import (
    10  	"fmt"
    11  
    12  	libkb "github.com/keybase/client/go/libkb"
    13  )
    14  
    15  type stubAPIEngine struct {
    16  	expectations map[string]stubAPIEngineExpectation
    17  	calls        []stubAPIEngineCallRecord
    18  }
    19  
    20  type stubAPIEngineExpectation struct {
    21  	JSON *libkb.ExternalAPIRes
    22  	HTML *libkb.ExternalHTMLRes
    23  	Text *libkb.ExternalTextRes
    24  }
    25  
    26  type stubAPIEngineCallRecord struct {
    27  	kind     libkb.XAPIResType
    28  	endpoint string
    29  }
    30  
    31  func newStubAPIEngine() *stubAPIEngine {
    32  	return &stubAPIEngine{
    33  		expectations: make(map[string]stubAPIEngineExpectation),
    34  		calls:        make([]stubAPIEngineCallRecord, 0),
    35  	}
    36  }
    37  
    38  func (e *stubAPIEngine) Get(m libkb.MetaContext, arg libkb.APIArg) (*libkb.ExternalAPIRes, error) {
    39  	res, _, _, err := e.getMock(arg, libkb.XAPIResJSON)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	return res, nil
    44  
    45  }
    46  
    47  func (e *stubAPIEngine) GetHTML(m libkb.MetaContext, arg libkb.APIArg) (*libkb.ExternalHTMLRes, error) {
    48  	_, res, _, err := e.getMock(arg, libkb.XAPIResHTML)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	return res, nil
    53  }
    54  
    55  func (e *stubAPIEngine) GetText(m libkb.MetaContext, arg libkb.APIArg) (*libkb.ExternalTextRes, error) {
    56  	_, _, res, err := e.getMock(arg, libkb.XAPIResText)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  	return res, nil
    61  }
    62  
    63  func (e *stubAPIEngine) Post(m libkb.MetaContext, arg libkb.APIArg) (*libkb.ExternalAPIRes, error) {
    64  	return nil, fmt.Errorf("unsupported operation Post for stub api")
    65  }
    66  
    67  func (e *stubAPIEngine) PostHTML(m libkb.MetaContext, arg libkb.APIArg) (*libkb.ExternalHTMLRes, error) {
    68  	return nil, fmt.Errorf("unsupported operation Post for stub api")
    69  }
    70  
    71  func (e *stubAPIEngine) Set(endpoint string, canned *libkb.ExternalAPIRes) {
    72  	e.setCommon(endpoint, canned, nil, nil)
    73  }
    74  
    75  func (e *stubAPIEngine) SetHTML(endpoint string, canned *libkb.ExternalHTMLRes) {
    76  	e.setCommon(endpoint, nil, canned, nil)
    77  }
    78  
    79  func (e *stubAPIEngine) SetText(endpoint string, canned *libkb.ExternalTextRes) {
    80  	e.setCommon(endpoint, nil, nil, canned)
    81  }
    82  
    83  func (e *stubAPIEngine) setCommon(endpoint string, e1 *libkb.ExternalAPIRes, e2 *libkb.ExternalHTMLRes, e3 *libkb.ExternalTextRes) {
    84  	entry := e.expectations[endpoint]
    85  	if e1 != nil {
    86  		entry.JSON = e1
    87  	}
    88  	if e2 != nil {
    89  		entry.HTML = e2
    90  	}
    91  	if e3 != nil {
    92  		entry.Text = e3
    93  	}
    94  	e.expectations[endpoint] = entry
    95  }
    96  
    97  func (e *stubAPIEngine) ResetCalls() {
    98  	e.calls = make([]stubAPIEngineCallRecord, 0)
    99  }
   100  
   101  func (e *stubAPIEngine) AssertCalledOnceWith(kind libkb.XAPIResType, endpoint string) error {
   102  	if len(e.calls) == 0 {
   103  		return fmt.Errorf("stub api not called")
   104  	}
   105  	if len(e.calls) > 1 {
   106  		return fmt.Errorf("stub api called more than once")
   107  	}
   108  	call := e.calls[0]
   109  	expected := stubAPIEngineCallRecord{kind, endpoint}
   110  	if call != expected {
   111  		return fmt.Errorf("stub api called with wrong arguments\n  expected:  %v %v\ngot: %v %v",
   112  			expected.kind, expected.endpoint, call.kind, call.endpoint)
   113  	}
   114  	return nil
   115  }
   116  
   117  func (e *stubAPIEngine) AssertCalledWith(kind libkb.XAPIResType, endpoint string) error {
   118  	if len(e.calls) == 0 {
   119  		return fmt.Errorf("stub api not called")
   120  	}
   121  	ok := false
   122  	for _, call := range e.calls {
   123  		expected := stubAPIEngineCallRecord{kind, endpoint}
   124  		if call == expected {
   125  			ok = true
   126  		}
   127  	}
   128  	if ok {
   129  		return nil
   130  	}
   131  	return fmt.Errorf("stub api never called with arguments: %v %v",
   132  		kind, endpoint)
   133  }
   134  
   135  func (e *stubAPIEngine) getMock(arg libkb.APIArg, restype libkb.XAPIResType) (
   136  	*libkb.ExternalAPIRes, *libkb.ExternalHTMLRes, *libkb.ExternalTextRes, error) {
   137  	e.calls = append(e.calls, stubAPIEngineCallRecord{
   138  		kind:     restype,
   139  		endpoint: arg.Endpoint,
   140  	})
   141  
   142  	entry := e.expectations[arg.Endpoint]
   143  	okjson := entry.JSON != nil
   144  	okhtml := entry.HTML != nil
   145  	oktext := entry.Text != nil
   146  
   147  	ok := (okjson || (restype != libkb.XAPIResJSON)) &&
   148  		(okhtml || (restype != libkb.XAPIResHTML)) &&
   149  		(oktext || (restype != libkb.XAPIResText))
   150  	if !ok {
   151  		return nil, nil, nil, fmt.Errorf("unexpected api call: %v %v", restype, arg.Endpoint)
   152  	}
   153  
   154  	return entry.JSON, entry.HTML, entry.Text, nil
   155  }