Skip to content

Commit

Permalink
Merge ce014c8 into cc52532
Browse files Browse the repository at this point in the history
  • Loading branch information
ozerovandrei committed Apr 19, 2018
2 parents cc52532 + ce014c8 commit 83cc8aa
Show file tree
Hide file tree
Showing 2 changed files with 266 additions and 0 deletions.
20 changes: 20 additions & 0 deletions selvpcclient/resell/v2/licenses/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,23 @@ var TestCreateLicenseResponse = []*licenses.License{
Type: "license_windows_2016_standard",
},
}

// TestManyLicensesInvalidResponseRaw represents a raw invalid response with several licenses.
const TestManyLicensesInvalidResponseRaw = `
{
"licenses": [
{
"id": "49338ac045f448e294b25d013f890317"
}
]
}
`

// TestSingleLicenseInvalidResponseRaw represents a raw invalid response with a single license.
const TestSingleLicenseInvalidResponseRaw = `
{
"license": {
"id": "49338ac045f448e294b25d013f890317"
}
}
`
246 changes: 246 additions & 0 deletions selvpcclient/resell/v2/licenses/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,75 @@ func TestGetLicense(t *testing.T) {
}
}

func TestGetLicenseHTTPError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/licenses/123123",
TestGetLicenseResponseRaw, http.MethodGet, http.StatusBadGateway,
&endpointCalled, t)

ctx := context.Background()
license, httpResponse, err := licenses.Get(ctx, testEnv.Client, "123123")

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if license != nil {
t.Fatal("expected no license from the Get method")
}
if err == nil {
t.Fatal("expected error from the Get method")
}
if httpResponse.StatusCode != http.StatusBadGateway {
t.Fatalf("expected %d status in the HTTP response, but got %d",
http.StatusBadGateway, httpResponse.StatusCode)
}
}

func TestGetLicenseTimeoutError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
testEnv.Server.Close()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()

ctx := context.Background()
license, _, err := licenses.Get(ctx, testEnv.Client, "123123")

if license != nil {
t.Fatal("expected no license from the Get method")
}
if err == nil {
t.Fatal("expected error from the Get method")
}
}

func TestGetLicenseUnmarshalError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/licenses/123123",
TestSingleLicenseInvalidResponseRaw, http.MethodGet, http.StatusOK,
&endpointCalled, t)

ctx := context.Background()
license, _, err := licenses.Get(ctx, testEnv.Client, "123123")

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if license != nil {
t.Fatal("expected no license from the List method")
}
if err == nil {
t.Fatal("expected error from the List method")
}
}

func TestListLicenses(t *testing.T) {
endpointCalled := false

Expand Down Expand Up @@ -90,6 +159,75 @@ func TestListLicensesSingle(t *testing.T) {
}
}

func TestListLicensesHTTPError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/licenses",
TestListLicensesResponseRaw, http.MethodGet, http.StatusBadGateway,
&endpointCalled, t)

ctx := context.Background()
allLicenses, httpResponse, err := licenses.List(ctx, testEnv.Client)

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if allLicenses != nil {
t.Fatal("expected no licenses from the Get method")
}
if err == nil {
t.Fatal("expected error from the Get method")
}
if httpResponse.StatusCode != http.StatusBadGateway {
t.Fatalf("expected %d status in the HTTP response, but got %d",
http.StatusBadGateway, httpResponse.StatusCode)
}
}

func TestListLicensesTimeoutError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
testEnv.Server.Close()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()

ctx := context.Background()
allLicenses, _, err := licenses.List(ctx, testEnv.Client)

if allLicenses != nil {
t.Fatal("expected no licenses from the List method")
}
if err == nil {
t.Fatal("expected error from the List method")
}
}

func TestListLicensesUnmarshalError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/licenses",
TestManyLicensesInvalidResponseRaw, http.MethodGet, http.StatusOK,
&endpointCalled, t)

ctx := context.Background()
allLicenses, _, err := licenses.List(ctx, testEnv.Client)

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if allLicenses != nil {
t.Fatal("expected no licenses from the List method")
}
if err == nil {
t.Fatal("expected error from the List method")
}
}

func TestCreateLicenses(t *testing.T) {
endpointCalled := false

Expand Down Expand Up @@ -117,6 +255,77 @@ func TestCreateLicenses(t *testing.T) {
}
}

func TestCreateLicensesHTTPError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/licenses/projects/49338ac045f448e294b25d013f890317",
TestCreateLicenseResponseRaw, TestCreateLicenseOptsRaw, http.MethodPost,
http.StatusBadRequest, &endpointCalled, t)

ctx := context.Background()
createOpts := TestCreateLicenseOpts
licenses, httpResponse, err := licenses.Create(ctx, testEnv.Client, "49338ac045f448e294b25d013f890317", createOpts)

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if licenses != nil {
t.Fatal("expected no licenses from the Create method")
}
if err == nil {
t.Fatal("expected error from the Create method")
}
if httpResponse.StatusCode != http.StatusBadRequest {
t.Fatalf("expected %d status in the HTTP response, but got %d",
http.StatusBadRequest, httpResponse.StatusCode)
}
}

func TestCreateLicensesTimeoutError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
testEnv.Server.Close()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()

ctx := context.Background()
createOpts := TestCreateLicenseOpts
licenses, _, err := licenses.Create(ctx, testEnv.Client, "49338ac045f448e294b25d013f890317", createOpts)

if licenses != nil {
t.Fatal("expected no licenses from the Create method")
}
if err == nil {
t.Fatal("expected error from the Create method")
}
}

func TestCreateLicensesUnmarshalError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/licenses/projects/49338ac045f448e294b25d013f890317",
TestManyLicensesInvalidResponseRaw, TestCreateLicenseOptsRaw, http.MethodPost, http.StatusOK, &endpointCalled, t)

ctx := context.Background()
createOpts := TestCreateLicenseOpts
licenses, _, err := licenses.Create(ctx, testEnv.Client, "49338ac045f448e294b25d013f890317", createOpts)

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if licenses != nil {
t.Fatal("expected no licenses from the Create method")
}
if err == nil {
t.Fatal("expected error from the Create method")
}
}

func TestDeleteLicense(t *testing.T) {
endpointCalled := false

Expand All @@ -135,3 +344,40 @@ func TestDeleteLicense(t *testing.T) {
t.Fatal("endpoint wasn't called")
}
}

func TestDeleteLicenseHTTPError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/licenses/5232d5f3-4950-454b-bd41-78c5295622cd",
"", http.MethodDelete, http.StatusBadGateway, &endpointCalled, t)

ctx := context.Background()
httpResponse, err := licenses.Delete(ctx, testEnv.Client, "5232d5f3-4950-454b-bd41-78c5295622cd")

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if err == nil {
t.Fatal("expected error from the Delete method")
}
if httpResponse.StatusCode != http.StatusBadGateway {
t.Fatalf("expected %d status in the HTTP response, but got %d", http.StatusBadRequest, httpResponse.StatusCode)
}
}

func TestDeleteLicenseTimeoutError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
testEnv.Server.Close()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()

ctx := context.Background()
_, err := licenses.Delete(ctx, testEnv.Client, "5232d5f3-4950-454b-bd41-78c5295622cd")

if err == nil {
t.Fatal("expected error from the Delete method")
}
}

0 comments on commit 83cc8aa

Please sign in to comment.