github.com/google/go-github/v71@v71.0.0/github/apps_marketplace_test.go (about)

     1  // Copyright 2017 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  	"testing"
    13  
    14  	"github.com/google/go-cmp/cmp"
    15  )
    16  
    17  func TestMarketplaceService_ListPlans(t *testing.T) {
    18  	t.Parallel()
    19  	client, mux, _ := setup(t)
    20  
    21  	mux.HandleFunc("/marketplace_listing/plans", func(w http.ResponseWriter, r *http.Request) {
    22  		testMethod(t, r, "GET")
    23  		testFormValues(t, r, values{
    24  			"page":     "1",
    25  			"per_page": "2",
    26  		})
    27  		fmt.Fprint(w, `[{"id":1}]`)
    28  	})
    29  
    30  	opt := &ListOptions{Page: 1, PerPage: 2}
    31  	client.Marketplace.Stubbed = false
    32  	ctx := context.Background()
    33  	plans, _, err := client.Marketplace.ListPlans(ctx, opt)
    34  	if err != nil {
    35  		t.Errorf("Marketplace.ListPlans returned error: %v", err)
    36  	}
    37  
    38  	want := []*MarketplacePlan{{ID: Ptr(int64(1))}}
    39  	if !cmp.Equal(plans, want) {
    40  		t.Errorf("Marketplace.ListPlans returned %+v, want %+v", plans, want)
    41  	}
    42  
    43  	const methodName = "ListPlans"
    44  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    45  		got, resp, err := client.Marketplace.ListPlans(ctx, opt)
    46  		if got != nil {
    47  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    48  		}
    49  		return resp, err
    50  	})
    51  }
    52  
    53  func TestMarketplaceService_Stubbed_ListPlans(t *testing.T) {
    54  	t.Parallel()
    55  	client, mux, _ := setup(t)
    56  
    57  	mux.HandleFunc("/marketplace_listing/stubbed/plans", func(w http.ResponseWriter, r *http.Request) {
    58  		testMethod(t, r, "GET")
    59  		fmt.Fprint(w, `[{"id":1}]`)
    60  	})
    61  
    62  	opt := &ListOptions{Page: 1, PerPage: 2}
    63  	client.Marketplace.Stubbed = true
    64  	ctx := context.Background()
    65  	plans, _, err := client.Marketplace.ListPlans(ctx, opt)
    66  	if err != nil {
    67  		t.Errorf("Marketplace.ListPlans (Stubbed) returned error: %v", err)
    68  	}
    69  
    70  	want := []*MarketplacePlan{{ID: Ptr(int64(1))}}
    71  	if !cmp.Equal(plans, want) {
    72  		t.Errorf("Marketplace.ListPlans (Stubbed) returned %+v, want %+v", plans, want)
    73  	}
    74  }
    75  
    76  func TestMarketplaceService_ListPlanAccountsForPlan(t *testing.T) {
    77  	t.Parallel()
    78  	client, mux, _ := setup(t)
    79  
    80  	mux.HandleFunc("/marketplace_listing/plans/1/accounts", func(w http.ResponseWriter, r *http.Request) {
    81  		testMethod(t, r, "GET")
    82  		fmt.Fprint(w, `[{"id":1}]`)
    83  	})
    84  
    85  	opt := &ListOptions{Page: 1, PerPage: 2}
    86  	client.Marketplace.Stubbed = false
    87  	ctx := context.Background()
    88  	accounts, _, err := client.Marketplace.ListPlanAccountsForPlan(ctx, 1, opt)
    89  	if err != nil {
    90  		t.Errorf("Marketplace.ListPlanAccountsForPlan returned error: %v", err)
    91  	}
    92  
    93  	want := []*MarketplacePlanAccount{{ID: Ptr(int64(1))}}
    94  	if !cmp.Equal(accounts, want) {
    95  		t.Errorf("Marketplace.ListPlanAccountsForPlan returned %+v, want %+v", accounts, want)
    96  	}
    97  
    98  	const methodName = "ListPlanAccountsForPlan"
    99  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   100  		got, resp, err := client.Marketplace.ListPlanAccountsForPlan(ctx, 1, opt)
   101  		if got != nil {
   102  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   103  		}
   104  		return resp, err
   105  	})
   106  }
   107  
   108  func TestMarketplaceService_Stubbed_ListPlanAccountsForPlan(t *testing.T) {
   109  	t.Parallel()
   110  	client, mux, _ := setup(t)
   111  
   112  	mux.HandleFunc("/marketplace_listing/stubbed/plans/1/accounts", func(w http.ResponseWriter, r *http.Request) {
   113  		testMethod(t, r, "GET")
   114  		fmt.Fprint(w, `[{"id":1}]`)
   115  	})
   116  
   117  	opt := &ListOptions{Page: 1, PerPage: 2}
   118  	client.Marketplace.Stubbed = true
   119  	ctx := context.Background()
   120  	accounts, _, err := client.Marketplace.ListPlanAccountsForPlan(ctx, 1, opt)
   121  	if err != nil {
   122  		t.Errorf("Marketplace.ListPlanAccountsForPlan (Stubbed) returned error: %v", err)
   123  	}
   124  
   125  	want := []*MarketplacePlanAccount{{ID: Ptr(int64(1))}}
   126  	if !cmp.Equal(accounts, want) {
   127  		t.Errorf("Marketplace.ListPlanAccountsForPlan (Stubbed) returned %+v, want %+v", accounts, want)
   128  	}
   129  }
   130  
   131  func TestMarketplaceService_GetPlanAccountForAccount(t *testing.T) {
   132  	t.Parallel()
   133  	client, mux, _ := setup(t)
   134  
   135  	mux.HandleFunc("/marketplace_listing/accounts/1", func(w http.ResponseWriter, r *http.Request) {
   136  		testMethod(t, r, "GET")
   137  		fmt.Fprint(w, `{"id":1, "marketplace_pending_change": {"id": 77}}`)
   138  	})
   139  
   140  	client.Marketplace.Stubbed = false
   141  	ctx := context.Background()
   142  	account, _, err := client.Marketplace.GetPlanAccountForAccount(ctx, 1)
   143  	if err != nil {
   144  		t.Errorf("Marketplace.GetPlanAccountForAccount returned error: %v", err)
   145  	}
   146  
   147  	want := &MarketplacePlanAccount{ID: Ptr(int64(1)), MarketplacePendingChange: &MarketplacePendingChange{ID: Ptr(int64(77))}}
   148  	if !cmp.Equal(account, want) {
   149  		t.Errorf("Marketplace.GetPlanAccountForAccount returned %+v, want %+v", account, want)
   150  	}
   151  
   152  	const methodName = "GetPlanAccountForAccount"
   153  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   154  		got, resp, err := client.Marketplace.GetPlanAccountForAccount(ctx, 1)
   155  		if got != nil {
   156  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   157  		}
   158  		return resp, err
   159  	})
   160  }
   161  
   162  func TestMarketplaceService_Stubbed_GetPlanAccountForAccount(t *testing.T) {
   163  	t.Parallel()
   164  	client, mux, _ := setup(t)
   165  
   166  	mux.HandleFunc("/marketplace_listing/stubbed/accounts/1", func(w http.ResponseWriter, r *http.Request) {
   167  		testMethod(t, r, "GET")
   168  		fmt.Fprint(w, `{"id":1}`)
   169  	})
   170  
   171  	client.Marketplace.Stubbed = true
   172  	ctx := context.Background()
   173  	account, _, err := client.Marketplace.GetPlanAccountForAccount(ctx, 1)
   174  	if err != nil {
   175  		t.Errorf("Marketplace.GetPlanAccountForAccount (Stubbed) returned error: %v", err)
   176  	}
   177  
   178  	want := &MarketplacePlanAccount{ID: Ptr(int64(1))}
   179  	if !cmp.Equal(account, want) {
   180  		t.Errorf("Marketplace.GetPlanAccountForAccount (Stubbed) returned %+v, want %+v", account, want)
   181  	}
   182  }
   183  
   184  func TestMarketplaceService_ListMarketplacePurchasesForUser(t *testing.T) {
   185  	t.Parallel()
   186  	client, mux, _ := setup(t)
   187  
   188  	mux.HandleFunc("/user/marketplace_purchases", func(w http.ResponseWriter, r *http.Request) {
   189  		testMethod(t, r, "GET")
   190  		fmt.Fprint(w, `[{"billing_cycle":"monthly"}]`)
   191  	})
   192  
   193  	opt := &ListOptions{Page: 1, PerPage: 2}
   194  	client.Marketplace.Stubbed = false
   195  	ctx := context.Background()
   196  	purchases, _, err := client.Marketplace.ListMarketplacePurchasesForUser(ctx, opt)
   197  	if err != nil {
   198  		t.Errorf("Marketplace.ListMarketplacePurchasesForUser returned error: %v", err)
   199  	}
   200  
   201  	want := []*MarketplacePurchase{{BillingCycle: Ptr("monthly")}}
   202  	if !cmp.Equal(purchases, want) {
   203  		t.Errorf("Marketplace.ListMarketplacePurchasesForUser returned %+v, want %+v", purchases, want)
   204  	}
   205  
   206  	const methodName = "ListMarketplacePurchasesForUser"
   207  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   208  		got, resp, err := client.Marketplace.ListMarketplacePurchasesForUser(ctx, opt)
   209  		if got != nil {
   210  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   211  		}
   212  		return resp, err
   213  	})
   214  }
   215  
   216  func TestMarketplaceService_Stubbed_ListMarketplacePurchasesForUser(t *testing.T) {
   217  	t.Parallel()
   218  	client, mux, _ := setup(t)
   219  
   220  	mux.HandleFunc("/user/marketplace_purchases/stubbed", func(w http.ResponseWriter, r *http.Request) {
   221  		testMethod(t, r, "GET")
   222  		fmt.Fprint(w, `[{"billing_cycle":"monthly"}]`)
   223  	})
   224  
   225  	opt := &ListOptions{Page: 1, PerPage: 2}
   226  	client.Marketplace.Stubbed = true
   227  	ctx := context.Background()
   228  	purchases, _, err := client.Marketplace.ListMarketplacePurchasesForUser(ctx, opt)
   229  	if err != nil {
   230  		t.Errorf("Marketplace.ListMarketplacePurchasesForUser returned error: %v", err)
   231  	}
   232  
   233  	want := []*MarketplacePurchase{{BillingCycle: Ptr("monthly")}}
   234  	if !cmp.Equal(purchases, want) {
   235  		t.Errorf("Marketplace.ListMarketplacePurchasesForUser returned %+v, want %+v", purchases, want)
   236  	}
   237  }
   238  
   239  func TestMarketplacePlan_Marshal(t *testing.T) {
   240  	t.Parallel()
   241  	testJSONMarshal(t, &MarketplacePlan{}, "{}")
   242  
   243  	u := &MarketplacePlan{
   244  		URL:                 Ptr("u"),
   245  		AccountsURL:         Ptr("au"),
   246  		ID:                  Ptr(int64(1)),
   247  		Number:              Ptr(1),
   248  		Name:                Ptr("n"),
   249  		Description:         Ptr("d"),
   250  		MonthlyPriceInCents: Ptr(1),
   251  		YearlyPriceInCents:  Ptr(1),
   252  		PriceModel:          Ptr("pm"),
   253  		UnitName:            Ptr("un"),
   254  		Bullets:             &[]string{"b"},
   255  		State:               Ptr("s"),
   256  		HasFreeTrial:        Ptr(false),
   257  	}
   258  
   259  	want := `{
   260  		"url": "u",
   261  		"accounts_url": "au",
   262  		"id": 1,
   263  		"number": 1,
   264  		"name": "n",
   265  		"description": "d",
   266  		"monthly_price_in_cents": 1,
   267  		"yearly_price_in_cents": 1,
   268  		"price_model": "pm",
   269  		"unit_name": "un",
   270  		"bullets": ["b"],
   271  		"state": "s",
   272  		"has_free_trial": false
   273  	}`
   274  
   275  	testJSONMarshal(t, u, want)
   276  }
   277  
   278  func TestMarketplacePurchase_Marshal(t *testing.T) {
   279  	t.Parallel()
   280  	testJSONMarshal(t, &MarketplacePurchase{}, "{}")
   281  
   282  	u := &MarketplacePurchase{
   283  		BillingCycle:    Ptr("bc"),
   284  		NextBillingDate: &Timestamp{referenceTime},
   285  		UnitCount:       Ptr(1),
   286  		Plan: &MarketplacePlan{
   287  			URL:                 Ptr("u"),
   288  			AccountsURL:         Ptr("au"),
   289  			ID:                  Ptr(int64(1)),
   290  			Number:              Ptr(1),
   291  			Name:                Ptr("n"),
   292  			Description:         Ptr("d"),
   293  			MonthlyPriceInCents: Ptr(1),
   294  			YearlyPriceInCents:  Ptr(1),
   295  			PriceModel:          Ptr("pm"),
   296  			UnitName:            Ptr("un"),
   297  			Bullets:             &[]string{"b"},
   298  			State:               Ptr("s"),
   299  			HasFreeTrial:        Ptr(false),
   300  		},
   301  		OnFreeTrial:     Ptr(false),
   302  		FreeTrialEndsOn: &Timestamp{referenceTime},
   303  		UpdatedAt:       &Timestamp{referenceTime},
   304  	}
   305  
   306  	want := `{
   307  		"billing_cycle": "bc",
   308  		"next_billing_date": ` + referenceTimeStr + `,
   309  		"unit_count": 1,
   310  		"plan": {
   311  			"url": "u",
   312  			"accounts_url": "au",
   313  			"id": 1,
   314  			"number": 1,
   315  			"name": "n",
   316  			"description": "d",
   317  			"monthly_price_in_cents": 1,
   318  			"yearly_price_in_cents": 1,
   319  			"price_model": "pm",
   320  			"unit_name": "un",
   321  			"bullets": ["b"],
   322  			"state": "s",
   323  			"has_free_trial": false
   324  			},
   325  		"on_free_trial": false,
   326  		"free_trial_ends_on": ` + referenceTimeStr + `,
   327  		"updated_at": ` + referenceTimeStr + `
   328  	}`
   329  
   330  	testJSONMarshal(t, u, want)
   331  }
   332  
   333  func TestMarketplacePendingChange_Marshal(t *testing.T) {
   334  	t.Parallel()
   335  	testJSONMarshal(t, &MarketplacePendingChange{}, "{}")
   336  
   337  	u := &MarketplacePendingChange{
   338  		EffectiveDate: &Timestamp{referenceTime},
   339  		UnitCount:     Ptr(1),
   340  		ID:            Ptr(int64(1)),
   341  		Plan: &MarketplacePlan{
   342  			URL:                 Ptr("u"),
   343  			AccountsURL:         Ptr("au"),
   344  			ID:                  Ptr(int64(1)),
   345  			Number:              Ptr(1),
   346  			Name:                Ptr("n"),
   347  			Description:         Ptr("d"),
   348  			MonthlyPriceInCents: Ptr(1),
   349  			YearlyPriceInCents:  Ptr(1),
   350  			PriceModel:          Ptr("pm"),
   351  			UnitName:            Ptr("un"),
   352  			Bullets:             &[]string{"b"},
   353  			State:               Ptr("s"),
   354  			HasFreeTrial:        Ptr(false),
   355  		},
   356  	}
   357  
   358  	want := `{
   359  		"effective_date": ` + referenceTimeStr + `,
   360  		"unit_count": 1,
   361  		"id": 1,
   362  		"plan": {
   363  			"url": "u",
   364  			"accounts_url": "au",
   365  			"id": 1,
   366  			"number": 1,
   367  			"name": "n",
   368  			"description": "d",
   369  			"monthly_price_in_cents": 1,
   370  			"yearly_price_in_cents": 1,
   371  			"price_model": "pm",
   372  			"unit_name": "un",
   373  			"bullets": ["b"],
   374  			"state": "s",
   375  			"has_free_trial": false
   376  			}
   377  	}`
   378  
   379  	testJSONMarshal(t, u, want)
   380  }
   381  
   382  func TestMarketplacePlanAccount_Marshal(t *testing.T) {
   383  	t.Parallel()
   384  	testJSONMarshal(t, &MarketplacePlanAccount{}, "{}")
   385  
   386  	u := &MarketplacePlanAccount{
   387  		URL:                      Ptr("u"),
   388  		Type:                     Ptr("t"),
   389  		ID:                       Ptr(int64(1)),
   390  		Login:                    Ptr("l"),
   391  		OrganizationBillingEmail: Ptr("obe"),
   392  		MarketplacePurchase: &MarketplacePurchase{
   393  			BillingCycle:    Ptr("bc"),
   394  			NextBillingDate: &Timestamp{referenceTime},
   395  			UnitCount:       Ptr(1),
   396  			Plan: &MarketplacePlan{
   397  				URL:                 Ptr("u"),
   398  				AccountsURL:         Ptr("au"),
   399  				ID:                  Ptr(int64(1)),
   400  				Number:              Ptr(1),
   401  				Name:                Ptr("n"),
   402  				Description:         Ptr("d"),
   403  				MonthlyPriceInCents: Ptr(1),
   404  				YearlyPriceInCents:  Ptr(1),
   405  				PriceModel:          Ptr("pm"),
   406  				UnitName:            Ptr("un"),
   407  				Bullets:             &[]string{"b"},
   408  				State:               Ptr("s"),
   409  				HasFreeTrial:        Ptr(false),
   410  			},
   411  			OnFreeTrial:     Ptr(false),
   412  			FreeTrialEndsOn: &Timestamp{referenceTime},
   413  			UpdatedAt:       &Timestamp{referenceTime},
   414  		},
   415  		MarketplacePendingChange: &MarketplacePendingChange{
   416  			EffectiveDate: &Timestamp{referenceTime},
   417  			UnitCount:     Ptr(1),
   418  			ID:            Ptr(int64(1)),
   419  			Plan: &MarketplacePlan{
   420  				URL:                 Ptr("u"),
   421  				AccountsURL:         Ptr("au"),
   422  				ID:                  Ptr(int64(1)),
   423  				Number:              Ptr(1),
   424  				Name:                Ptr("n"),
   425  				Description:         Ptr("d"),
   426  				MonthlyPriceInCents: Ptr(1),
   427  				YearlyPriceInCents:  Ptr(1),
   428  				PriceModel:          Ptr("pm"),
   429  				UnitName:            Ptr("un"),
   430  				Bullets:             &[]string{"b"},
   431  				State:               Ptr("s"),
   432  				HasFreeTrial:        Ptr(false),
   433  			},
   434  		},
   435  	}
   436  
   437  	want := `{
   438  		"url": "u",
   439  		"type": "t",
   440  		"id": 1,
   441  		"login": "l",
   442  		"organization_billing_email": "obe",
   443  		"marketplace_purchase": {
   444  			"billing_cycle": "bc",
   445  			"next_billing_date": ` + referenceTimeStr + `,
   446  			"unit_count": 1,
   447  			"plan": {
   448  				"url": "u",
   449  				"accounts_url": "au",
   450  				"id": 1,
   451  				"number": 1,
   452  				"name": "n",
   453  				"description": "d",
   454  				"monthly_price_in_cents": 1,
   455  				"yearly_price_in_cents": 1,
   456  				"price_model": "pm",
   457  				"unit_name": "un",
   458  				"bullets": ["b"],
   459  				"state": "s",
   460  				"has_free_trial": false
   461  				},
   462  			"on_free_trial": false,
   463  			"free_trial_ends_on": ` + referenceTimeStr + `,
   464  			"updated_at": ` + referenceTimeStr + `
   465  		},
   466  		"marketplace_pending_change": {
   467  			"effective_date": ` + referenceTimeStr + `,
   468  			"unit_count": 1,
   469  			"id": 1,
   470  			"plan": {
   471  				"url": "u",
   472  				"accounts_url": "au",
   473  				"id": 1,
   474  				"number": 1,
   475  				"name": "n",
   476  				"description": "d",
   477  				"monthly_price_in_cents": 1,
   478  				"yearly_price_in_cents": 1,
   479  				"price_model": "pm",
   480  				"unit_name": "un",
   481  				"bullets": ["b"],
   482  				"state": "s",
   483  				"has_free_trial": false
   484  			}
   485  		}
   486  	}`
   487  
   488  	testJSONMarshal(t, u, want)
   489  }