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

     1  package repos
     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(7991)
    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:7991/") {
    52  		time.Sleep(time.Second)
    53  	}
    54  
    55  }
    56  
    57  func (s *S) mockStdout() {
    58  	// Create Fake Buffer for Stdout
    59  	s.originalStdout = os.Stdout
    60  	var w *os.File
    61  	s.readStdout, w, _ = os.Pipe()
    62  	os.Stdout = w
    63  }
    64  
    65  func (s *S) closeMockStdout() {
    66  	os.Stdout.Close()
    67  	os.Stdout = s.originalStdout
    68  }
    69  
    70  func (s *S) TestListInvalidHost(c *check.C) {
    71  	*s.host = "http://localhost:7992"
    72  
    73  	os.Args = []string{"users", "list"}
    74  
    75  	ctx := common.APIClientContext(&s.stashInfo)
    76  	err := List.ExecuteContext(ctx)
    77  	c.Assert(err, check.ErrorMatches, ".*connect: connection refused")
    78  }
    79  
    80  func (s *S) TestListInvalidHostTimeouted(c *check.C) {
    81  	*s.host = "http://localhost:7992"
    82  
    83  	os.Args = []string{"users", "list"}
    84  
    85  	*s.stashInfo.Timeout() = 0
    86  
    87  	ctx := common.APIClientContext(&s.stashInfo)
    88  	err := List.ExecuteContext(ctx)
    89  	c.Assert(err, check.ErrorMatches, ".*context deadline exceeded")
    90  	*s.stashInfo.Timeout() = 2
    91  }
    92  
    93  func (s *S) TestListValidHost(c *check.C) {
    94  	*s.host = "http://localhost:7991"
    95  
    96  	os.Args = []string{"users", "list"}
    97  
    98  	s.mockStdout()
    99  
   100  	ctx := common.APIClientContext(&s.stashInfo)
   101  	err := List.ExecuteContext(ctx)
   102  	c.Assert(err, check.IsNil)
   103  
   104  	s.closeMockStdout()
   105  
   106  	got, err := ioutil.ReadAll(s.readStdout)
   107  	c.Assert(err, check.IsNil)
   108  
   109  	want, err := ioutil.ReadFile("mocked_responses/repos_list.output")
   110  	c.Assert(err, check.IsNil)
   111  
   112  	c.Assert(string(got), check.Equals, string(want))
   113  }