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

     1  // Copyright 2016 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  	"fmt"
    11  	"net/http"
    12  	"reflect"
    13  	"strings"
    14  	"testing"
    15  )
    16  
    17  func TestMigrationService_StartMigration(t *testing.T) {
    18  	client, mux, _, teardown := setup()
    19  	defer teardown()
    20  
    21  	mux.HandleFunc("/orgs/o/migrations", func(w http.ResponseWriter, r *http.Request) {
    22  		testMethod(t, r, "POST")
    23  		testHeader(t, r, "Accept", mediaTypeMigrationsPreview)
    24  
    25  		w.WriteHeader(http.StatusCreated)
    26  		w.Write(migrationJSON)
    27  	})
    28  
    29  	opt := &MigrationOptions{
    30  		LockRepositories:   true,
    31  		ExcludeAttachments: false,
    32  	}
    33  	got, _, err := client.Migrations.StartMigration(context.Background(), "o", []string{"r"}, opt)
    34  	if err != nil {
    35  		t.Errorf("StartMigration returned error: %v", err)
    36  	}
    37  	if want := wantMigration; !reflect.DeepEqual(got, want) {
    38  		t.Errorf("StartMigration = %+v, want %+v", got, want)
    39  	}
    40  }
    41  
    42  func TestMigrationService_ListMigrations(t *testing.T) {
    43  	client, mux, _, teardown := setup()
    44  	defer teardown()
    45  
    46  	mux.HandleFunc("/orgs/o/migrations", func(w http.ResponseWriter, r *http.Request) {
    47  		testMethod(t, r, "GET")
    48  		testHeader(t, r, "Accept", mediaTypeMigrationsPreview)
    49  
    50  		w.WriteHeader(http.StatusOK)
    51  		w.Write([]byte(fmt.Sprintf("[%s]", migrationJSON)))
    52  	})
    53  
    54  	got, _, err := client.Migrations.ListMigrations(context.Background(), "o", &ListOptions{Page: 1, PerPage: 2})
    55  	if err != nil {
    56  		t.Errorf("ListMigrations returned error: %v", err)
    57  	}
    58  	if want := []*Migration{wantMigration}; !reflect.DeepEqual(got, want) {
    59  		t.Errorf("ListMigrations = %+v, want %+v", got, want)
    60  	}
    61  }
    62  
    63  func TestMigrationService_MigrationStatus(t *testing.T) {
    64  	client, mux, _, teardown := setup()
    65  	defer teardown()
    66  
    67  	mux.HandleFunc("/orgs/o/migrations/1", func(w http.ResponseWriter, r *http.Request) {
    68  		testMethod(t, r, "GET")
    69  		testHeader(t, r, "Accept", mediaTypeMigrationsPreview)
    70  
    71  		w.WriteHeader(http.StatusOK)
    72  		w.Write(migrationJSON)
    73  	})
    74  
    75  	got, _, err := client.Migrations.MigrationStatus(context.Background(), "o", 1)
    76  	if err != nil {
    77  		t.Errorf("MigrationStatus returned error: %v", err)
    78  	}
    79  	if want := wantMigration; !reflect.DeepEqual(got, want) {
    80  		t.Errorf("MigrationStatus = %+v, want %+v", got, want)
    81  	}
    82  }
    83  
    84  func TestMigrationService_MigrationArchiveURL(t *testing.T) {
    85  	client, mux, _, teardown := setup()
    86  	defer teardown()
    87  
    88  	mux.HandleFunc("/orgs/o/migrations/1/archive", func(w http.ResponseWriter, r *http.Request) {
    89  		testMethod(t, r, "GET")
    90  		testHeader(t, r, "Accept", mediaTypeMigrationsPreview)
    91  
    92  		http.Redirect(w, r, "/yo", http.StatusFound)
    93  	})
    94  	mux.HandleFunc("/yo", func(w http.ResponseWriter, r *http.Request) {
    95  		testMethod(t, r, "GET")
    96  
    97  		w.WriteHeader(http.StatusOK)
    98  		w.Write([]byte("0123456789abcdef"))
    99  	})
   100  
   101  	got, err := client.Migrations.MigrationArchiveURL(context.Background(), "o", 1)
   102  	if err != nil {
   103  		t.Errorf("MigrationStatus returned error: %v", err)
   104  	}
   105  	if want := "/yo"; !strings.HasSuffix(got, want) {
   106  		t.Errorf("MigrationArchiveURL = %+v, want %+v", got, want)
   107  	}
   108  }
   109  
   110  func TestMigrationService_DeleteMigration(t *testing.T) {
   111  	client, mux, _, teardown := setup()
   112  	defer teardown()
   113  
   114  	mux.HandleFunc("/orgs/o/migrations/1/archive", func(w http.ResponseWriter, r *http.Request) {
   115  		testMethod(t, r, "DELETE")
   116  		testHeader(t, r, "Accept", mediaTypeMigrationsPreview)
   117  
   118  		w.WriteHeader(http.StatusNoContent)
   119  	})
   120  
   121  	if _, err := client.Migrations.DeleteMigration(context.Background(), "o", 1); err != nil {
   122  		t.Errorf("DeleteMigration returned error: %v", err)
   123  	}
   124  }
   125  
   126  func TestMigrationService_UnlockRepo(t *testing.T) {
   127  	client, mux, _, teardown := setup()
   128  	defer teardown()
   129  
   130  	mux.HandleFunc("/orgs/o/migrations/1/repos/r/lock", func(w http.ResponseWriter, r *http.Request) {
   131  		testMethod(t, r, "DELETE")
   132  		testHeader(t, r, "Accept", mediaTypeMigrationsPreview)
   133  
   134  		w.WriteHeader(http.StatusNoContent)
   135  	})
   136  
   137  	if _, err := client.Migrations.UnlockRepo(context.Background(), "o", 1, "r"); err != nil {
   138  		t.Errorf("UnlockRepo returned error: %v", err)
   139  	}
   140  }
   141  
   142  var migrationJSON = []byte(`{
   143    "id": 79,
   144    "guid": "0b989ba4-242f-11e5-81e1-c7b6966d2516",
   145    "state": "pending",
   146    "lock_repositories": true,
   147    "exclude_attachments": false,
   148    "url": "https://api.github.com/orgs/octo-org/migrations/79",
   149    "created_at": "2015-07-06T15:33:38-07:00",
   150    "updated_at": "2015-07-06T15:33:38-07:00",
   151    "repositories": [
   152      {
   153        "id": 1296269,
   154        "name": "Hello-World",
   155        "full_name": "octocat/Hello-World",
   156        "description": "This your first repo!"
   157      }
   158    ]
   159  }`)
   160  
   161  var wantMigration = &Migration{
   162  	ID:                 Int64(79),
   163  	GUID:               String("0b989ba4-242f-11e5-81e1-c7b6966d2516"),
   164  	State:              String("pending"),
   165  	LockRepositories:   Bool(true),
   166  	ExcludeAttachments: Bool(false),
   167  	URL:                String("https://api.github.com/orgs/octo-org/migrations/79"),
   168  	CreatedAt:          String("2015-07-06T15:33:38-07:00"),
   169  	UpdatedAt:          String("2015-07-06T15:33:38-07:00"),
   170  	Repositories: []*Repository{
   171  		{
   172  			ID:          Int64(1296269),
   173  			Name:        String("Hello-World"),
   174  			FullName:    String("octocat/Hello-World"),
   175  			Description: String("This your first repo!"),
   176  		},
   177  	},
   178  }