github.com/google/go-github/v33@v33.0.0/github/issue_import_test.go (about)

     1  // Copyright 2020 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"encoding/json"
    11  	"fmt"
    12  	"net/http"
    13  	"reflect"
    14  	"testing"
    15  	"time"
    16  )
    17  
    18  func TestIssueImportService_Create(t *testing.T) {
    19  	client, mux, _, teardown := setup()
    20  	defer teardown()
    21  
    22  	createdAt := time.Date(2020, time.August, 11, 15, 30, 0, 0, time.UTC)
    23  	input := &IssueImportRequest{
    24  		IssueImport: IssueImport{
    25  			Assignee:  String("developer"),
    26  			Body:      "Dummy description",
    27  			CreatedAt: &createdAt,
    28  			Labels:    []string{"l1", "l2"},
    29  			Milestone: Int(1),
    30  			Title:     "Dummy Issue",
    31  		},
    32  		Comments: []*Comment{{
    33  			CreatedAt: &createdAt,
    34  			Body:      "Comment body",
    35  		}},
    36  	}
    37  
    38  	mux.HandleFunc("/repos/o/r/import/issues", func(w http.ResponseWriter, r *http.Request) {
    39  		v := new(IssueImportRequest)
    40  		json.NewDecoder(r.Body).Decode(v)
    41  		testMethod(t, r, "POST")
    42  		testHeader(t, r, "Accept", mediaTypeIssueImportAPI)
    43  		if !reflect.DeepEqual(v, input) {
    44  			t.Errorf("Request body = %+v, want %+v", v, input)
    45  		}
    46  
    47  		w.WriteHeader(http.StatusAccepted)
    48  		w.Write(issueImportResponseJSON)
    49  	})
    50  
    51  	got, _, err := client.IssueImport.Create(context.Background(), "o", "r", input)
    52  	if err != nil {
    53  		t.Errorf("Create returned error: %v", err)
    54  	}
    55  
    56  	want := wantIssueImportResponse
    57  	if !reflect.DeepEqual(got, want) {
    58  		t.Errorf("Create = %+v, want %+v", got, want)
    59  	}
    60  }
    61  
    62  func TestIssueImportService_Create_invalidOwner(t *testing.T) {
    63  	client, _, _, teardown := setup()
    64  	defer teardown()
    65  
    66  	_, _, err := client.IssueImport.Create(context.Background(), "%", "r", nil)
    67  	testURLParseError(t, err)
    68  }
    69  
    70  func TestIssueImportService_CheckStatus(t *testing.T) {
    71  	client, mux, _, teardown := setup()
    72  	defer teardown()
    73  
    74  	mux.HandleFunc("/repos/o/r/import/issues/3", func(w http.ResponseWriter, r *http.Request) {
    75  		testMethod(t, r, "GET")
    76  		testHeader(t, r, "Accept", mediaTypeIssueImportAPI)
    77  		w.WriteHeader(http.StatusOK)
    78  		w.Write(issueImportResponseJSON)
    79  	})
    80  
    81  	got, _, err := client.IssueImport.CheckStatus(context.Background(), "o", "r", 3)
    82  	if err != nil {
    83  		t.Errorf("CheckStatus returned error: %v", err)
    84  	}
    85  
    86  	want := wantIssueImportResponse
    87  	if !reflect.DeepEqual(got, want) {
    88  		t.Errorf("CheckStatus = %+v, want %+v", got, want)
    89  	}
    90  }
    91  
    92  func TestIssueImportService_CheckStatus_invalidOwner(t *testing.T) {
    93  	client, _, _, teardown := setup()
    94  	defer teardown()
    95  
    96  	_, _, err := client.IssueImport.CheckStatus(context.Background(), "%", "r", 1)
    97  	testURLParseError(t, err)
    98  }
    99  
   100  func TestIssueImportService_CheckStatusSince(t *testing.T) {
   101  	client, mux, _, teardown := setup()
   102  	defer teardown()
   103  
   104  	mux.HandleFunc("/repos/o/r/import/issues", func(w http.ResponseWriter, r *http.Request) {
   105  		testMethod(t, r, "GET")
   106  		testHeader(t, r, "Accept", mediaTypeIssueImportAPI)
   107  		w.WriteHeader(http.StatusOK)
   108  		w.Write([]byte(fmt.Sprintf("[%s]", issueImportResponseJSON)))
   109  	})
   110  
   111  	got, _, err := client.IssueImport.CheckStatusSince(context.Background(), "o", "r", time.Now())
   112  	if err != nil {
   113  		t.Errorf("CheckStatusSince returned error: %v", err)
   114  	}
   115  
   116  	want := []*IssueImportResponse{wantIssueImportResponse}
   117  	if !reflect.DeepEqual(want, got) {
   118  		t.Errorf("CheckStatusSince = %v, want = %v", got, want)
   119  	}
   120  }
   121  
   122  func TestIssueImportService_CheckStatusSince_invalidOwner(t *testing.T) {
   123  	client, _, _, teardown := setup()
   124  	defer teardown()
   125  
   126  	_, _, err := client.IssueImport.CheckStatusSince(context.Background(), "%", "r", time.Now())
   127  	testURLParseError(t, err)
   128  }
   129  
   130  var issueImportResponseJSON = []byte(`{
   131  	"id": 3,
   132  	"status": "pending",
   133  	"url": "https://api.github.com/repos/o/r/import/issues/3",
   134  	"import_issues_url": "https://api.github.com/repos/o/r/import/issues",
   135  	"repository_url": "https://api.github.com/repos/o/r"
   136  }`)
   137  
   138  var wantIssueImportResponse = &IssueImportResponse{
   139  	ID:              Int(3),
   140  	Status:          String("pending"),
   141  	URL:             String("https://api.github.com/repos/o/r/import/issues/3"),
   142  	ImportIssuesURL: String("https://api.github.com/repos/o/r/import/issues"),
   143  	RepositoryURL:   String("https://api.github.com/repos/o/r"),
   144  }