github.com/gfleury/gobbs@v0.0.0-20200831213239-44ca2b94c1a1/search/search_test.go (about)

     1  package search
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"os"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/gfleury/gobbs/common"
    12  
    13  	bbmock "github.com/gfleury/go-bitbucket-v1/test/bb-mock-server/go"
    14  	"gopkg.in/check.v1"
    15  )
    16  
    17  func Test(t *testing.T) { check.TestingT(t) }
    18  
    19  type S struct {
    20  	host, project, repo        *string
    21  	stashInfo                  common.StashInfo
    22  	originalStdout, readStdout *os.File
    23  }
    24  
    25  var _ = check.Suite(&S{})
    26  
    27  func (s *S) TearDownSuite(c *check.C) {
    28  }
    29  
    30  func (s *S) SetUpSuite(c *check.C) {
    31  	s.stashInfo = common.StashInfo{}
    32  	s.host = s.stashInfo.Host()
    33  	s.project = s.stashInfo.Project()
    34  	s.repo = s.stashInfo.Repo()
    35  
    36  	*s.stashInfo.Timeout() = 2
    37  
    38  	*s.project = "PRJ1"
    39  	*s.repo = "my-repository"
    40  
    41  	*s.stashInfo.Credential().Passwd() = "passwd"
    42  	*s.stashInfo.Credential().User() = "test"
    43  
    44  	go func() {
    45  		err := bbmock.RunServer(7995)
    46  		if err != nil {
    47  			c.Assert(err, check.IsNil)
    48  		}
    49  	}()
    50  
    51  	for err := fmt.Errorf(""); err != nil; _, err = http.Get("http://localhost:7995/") {
    52  
    53  		time.Sleep(time.Second)
    54  	}
    55  
    56  }
    57  
    58  func (s *S) mockStdout() {
    59  	// Create Fake Buffer for Stdout
    60  	s.originalStdout = os.Stdout
    61  	var w *os.File
    62  	s.readStdout, w, _ = os.Pipe()
    63  	os.Stdout = w
    64  }
    65  
    66  func (s *S) closeMockStdout() {
    67  	os.Stdout.Close()
    68  	os.Stdout = s.originalStdout
    69  }
    70  
    71  func (s *S) TestListInvalidHost(c *check.C) {
    72  	*s.host = "http://localhost:7992"
    73  
    74  	os.Args = []string{"users", "list"}
    75  
    76  	ctx := common.APIClientContext(&s.stashInfo)
    77  	err := Search.ExecuteContext(ctx)
    78  	c.Assert(err, check.ErrorMatches, ".*connect: connection refused")
    79  }
    80  
    81  func (s *S) TestListInvalidHostTimeouted(c *check.C) {
    82  	*s.host = "http://localhost:7992"
    83  
    84  	os.Args = []string{"users", "list"}
    85  
    86  	*s.stashInfo.Timeout() = 0
    87  
    88  	ctx := common.APIClientContext(&s.stashInfo)
    89  	err := Search.ExecuteContext(ctx)
    90  	c.Assert(err, check.ErrorMatches, ".*context deadline exceeded")
    91  	*s.stashInfo.Timeout() = 2
    92  }
    93  
    94  func (s *S) TestListValidHost(c *check.C) {
    95  	*s.host = "http://localhost:7995"
    96  
    97  	os.Args = []string{"search", "-N", "git", "clone"}
    98  
    99  	s.mockStdout()
   100  
   101  	ctx := common.APIClientContext(&s.stashInfo)
   102  	err := Search.ExecuteContext(ctx)
   103  	c.Assert(err, check.IsNil)
   104  
   105  	s.closeMockStdout()
   106  
   107  	got, err := ioutil.ReadAll(s.readStdout)
   108  	c.Assert(err, check.IsNil)
   109  
   110  	want, err := ioutil.ReadFile("mocked_responses/search.output")
   111  	c.Assert(err, check.IsNil)
   112  
   113  	c.Assert(string(got), check.Equals, string(want))
   114  }