github.com/triarius/goreleaser@v1.12.5/internal/pipe/linkedin/client_test.go (about)

     1  package linkedin
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/triarius/goreleaser/pkg/config"
    11  	"github.com/triarius/goreleaser/pkg/context"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestCreateLinkedInClient(t *testing.T) {
    16  	tests := []struct {
    17  		name    string
    18  		cfg     oauthClientConfig
    19  		wantErr error
    20  	}{
    21  		{
    22  			"non-empty context and access token",
    23  			oauthClientConfig{
    24  				Context:     context.New(config.Project{}),
    25  				AccessToken: "foo",
    26  			},
    27  			nil,
    28  		},
    29  		{
    30  			"empty context",
    31  			oauthClientConfig{
    32  				Context:     nil,
    33  				AccessToken: "foo",
    34  			},
    35  			fmt.Errorf("context is nil"),
    36  		},
    37  		{
    38  			"empty access token",
    39  			oauthClientConfig{
    40  				Context:     context.New(config.Project{}),
    41  				AccessToken: "",
    42  			},
    43  			fmt.Errorf("empty access token"),
    44  		},
    45  	}
    46  	for _, tt := range tests {
    47  		t.Run(tt.name, func(t *testing.T) {
    48  			_, err := createLinkedInClient(tt.cfg)
    49  			if tt.wantErr != nil {
    50  				require.EqualError(t, err, tt.wantErr.Error())
    51  			} else {
    52  				require.NoError(t, err)
    53  			}
    54  		})
    55  	}
    56  }
    57  
    58  func TestClient_Share(t *testing.T) {
    59  	server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
    60  		_, _ = io.WriteString(rw, `
    61  {
    62  	"id": "foo",
    63  	"activity": "123456789"
    64  }
    65  `)
    66  	}))
    67  	defer server.Close()
    68  
    69  	c, err := createLinkedInClient(oauthClientConfig{
    70  		Context:     context.New(config.Project{}),
    71  		AccessToken: "foo",
    72  	})
    73  	if err != nil {
    74  		t.Fatalf("could not create client: %v", err)
    75  	}
    76  
    77  	c.baseURL = server.URL
    78  
    79  	link, err := c.Share("test")
    80  	if err != nil {
    81  		t.Fatalf("could not share: %v", err)
    82  	}
    83  
    84  	wantLink := "https://www.linkedin.com/feed/update/123456789"
    85  	require.Equal(t, wantLink, link)
    86  }