github.com/Ptt-official-app/go-bbs@v0.12.0/serverlet/route_users_test.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/Ptt-official-app/go-bbs"
     5  
     6  	"encoding/json"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  type MockUserRecord struct {
    14  	userID string
    15  }
    16  
    17  func NewMockUserRecord(userID string) *MockUserRecord { return &MockUserRecord{userID: userID} }
    18  func (u *MockUserRecord) UserID() string              { return u.userID }
    19  
    20  // HashedPassword return user hashed password, it only for debug,
    21  // If you want to check is user password correct, please use
    22  // VerifyPassword insteaded.
    23  func (u *MockUserRecord) HashedPassword() string { return "" }
    24  
    25  // VerifyPassword will check user's password is OK. it will return null
    26  // when OK and error when there are something wrong
    27  func (u *MockUserRecord) VerifyPassword(password string) error { return nil }
    28  
    29  // Nickname return a string for user's nickname, this string may change
    30  // depend on user's mood, return empty string if this bbs system do not support
    31  func (u *MockUserRecord) Nickname() string { return "" }
    32  
    33  // RealName return a string for user's real name, this string may not be changed
    34  // return empty string if this bbs system do not support
    35  func (u *MockUserRecord) RealName() string { return "" }
    36  
    37  // NumLoginDays return how many days this have been login since account created.
    38  func (u *MockUserRecord) NumLoginDays() int { return 0 }
    39  
    40  // NumPosts return how many posts this user has posted.
    41  func (u *MockUserRecord) NumPosts() int { return 0 }
    42  
    43  // Money return the money this user have.
    44  func (u *MockUserRecord) Money() int { return 0 }
    45  
    46  // LastLogin return last login time of user
    47  func (u *MockUserRecord) LastLogin() time.Time { return time.Now() }
    48  
    49  // LastHost return last login host of user, it is IPv4 address usually, but it
    50  // could be domain name or IPv6 address.
    51  func (u *MockUserRecord) LastHost() string { return "" }
    52  
    53  // UserFlag return user setting.
    54  // uint32, see https://github.com/ptt/pttbbs/blob/master/include/uflags.h
    55  func (u *MockUserRecord) UserFlag() uint32 { return 0x00000001 }
    56  
    57  func TestGetUserInformation(t *testing.T) {
    58  
    59  	expected := NewMockUserRecord("SYSOP")
    60  
    61  	userRecs = []bbs.UserRecord{
    62  		expected,
    63  	}
    64  
    65  	req, err := http.NewRequest("GET", "/v1/users/SYSOP/information", nil)
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  
    70  	token := newAccessTokenWithUsername(expected.UserID())
    71  	t.Logf("testing token: %v", token)
    72  	req.Header.Add("Authorization", "bearer "+token)
    73  
    74  	rr := httptest.NewRecorder()
    75  	r := http.NewServeMux()
    76  	r.HandleFunc("/v1/users/", routeUsers)
    77  	r.ServeHTTP(rr, req)
    78  
    79  	if status := rr.Code; status != http.StatusOK {
    80  		t.Errorf("handler returned wrong status code: got %v want %v",
    81  			status, http.StatusOK)
    82  	}
    83  
    84  	responsedMap := map[string]interface{}{}
    85  	json.Unmarshal(rr.Body.Bytes(), &responsedMap)
    86  	t.Logf("got response %v", rr.Body.String())
    87  	responsedData := responsedMap["data"].(map[string]interface{})
    88  	if responsedData["user_id"] != expected.UserID() {
    89  		t.Errorf("handler returned unexpected body, user_id not match: got %v want userID %v",
    90  			rr.Body.String(), expected.UserID())
    91  
    92  	}
    93  
    94  }
    95  
    96  func TestParseUserPath(t *testing.T) {
    97  
    98  	type TestCase struct {
    99  		input         string
   100  		expectdUserID string
   101  		expectdItem   string
   102  	}
   103  
   104  	cases := []TestCase{
   105  		{
   106  			input:         "/v1/users/Pichu/information",
   107  			expectdUserID: "Pichu",
   108  			expectdItem:   "information",
   109  		},
   110  		{
   111  			input:         "/v1/users/Pichu/",
   112  			expectdUserID: "Pichu",
   113  			expectdItem:   "",
   114  		},
   115  		{
   116  			input:         "/v1/users/Pichu",
   117  			expectdUserID: "Pichu",
   118  			expectdItem:   "",
   119  		},
   120  	}
   121  
   122  	for index, c := range cases {
   123  		input := c.input
   124  		expectdUserID := c.expectdUserID
   125  		expectdItem := c.expectdItem
   126  		actualUserID, actualItem, err := parseUserPath(input)
   127  		if err != nil {
   128  			t.Errorf("error on index %d, got: %v", index, err)
   129  
   130  		}
   131  
   132  		if actualUserID != expectdUserID {
   133  			t.Errorf("userID not match on index %d, expected: %v, got: %v", index, expectdUserID, actualUserID)
   134  		}
   135  
   136  		if actualItem != expectdItem {
   137  			t.Errorf("item not match on index %d, expected: %v, got: %v", index, expectdItem, actualItem)
   138  		}
   139  
   140  	}
   141  
   142  }