github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/libkb/login_session_test.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package libkb
     5  
     6  import (
     7  	"bytes"
     8  	"context"
     9  	"encoding/json"
    10  	"fmt"
    11  	"io"
    12  	"net/http"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/keybase/clockwork"
    17  	jsonw "github.com/keybase/go-jsonw"
    18  )
    19  
    20  const fakeResponse = `{
    21  	"status": {
    22  		"code": 0,
    23  		"name": "OK"
    24  	},
    25  	"salt": "1a507c4a3c083e6a6e72d79f71ea99e8",
    26  	"login_session": "lgHZIGRiYjE2NWI3ODc5ZmU3YjExNzRkZjczYmVkMGI5NTAwzlaYTrLNCWDAxCBqkB8LnhG44ndNkVgHvRUoVbDUn1FMaIRPMUC1LCROmw==",
    27  	"pwh_version": 3,
    28  	"csrf_token": "lgHZIDY3M2E3NDBjZDIwZmI0YmQzNDg3MzhiMTZkMjI4MjE5zlaYTmPOAAFRgMDEIITHl1hPEaTsnia433+727nztAC/Td62RJY8K8tBCoGZ"
    29  }`
    30  
    31  type FakeAPI struct{}
    32  
    33  func (a *FakeAPI) Get(mctx MetaContext, arg APIArg) (*APIRes, error) {
    34  
    35  	decoder := json.NewDecoder(bytes.NewBufferString(fakeResponse))
    36  	var obj interface{}
    37  	decoder.UseNumber()
    38  	err := decoder.Decode(&obj)
    39  	if err != nil {
    40  		err = fmt.Errorf("Error in parsing JSON reply from server: %s", err)
    41  		return nil, err
    42  	}
    43  
    44  	jw := jsonw.NewWrapper(obj)
    45  
    46  	status, err := jw.AtKey("status").ToDictionary()
    47  	if err != nil {
    48  		err = fmt.Errorf("Cannot parse server's 'status' field: %s", err)
    49  		return nil, err
    50  	}
    51  
    52  	body := jw
    53  	return &APIRes{status, body, 200, nil}, err
    54  
    55  }
    56  
    57  func (a *FakeAPI) GetDecode(mctx MetaContext, arg APIArg, v APIResponseWrapper) error {
    58  	return fmt.Errorf("GetDecode is phony")
    59  }
    60  
    61  func (a *FakeAPI) GetDecodeCtx(ctx context.Context, arg APIArg, v APIResponseWrapper) error {
    62  	return fmt.Errorf("GetDecode is phony")
    63  }
    64  
    65  func (a *FakeAPI) GetResp(MetaContext, APIArg) (*http.Response, func(), error) {
    66  	return nil, noopFinisher, fmt.Errorf("GetResp is phony")
    67  }
    68  
    69  func (a *FakeAPI) Post(MetaContext, APIArg) (*APIRes, error) {
    70  	return nil, fmt.Errorf("Post is phony")
    71  }
    72  
    73  func (a *FakeAPI) PostJSON(MetaContext, APIArg) (*APIRes, error) {
    74  	return nil, fmt.Errorf("PostJSON is phony")
    75  }
    76  
    77  func (a *FakeAPI) PostRaw(MetaContext, APIArg, string, io.Reader) (*APIRes, error) {
    78  	return nil, fmt.Errorf("PostRaw is phony")
    79  }
    80  
    81  func (a *FakeAPI) PostDecode(MetaContext, APIArg, APIResponseWrapper) error {
    82  	return fmt.Errorf("GetDecode is phony")
    83  }
    84  
    85  func (a *FakeAPI) PostDecodeCtx(ctx context.Context, arg APIArg, v APIResponseWrapper) error {
    86  	return fmt.Errorf("GetDecode is phony")
    87  }
    88  
    89  func (a *FakeAPI) Delete(MetaContext, APIArg) (*APIRes, error) {
    90  	return nil, fmt.Errorf("Delete is phony")
    91  }
    92  
    93  func TestLoginSessionTimeout(t *testing.T) {
    94  	tc := SetupTest(t, "login_session_test", 1)
    95  	defer tc.Cleanup()
    96  
    97  	tc.G.API = &FakeAPI{}
    98  	c := clockwork.NewFakeClock()
    99  	tc.G.SetClock(c)
   100  
   101  	sesh := NewLoginSession(tc.G, "logintest")
   102  	err := sesh.Load(NewMetaContextForTest(tc))
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  	if !sesh.NotExpired() {
   107  		t.Fatal("Fresh LoginSession says expired")
   108  	}
   109  	c.Advance(LoginSessionMemoryTimeout + 1*time.Second)
   110  	tc.G.SetClock(c) // ??
   111  	if sesh.NotExpired() {
   112  		t.Fatal("Stale LoginSession says not expired")
   113  	}
   114  }