github.com/google/go-github/v74@v74.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  	"strings"
    13  	"testing"
    14  
    15  	"github.com/google/go-cmp/cmp"
    16  )
    17  
    18  func TestMigrationService_StartMigration(t *testing.T) {
    19  	t.Parallel()
    20  	client, mux, _ := setup(t)
    21  
    22  	mux.HandleFunc("/orgs/o/migrations", func(w http.ResponseWriter, r *http.Request) {
    23  		testMethod(t, r, "POST")
    24  		testHeader(t, r, "Accept", mediaTypeMigrationsPreview)
    25  
    26  		w.WriteHeader(http.StatusCreated)
    27  		assertWrite(t, w, migrationJSON)
    28  	})
    29  
    30  	opt := &MigrationOptions{
    31  		LockRepositories:   true,
    32  		ExcludeAttachments: false,
    33  		ExcludeReleases:    true,
    34  		Exclude:            []string{"repositories"},
    35  	}
    36  	ctx := context.Background()
    37  	got, _, err := client.Migrations.StartMigration(ctx, "o", []string{"r"}, opt)
    38  	if err != nil {
    39  		t.Errorf("StartMigration returned error: %v", err)
    40  	}
    41  	if want := wantMigration; !cmp.Equal(got, want) {
    42  		t.Errorf("StartMigration = %+v, want %+v", got, want)
    43  	}
    44  
    45  	const methodName = "StartMigration"
    46  	testBadOptions(t, methodName, func() (err error) {
    47  		_, _, err = client.Migrations.StartMigration(ctx, "\n", []string{"\n"}, opt)
    48  		return err
    49  	})
    50  
    51  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    52  		got, resp, err := client.Migrations.StartMigration(ctx, "o", []string{"r"}, opt)
    53  		if got != nil {
    54  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    55  		}
    56  		return resp, err
    57  	})
    58  }
    59  
    60  func TestMigrationService_ListMigrations(t *testing.T) {
    61  	t.Parallel()
    62  	client, mux, _ := setup(t)
    63  
    64  	mux.HandleFunc("/orgs/o/migrations", func(w http.ResponseWriter, r *http.Request) {
    65  		testMethod(t, r, "GET")
    66  		testHeader(t, r, "Accept", mediaTypeMigrationsPreview)
    67  
    68  		w.WriteHeader(http.StatusOK)
    69  		assertWrite(t, w, []byte(fmt.Sprintf("[%s]", migrationJSON)))
    70  	})
    71  
    72  	ctx := context.Background()
    73  	got, _, err := client.Migrations.ListMigrations(ctx, "o", &ListOptions{Page: 1, PerPage: 2})
    74  	if err != nil {
    75  		t.Errorf("ListMigrations returned error: %v", err)
    76  	}
    77  	if want := []*Migration{wantMigration}; !cmp.Equal(got, want) {
    78  		t.Errorf("ListMigrations = %+v, want %+v", got, want)
    79  	}
    80  
    81  	const methodName = "ListMigrations"
    82  	testBadOptions(t, methodName, func() (err error) {
    83  		_, _, err = client.Migrations.ListMigrations(ctx, "\n", &ListOptions{Page: 1, PerPage: 2})
    84  		return err
    85  	})
    86  
    87  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    88  		got, resp, err := client.Migrations.ListMigrations(ctx, "o", &ListOptions{Page: 1, PerPage: 2})
    89  		if got != nil {
    90  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    91  		}
    92  		return resp, err
    93  	})
    94  }
    95  
    96  func TestMigrationService_MigrationStatus(t *testing.T) {
    97  	t.Parallel()
    98  	client, mux, _ := setup(t)
    99  
   100  	mux.HandleFunc("/orgs/o/migrations/1", func(w http.ResponseWriter, r *http.Request) {
   101  		testMethod(t, r, "GET")
   102  		testHeader(t, r, "Accept", mediaTypeMigrationsPreview)
   103  
   104  		w.WriteHeader(http.StatusOK)
   105  		assertWrite(t, w, migrationJSON)
   106  	})
   107  
   108  	ctx := context.Background()
   109  	got, _, err := client.Migrations.MigrationStatus(ctx, "o", 1)
   110  	if err != nil {
   111  		t.Errorf("MigrationStatus returned error: %v", err)
   112  	}
   113  	if want := wantMigration; !cmp.Equal(got, want) {
   114  		t.Errorf("MigrationStatus = %+v, want %+v", got, want)
   115  	}
   116  
   117  	const methodName = "MigrationStatus"
   118  	testBadOptions(t, methodName, func() (err error) {
   119  		_, _, err = client.Migrations.MigrationStatus(ctx, "\n", -1)
   120  		return err
   121  	})
   122  
   123  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   124  		got, resp, err := client.Migrations.MigrationStatus(ctx, "o", 1)
   125  		if got != nil {
   126  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   127  		}
   128  		return resp, err
   129  	})
   130  }
   131  
   132  func TestMigrationService_MigrationArchiveURL(t *testing.T) {
   133  	t.Parallel()
   134  	client, mux, _ := setup(t)
   135  
   136  	mux.HandleFunc("/orgs/o/migrations/1/archive", func(w http.ResponseWriter, r *http.Request) {
   137  		testMethod(t, r, "GET")
   138  		testHeader(t, r, "Accept", mediaTypeMigrationsPreview)
   139  
   140  		http.Redirect(w, r, "/yo", http.StatusFound)
   141  	})
   142  	mux.HandleFunc("/yo", func(w http.ResponseWriter, r *http.Request) {
   143  		testMethod(t, r, "GET")
   144  
   145  		w.WriteHeader(http.StatusOK)
   146  		assertWrite(t, w, []byte("0123456789abcdef"))
   147  	})
   148  
   149  	ctx := context.Background()
   150  	got, err := client.Migrations.MigrationArchiveURL(ctx, "o", 1)
   151  	if err != nil {
   152  		t.Errorf("MigrationStatus returned error: %v", err)
   153  	}
   154  	if want := "/yo"; !strings.HasSuffix(got, want) {
   155  		t.Errorf("MigrationArchiveURL = %+v, want %+v", got, want)
   156  	}
   157  
   158  	const methodName = "MigrationArchiveURL"
   159  	testBadOptions(t, methodName, func() (err error) {
   160  		_, err = client.Migrations.MigrationArchiveURL(ctx, "\n", -1)
   161  		return err
   162  	})
   163  }
   164  
   165  func TestMigrationService_DeleteMigration(t *testing.T) {
   166  	t.Parallel()
   167  	client, mux, _ := setup(t)
   168  
   169  	mux.HandleFunc("/orgs/o/migrations/1/archive", func(w http.ResponseWriter, r *http.Request) {
   170  		testMethod(t, r, "DELETE")
   171  		testHeader(t, r, "Accept", mediaTypeMigrationsPreview)
   172  
   173  		w.WriteHeader(http.StatusNoContent)
   174  	})
   175  
   176  	ctx := context.Background()
   177  	if _, err := client.Migrations.DeleteMigration(ctx, "o", 1); err != nil {
   178  		t.Errorf("DeleteMigration returned error: %v", err)
   179  	}
   180  
   181  	const methodName = "DeleteMigration"
   182  	testBadOptions(t, methodName, func() (err error) {
   183  		_, err = client.Migrations.DeleteMigration(ctx, "\n", -1)
   184  		return err
   185  	})
   186  
   187  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   188  		return client.Migrations.DeleteMigration(ctx, "o", 1)
   189  	})
   190  }
   191  
   192  func TestMigrationService_UnlockRepo(t *testing.T) {
   193  	t.Parallel()
   194  	client, mux, _ := setup(t)
   195  
   196  	mux.HandleFunc("/orgs/o/migrations/1/repos/r/lock", func(w http.ResponseWriter, r *http.Request) {
   197  		testMethod(t, r, "DELETE")
   198  		testHeader(t, r, "Accept", mediaTypeMigrationsPreview)
   199  
   200  		w.WriteHeader(http.StatusNoContent)
   201  	})
   202  
   203  	ctx := context.Background()
   204  	if _, err := client.Migrations.UnlockRepo(ctx, "o", 1, "r"); err != nil {
   205  		t.Errorf("UnlockRepo returned error: %v", err)
   206  	}
   207  
   208  	const methodName = "UnlockRepo"
   209  	testBadOptions(t, methodName, func() (err error) {
   210  		_, err = client.Migrations.UnlockRepo(ctx, "\n", -1, "\n")
   211  		return err
   212  	})
   213  
   214  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   215  		return client.Migrations.UnlockRepo(ctx, "o", 1, "r")
   216  	})
   217  }
   218  
   219  var migrationJSON = []byte(`{
   220    "id": 79,
   221    "guid": "0b989ba4-242f-11e5-81e1-c7b6966d2516",
   222    "state": "pending",
   223    "lock_repositories": true,
   224    "exclude_attachments": false,
   225    "url": "https://api.github.com/orgs/octo-org/migrations/79",
   226    "created_at": "2015-07-06T15:33:38-07:00",
   227    "updated_at": "2015-07-06T15:33:38-07:00",
   228    "repositories": [
   229      {
   230        "id": 1296269,
   231        "name": "Hello-World",
   232        "full_name": "octocat/Hello-World",
   233        "description": "This your first repo!"
   234      }
   235    ]
   236  }`)
   237  
   238  var wantMigration = &Migration{
   239  	ID:                 Ptr(int64(79)),
   240  	GUID:               Ptr("0b989ba4-242f-11e5-81e1-c7b6966d2516"),
   241  	State:              Ptr("pending"),
   242  	LockRepositories:   Ptr(true),
   243  	ExcludeAttachments: Ptr(false),
   244  	URL:                Ptr("https://api.github.com/orgs/octo-org/migrations/79"),
   245  	CreatedAt:          Ptr("2015-07-06T15:33:38-07:00"),
   246  	UpdatedAt:          Ptr("2015-07-06T15:33:38-07:00"),
   247  	Repositories: []*Repository{
   248  		{
   249  			ID:          Ptr(int64(1296269)),
   250  			Name:        Ptr("Hello-World"),
   251  			FullName:    Ptr("octocat/Hello-World"),
   252  			Description: Ptr("This your first repo!"),
   253  		},
   254  	},
   255  }
   256  
   257  func TestMigration_Marshal(t *testing.T) {
   258  	t.Parallel()
   259  	testJSONMarshal(t, &Migration{}, "{}")
   260  
   261  	u := &Migration{
   262  		ID:                 Ptr(int64(1)),
   263  		GUID:               Ptr("guid"),
   264  		State:              Ptr("state"),
   265  		LockRepositories:   Ptr(false),
   266  		ExcludeAttachments: Ptr(false),
   267  		URL:                Ptr("url"),
   268  		CreatedAt:          Ptr("ca"),
   269  		UpdatedAt:          Ptr("ua"),
   270  		Repositories:       []*Repository{{ID: Ptr(int64(1))}},
   271  	}
   272  
   273  	want := `{
   274  		"id": 1,
   275  		"guid": "guid",
   276  		"state": "state",
   277  		"lock_repositories": false,
   278  		"exclude_attachments": false,
   279  		"url": "url",
   280  		"created_at": "ca",
   281  		"updated_at": "ua",
   282  		"repositories": [
   283  			{
   284  				"id": 1
   285  			}
   286  		]
   287  	}`
   288  
   289  	testJSONMarshal(t, u, want)
   290  }
   291  
   292  func TestStartMigration_Marshal(t *testing.T) {
   293  	t.Parallel()
   294  	testJSONMarshal(t, &startMigration{}, "{}")
   295  
   296  	u := &startMigration{
   297  		Repositories:       []string{"r"},
   298  		LockRepositories:   Ptr(false),
   299  		ExcludeAttachments: Ptr(false),
   300  	}
   301  
   302  	want := `{
   303  		"repositories": [
   304  			"r"
   305  		],
   306  		"lock_repositories": false,
   307  		"exclude_attachments": false
   308  	}`
   309  
   310  	testJSONMarshal(t, u, want)
   311  }