github.com/covergates/covergates@v0.2.2-0.20201009050117-42ef8a19fb95/modules/scm/repo_github_test.go (about)

     1  // +build github
     2  
     3  package scm
     4  
     5  import (
     6  	"context"
     7  	"net/http"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/covergates/covergates/core"
    12  	"github.com/drone/go-scm/scm"
    13  	"github.com/drone/go-scm/scm/driver/github"
    14  	"github.com/drone/go-scm/scm/transport/oauth2"
    15  )
    16  
    17  func getGithubClient() *scm.Client {
    18  	client, _ := github.New("https://api.github.com")
    19  	client.Client = &http.Client{
    20  		Transport: &oauth2.Transport{
    21  			Scheme: oauth2.SchemeBearer,
    22  			Source: &oauth2.Refresher{
    23  				Source: oauth2.ContextTokenSource(),
    24  			},
    25  		},
    26  	}
    27  	return client
    28  }
    29  
    30  func TestGithubList(t *testing.T) {
    31  	user := &core.User{
    32  		GithubToken: os.Getenv("GITHUB_SECRET"),
    33  	}
    34  	service := &repoService{
    35  		client: getGithubClient(),
    36  		scm:    core.Github,
    37  	}
    38  	repo, err := service.List(context.Background(), user)
    39  	if err != nil {
    40  		t.Error(err)
    41  		return
    42  	}
    43  	if len(repo) <= 0 {
    44  		t.Fail()
    45  	}
    46  }
    47  
    48  func TestGithubFind(t *testing.T) {
    49  
    50  	service := repoService{
    51  		client: getGithubClient(),
    52  		scm:    core.Github,
    53  	}
    54  	user := &core.User{
    55  		GithubToken: os.Getenv("GITHUB_SECRET"),
    56  	}
    57  	repo, err := service.Find(context.Background(), user, "blueworrybear/livelogs")
    58  	if err != nil {
    59  		t.Error(err)
    60  		return
    61  	}
    62  	if repo.NameSpace != "blueworrybear" || repo.Name != "livelogs" {
    63  		t.Fail()
    64  	}
    65  	if repo.URL == "" {
    66  		t.Fail()
    67  	}
    68  }
    69  
    70  func TestGithubRepoCloneURL(t *testing.T) {
    71  	service := repoService{
    72  		client: getGithubClient(),
    73  		scm:    core.Github,
    74  	}
    75  	user := &core.User{
    76  		GithubToken: os.Getenv("GITHUB_SECRET"),
    77  	}
    78  	url, err := service.CloneURL(context.Background(), user, "blueworrybear/livelogs")
    79  	if err != nil {
    80  		t.Error(err)
    81  		return
    82  	}
    83  	if url == "" {
    84  		t.Fail()
    85  	}
    86  }