...

Source file src/github.com/google/go-github/v45/github/apps_marketplace_test.go

Documentation: github.com/google/go-github/v45/github

     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  	client, mux, _, teardown := setup()
    19  	defer teardown()
    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: 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  	client, mux, _, teardown := setup()
    55  	defer teardown()
    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: 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  	client, mux, _, teardown := setup()
    78  	defer teardown()
    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: 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  	client, mux, _, teardown := setup()
   110  	defer teardown()
   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: 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  	client, mux, _, teardown := setup()
   133  	defer teardown()
   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: Int64(1), MarketplacePendingChange: &MarketplacePendingChange{ID: 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  	client, mux, _, teardown := setup()
   164  	defer teardown()
   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: 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  	client, mux, _, teardown := setup()
   186  	defer teardown()
   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: String("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  	client, mux, _, teardown := setup()
   218  	defer teardown()
   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: String("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  	testJSONMarshal(t, &MarketplacePlan{}, "{}")
   241  
   242  	u := &MarketplacePlan{
   243  		URL:                 String("u"),
   244  		AccountsURL:         String("au"),
   245  		ID:                  Int64(1),
   246  		Number:              Int(1),
   247  		Name:                String("n"),
   248  		Description:         String("d"),
   249  		MonthlyPriceInCents: Int(1),
   250  		YearlyPriceInCents:  Int(1),
   251  		PriceModel:          String("pm"),
   252  		UnitName:            String("un"),
   253  		Bullets:             &[]string{"b"},
   254  		State:               String("s"),
   255  		HasFreeTrial:        Bool(false),
   256  	}
   257  
   258  	want := `{
   259  		"url": "u",
   260  		"accounts_url": "au",
   261  		"id": 1,
   262  		"number": 1,
   263  		"name": "n",
   264  		"description": "d",
   265  		"monthly_price_in_cents": 1,
   266  		"yearly_price_in_cents": 1,
   267  		"price_model": "pm",
   268  		"unit_name": "un",
   269  		"bullets": ["b"],
   270  		"state": "s",
   271  		"has_free_trial": false
   272  	}`
   273  
   274  	testJSONMarshal(t, u, want)
   275  }
   276  
   277  func TestMarketplacePurchase_Marshal(t *testing.T) {
   278  	testJSONMarshal(t, &MarketplacePurchase{}, "{}")
   279  
   280  	u := &MarketplacePurchase{
   281  		BillingCycle:    String("bc"),
   282  		NextBillingDate: &Timestamp{referenceTime},
   283  		UnitCount:       Int(1),
   284  		Plan: &MarketplacePlan{
   285  			URL:                 String("u"),
   286  			AccountsURL:         String("au"),
   287  			ID:                  Int64(1),
   288  			Number:              Int(1),
   289  			Name:                String("n"),
   290  			Description:         String("d"),
   291  			MonthlyPriceInCents: Int(1),
   292  			YearlyPriceInCents:  Int(1),
   293  			PriceModel:          String("pm"),
   294  			UnitName:            String("un"),
   295  			Bullets:             &[]string{"b"},
   296  			State:               String("s"),
   297  			HasFreeTrial:        Bool(false),
   298  		},
   299  		OnFreeTrial:     Bool(false),
   300  		FreeTrialEndsOn: &Timestamp{referenceTime},
   301  		UpdatedAt:       &Timestamp{referenceTime},
   302  	}
   303  
   304  	want := `{
   305  		"billing_cycle": "bc",
   306  		"next_billing_date": ` + referenceTimeStr + `,
   307  		"unit_count": 1,
   308  		"plan": {
   309  			"url": "u",
   310  			"accounts_url": "au",
   311  			"id": 1,
   312  			"number": 1,
   313  			"name": "n",
   314  			"description": "d",
   315  			"monthly_price_in_cents": 1,
   316  			"yearly_price_in_cents": 1,
   317  			"price_model": "pm",
   318  			"unit_name": "un",
   319  			"bullets": ["b"],
   320  			"state": "s",
   321  			"has_free_trial": false
   322  			},
   323  		"on_free_trial": false,
   324  		"free_trial_ends_on": ` + referenceTimeStr + `,
   325  		"updated_at": ` + referenceTimeStr + `
   326  	}`
   327  
   328  	testJSONMarshal(t, u, want)
   329  }
   330  
   331  func TestMarketplacePendingChange_Marshal(t *testing.T) {
   332  	testJSONMarshal(t, &MarketplacePendingChange{}, "{}")
   333  
   334  	u := &MarketplacePendingChange{
   335  		EffectiveDate: &Timestamp{referenceTime},
   336  		UnitCount:     Int(1),
   337  		ID:            Int64(1),
   338  		Plan: &MarketplacePlan{
   339  			URL:                 String("u"),
   340  			AccountsURL:         String("au"),
   341  			ID:                  Int64(1),
   342  			Number:              Int(1),
   343  			Name:                String("n"),
   344  			Description:         String("d"),
   345  			MonthlyPriceInCents: Int(1),
   346  			YearlyPriceInCents:  Int(1),
   347  			PriceModel:          String("pm"),
   348  			UnitName:            String("un"),
   349  			Bullets:             &[]string{"b"},
   350  			State:               String("s"),
   351  			HasFreeTrial:        Bool(false),
   352  		},
   353  	}
   354  
   355  	want := `{
   356  		"effective_date": ` + referenceTimeStr + `,
   357  		"unit_count": 1,
   358  		"id": 1,
   359  		"plan": {
   360  			"url": "u",
   361  			"accounts_url": "au",
   362  			"id": 1,
   363  			"number": 1,
   364  			"name": "n",
   365  			"description": "d",
   366  			"monthly_price_in_cents": 1,
   367  			"yearly_price_in_cents": 1,
   368  			"price_model": "pm",
   369  			"unit_name": "un",
   370  			"bullets": ["b"],
   371  			"state": "s",
   372  			"has_free_trial": false
   373  			}
   374  	}`
   375  
   376  	testJSONMarshal(t, u, want)
   377  }
   378  
   379  func TestMarketplacePlanAccount_Marshal(t *testing.T) {
   380  	testJSONMarshal(t, &MarketplacePlanAccount{}, "{}")
   381  
   382  	u := &MarketplacePlanAccount{
   383  		URL:                      String("u"),
   384  		Type:                     String("t"),
   385  		ID:                       Int64(1),
   386  		Login:                    String("l"),
   387  		OrganizationBillingEmail: String("obe"),
   388  		MarketplacePurchase: &MarketplacePurchase{
   389  			BillingCycle:    String("bc"),
   390  			NextBillingDate: &Timestamp{referenceTime},
   391  			UnitCount:       Int(1),
   392  			Plan: &MarketplacePlan{
   393  				URL:                 String("u"),
   394  				AccountsURL:         String("au"),
   395  				ID:                  Int64(1),
   396  				Number:              Int(1),
   397  				Name:                String("n"),
   398  				Description:         String("d"),
   399  				MonthlyPriceInCents: Int(1),
   400  				YearlyPriceInCents:  Int(1),
   401  				PriceModel:          String("pm"),
   402  				UnitName:            String("un"),
   403  				Bullets:             &[]string{"b"},
   404  				State:               String("s"),
   405  				HasFreeTrial:        Bool(false),
   406  			},
   407  			OnFreeTrial:     Bool(false),
   408  			FreeTrialEndsOn: &Timestamp{referenceTime},
   409  			UpdatedAt:       &Timestamp{referenceTime},
   410  		},
   411  		MarketplacePendingChange: &MarketplacePendingChange{
   412  			EffectiveDate: &Timestamp{referenceTime},
   413  			UnitCount:     Int(1),
   414  			ID:            Int64(1),
   415  			Plan: &MarketplacePlan{
   416  				URL:                 String("u"),
   417  				AccountsURL:         String("au"),
   418  				ID:                  Int64(1),
   419  				Number:              Int(1),
   420  				Name:                String("n"),
   421  				Description:         String("d"),
   422  				MonthlyPriceInCents: Int(1),
   423  				YearlyPriceInCents:  Int(1),
   424  				PriceModel:          String("pm"),
   425  				UnitName:            String("un"),
   426  				Bullets:             &[]string{"b"},
   427  				State:               String("s"),
   428  				HasFreeTrial:        Bool(false),
   429  			},
   430  		},
   431  	}
   432  
   433  	want := `{
   434  		"url": "u",
   435  		"type": "t",
   436  		"id": 1,
   437  		"login": "l",
   438  		"organization_billing_email": "obe",
   439  		"marketplace_purchase": {
   440  			"billing_cycle": "bc",
   441  			"next_billing_date": ` + referenceTimeStr + `,
   442  			"unit_count": 1,
   443  			"plan": {
   444  				"url": "u",
   445  				"accounts_url": "au",
   446  				"id": 1,
   447  				"number": 1,
   448  				"name": "n",
   449  				"description": "d",
   450  				"monthly_price_in_cents": 1,
   451  				"yearly_price_in_cents": 1,
   452  				"price_model": "pm",
   453  				"unit_name": "un",
   454  				"bullets": ["b"],
   455  				"state": "s",
   456  				"has_free_trial": false
   457  				},
   458  			"on_free_trial": false,
   459  			"free_trial_ends_on": ` + referenceTimeStr + `,
   460  			"updated_at": ` + referenceTimeStr + `
   461  		},
   462  		"marketplace_pending_change": {
   463  			"effective_date": ` + referenceTimeStr + `,
   464  			"unit_count": 1,
   465  			"id": 1,
   466  			"plan": {
   467  				"url": "u",
   468  				"accounts_url": "au",
   469  				"id": 1,
   470  				"number": 1,
   471  				"name": "n",
   472  				"description": "d",
   473  				"monthly_price_in_cents": 1,
   474  				"yearly_price_in_cents": 1,
   475  				"price_model": "pm",
   476  				"unit_name": "un",
   477  				"bullets": ["b"],
   478  				"state": "s",
   479  				"has_free_trial": false
   480  			}
   481  		}
   482  	}`
   483  
   484  	testJSONMarshal(t, u, want)
   485  }
   486  

View as plain text