github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/libkb/invite_test.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  package libkb
     5  
     6  import (
     7  	"testing"
     8  
     9  	jsonw "github.com/keybase/go-jsonw"
    10  )
    11  
    12  func TestInvitationArgs(t *testing.T) {
    13  	tc := SetupTest(t, "invite", 1)
    14  	defer tc.Cleanup()
    15  
    16  	rec := newSendInvitationMock()
    17  	tc.G.API = rec
    18  	mctx := NewMetaContextForTest(tc)
    19  
    20  	email := "email@nomail.keybase.io"
    21  	inv, err := SendInvitation(mctx, email, InviteArg{Message: "message", NoteToSelf: "note"})
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	if len(rec.Records) != 1 {
    26  		t.Fatalf("recorded args: %d, expected 1", len(rec.Records))
    27  	}
    28  	checkArg(t, rec.Records[0].Arg)
    29  	checkHTTPArg(t, rec.Records[0].Arg, "email", email)
    30  	checkHTTPArg(t, rec.Records[0].Arg, "invitation_message", "message")
    31  	checkHTTPArg(t, rec.Records[0].Arg, "note_to_self", "note")
    32  	checkInvitation(t, inv)
    33  
    34  	rec.Reset()
    35  
    36  	inv, err = GenerateInvitationCode(mctx, InviteArg{})
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	if len(rec.Records) != 1 {
    41  		t.Fatalf("recorded args: %d, expected 1", len(rec.Records))
    42  	}
    43  	checkArg(t, rec.Records[0].Arg)
    44  	checkHTTPArg(t, rec.Records[0].Arg, "invitation_message", "")
    45  	checkHTTPArg(t, rec.Records[0].Arg, "note_to_self", "")
    46  	checkInvitation(t, inv)
    47  
    48  	rec.Reset()
    49  
    50  	assertion, ok := NormalizeSocialAssertion(testAssertionContext{}, "twitter:KeyBase")
    51  	if !ok {
    52  		t.Fatal("invalid social assertion")
    53  	}
    54  	inv, err = GenerateInvitationCodeForAssertion(mctx, assertion, InviteArg{})
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	if len(rec.Records) != 1 {
    59  		t.Fatalf("recorded args: %d, expected 1", len(rec.Records))
    60  	}
    61  	checkArg(t, rec.Records[0].Arg)
    62  	checkHTTPArg(t, rec.Records[0].Arg, "assertion", "keybase@twitter")
    63  	checkHTTPArg(t, rec.Records[0].Arg, "invitation_message", "")
    64  	checkHTTPArg(t, rec.Records[0].Arg, "note_to_self", "")
    65  	checkInvitation(t, inv)
    66  }
    67  
    68  func checkArg(t *testing.T, arg APIArg) {
    69  	if arg.Endpoint != "send_invitation" {
    70  		t.Errorf("endpoint: %s, expected send_invitation", arg.Endpoint)
    71  	}
    72  	if arg.SessionType != APISessionTypeREQUIRED {
    73  		t.Errorf("SessionType should be APISessionTypeREQUIRED")
    74  	}
    75  }
    76  
    77  func checkHTTPArg(t *testing.T, arg APIArg, key, value string) {
    78  	if arg.Args[key].String() != value {
    79  		t.Errorf("%s parameter: %q, expected %q", key, arg.Args[key], value)
    80  	}
    81  
    82  }
    83  
    84  func checkInvitation(t *testing.T, inv *Invitation) {
    85  	if inv.ID != "2b25175f6da1d9155f23800d" {
    86  		t.Errorf("invitation id: %q, expected 2b25175f6da1d9155f23800d", inv.ID)
    87  	}
    88  	if inv.ShortCode != "clip outside broccoli culture" {
    89  		t.Errorf("short code: %q, expected clip outside broccoli culture", inv.ShortCode)
    90  	}
    91  }
    92  
    93  type sendInvitationMock struct {
    94  	*APIArgRecorder
    95  }
    96  
    97  func newSendInvitationMock() *sendInvitationMock {
    98  	return &sendInvitationMock{NewAPIArgRecorderWithNullAPI()}
    99  }
   100  
   101  func (s *sendInvitationMock) Post(mctx MetaContext, arg APIArg) (*APIRes, error) {
   102  	if _, err := s.APIArgRecorder.Post(mctx, arg); err != nil {
   103  		return nil, err
   104  	}
   105  	jw, err := jsonw.Unmarshal([]byte(`{"status":{"code":0,"name":"OK"},"short_code":"clip outside broccoli culture","invitation_id":"2b25175f6da1d9155f23800d","csrf_token":"lgHZIDBlNjRhNDBhOTQ3ZWYyMTMxOWQ4MzM1Y2M4YjQ1YjE5zlcVNMHOAAFRgMDEIFyHOIg/AetihKRvVMNT2NoBNNG1QoCVxtDfzEK7/rdF"}`))
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  	return &APIRes{
   110  		Status:     jw.AtKey("status"),
   111  		Body:       jw,
   112  		HTTPStatus: 200,
   113  		AppStatus: &AppStatus{
   114  			Code: SCOk,
   115  			Name: "OK",
   116  			Desc: "Ok",
   117  		},
   118  	}, nil
   119  }