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

     1  package main
     2  
     3  import (
     4  	"io"
     5  	"os/exec"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestHandlerChat(t *testing.T) {
    11  	h := newHandler()
    12  
    13  	var ranCmd string
    14  	h.Run = func(cmd *exec.Cmd) error {
    15  		ranCmd = strings.Join(cmd.Args, " ")
    16  		return nil
    17  	}
    18  	h.FindKeybaseBinary = func() (string, error) {
    19  		return "/mocked/test/path/keybase", nil
    20  	}
    21  
    22  	req := &Request{
    23  		Method: "chat",
    24  		Body:   "test message",
    25  		To:     "testkeybaseuser",
    26  	}
    27  
    28  	if _, err := h.Handle(req); err != nil {
    29  		t.Errorf("request failed: %q", err)
    30  	}
    31  
    32  	if ranCmd != "/mocked/test/path/keybase chat send --private testkeybaseuser" {
    33  		t.Errorf("unexpected command: %q", ranCmd)
    34  	}
    35  }
    36  
    37  const queryResponse = `[INFO] 001 Identifying sometestuser
    38  ✔ public key fingerprint: 9FCE A980 CCFD 3C13 E11E 88A9 3506 87D1 7E81 FD68
    39  ✔ admin of sometestuser.net via HTTPS: https://sometestuser.net/keybase.txt
    40  ✔ "sometestuser" on github: https://gist.github.com/10763855
    41  ✔ "sometestuser" on twitter: https://twitter.com/sometestuser/status/456154521052274689 [cached 2017-04-06 10:20:10 EDT]
    42  ✔ "sometestuser" on hackernews: https://news.ycombinator.com/user?id=sometestuser [cached 2017-04-06 10:20:09 EDT]
    43  ✔ "sometestuser" on reddit: https://www.reddit.com/r/KeybaseProofs/comments/2o8dbv/my_keybase_proof_redditsometestuser_keybasesometestuser/ [cached 2017-04-06 10:20:10 EDT]
    44  `
    45  
    46  const queryResponseErr = `[ERRO] 001 Not found
    47  `
    48  
    49  const queryResponseErrUnexpected = `[INFO] 001 Random progress message
    50  [ERRO] 002 Something unexpected happened
    51  `
    52  
    53  func TestHandlerQueryError(t *testing.T) {
    54  	h := newHandler()
    55  
    56  	var ranCmd string
    57  	h.Run = func(cmd *exec.Cmd) error {
    58  		ranCmd = strings.Join(cmd.Args, " ")
    59  		_ = ranCmd
    60  		_, err := io.WriteString(cmd.Stderr, queryResponseErr)
    61  		return err
    62  	}
    63  	h.FindKeybaseBinary = func() (string, error) {
    64  		return "/mocked/test/path/keybase", nil
    65  	}
    66  
    67  	req := &Request{
    68  		Method: "query",
    69  		To:     "doesnotexist",
    70  	}
    71  
    72  	_, err := h.Handle(req)
    73  	if err == nil {
    74  		t.Fatal("request succeeded when failure was expected")
    75  	}
    76  
    77  	if got, want := err.Error(), "user not found"; got != want {
    78  		t.Errorf("incorrect error; got: %q, want %q", got, want)
    79  	}
    80  }
    81  
    82  func TestHandlerQueryErrorUnexpected(t *testing.T) {
    83  	h := newHandler()
    84  
    85  	var ranCmd string
    86  	h.Run = func(cmd *exec.Cmd) error {
    87  		ranCmd = strings.Join(cmd.Args, " ")
    88  		_ = ranCmd
    89  		_, err := io.WriteString(cmd.Stderr, queryResponseErrUnexpected)
    90  		return err
    91  	}
    92  	h.FindKeybaseBinary = func() (string, error) {
    93  		return "/mocked/test/path/keybase", nil
    94  	}
    95  
    96  	req := &Request{
    97  		Method: "query",
    98  		To:     "doesnotexist",
    99  	}
   100  
   101  	_, err := h.Handle(req)
   102  	if err == nil {
   103  		t.Fatal("request succeeded when failure was expected")
   104  	}
   105  
   106  	if got, want := err.Error(), "unexpected error: Something unexpected happened"; got != want {
   107  		t.Errorf("incorrect error; got: %q, want %q", got, want)
   108  	}
   109  }
   110  
   111  func TestHandlerQuery(t *testing.T) {
   112  	h := newHandler()
   113  
   114  	var ranCmd string
   115  	h.Run = func(cmd *exec.Cmd) error {
   116  		ranCmd = strings.Join(cmd.Args, " ")
   117  		_, err := io.WriteString(cmd.Stderr, queryResponse)
   118  		return err
   119  	}
   120  	h.FindKeybaseBinary = func() (string, error) {
   121  		return "/mocked/test/path/keybase", nil
   122  	}
   123  
   124  	req := &Request{
   125  		Method: "query",
   126  		To:     "sometestuser",
   127  	}
   128  
   129  	res, err := h.Handle(req)
   130  	if err != nil {
   131  		t.Errorf("request failed: %q", err)
   132  	}
   133  	result, ok := res.(*resultQuery)
   134  	if !ok {
   135  		t.Errorf("result is not *resultQuery: %T", res)
   136  	}
   137  
   138  	if ranCmd != "/mocked/test/path/keybase id sometestuser" {
   139  		t.Errorf("unexpected command: %q", ranCmd)
   140  	}
   141  
   142  	if result == nil {
   143  		t.Fatal("result is nil")
   144  	}
   145  
   146  	if result.Username != "sometestuser" {
   147  		t.Errorf("invalid result value: %q", result)
   148  	}
   149  }
   150  
   151  func TestCleanCmdArg(t *testing.T) {
   152  	testcases := []struct {
   153  		Input string
   154  		Err   error
   155  	}{
   156  		{"shazow@reddit", nil},
   157  		{"shazow:twitter.com", nil},
   158  		{`abcABC123_@.`, nil},
   159  		{``, errMissingField},
   160  		{`a-bc${foo} bar`, errInvalidInput},
   161  		{"foo\nbar", errInvalidInput},
   162  		{"foo ", errInvalidInput},
   163  	}
   164  
   165  	for i, test := range testcases {
   166  		if _, err := checkUsernameQuery(test.Input); err != test.Err {
   167  			t.Errorf("case %d: got %q; want %q", i, err, test.Err)
   168  		}
   169  	}
   170  }