-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kevin DeJong <kddejong@amazon.com>
- Loading branch information
Showing
11 changed files
with
363 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"path" | ||
|
||
"github.com/Optum/Redbox/pkg/db" | ||
|
||
"github.com/Optum/Redbox/pkg/api/response" | ||
"github.com/aws/aws-lambda-go/events" | ||
) | ||
|
||
type getController struct { | ||
Dao db.DBer | ||
} | ||
|
||
// Call - function to return a specific AWS Lease record to the request | ||
func (controller getController) Call(ctx context.Context, req *events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { | ||
// Fetch the account. | ||
leaseID := path.Base(req.Path) | ||
lease, err := controller.Dao.GetLeaseByID(leaseID) | ||
if err != nil { | ||
log.Printf("Error Getting Lease for Id: %s", err) | ||
return response.CreateAPIErrorResponse(http.StatusInternalServerError, | ||
response.CreateErrorResponse("ServerError", | ||
fmt.Sprintf("Failed Get on Lease %s", | ||
leaseID))), nil | ||
} | ||
if lease == nil { | ||
log.Printf("Error Getting Lease for Id: %s", err) | ||
return response.NotFoundError(), nil | ||
} | ||
|
||
leaseResponse := response.LeaseResponse(*lease) | ||
return response.CreateJSONResponse(http.StatusOK, leaseResponse), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/Optum/Redbox/pkg/api/response" | ||
"github.com/Optum/Redbox/pkg/db" | ||
"github.com/Optum/Redbox/pkg/db/mocks" | ||
"github.com/aws/aws-lambda-go/events" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetLeaseByID(t *testing.T) { | ||
|
||
t.Run("When the invoking Call and there are no errors", func(t *testing.T) { | ||
expectdLease := &db.RedboxLease{ | ||
ID: "unique-id", | ||
AccountID: "123456789", | ||
PrincipalID: "test", | ||
LeaseStatus: db.Active, | ||
LastModifiedOn: 1561149393, | ||
} | ||
expectedLeaseResponse := &response.LeaseResponse{ | ||
ID: "unique-id", | ||
AccountID: "123456789", | ||
PrincipalID: "test", | ||
LeaseStatus: db.Active, | ||
LastModifiedOn: 1561149393, | ||
} | ||
mockDb := mocks.DBer{} | ||
mockDb.On("GetLeaseByID", "unique-id").Return(expectdLease, nil) | ||
mockRequest := events.APIGatewayProxyRequest{HTTPMethod: http.MethodGet, Path: "/leases/unique-id"} | ||
|
||
controller := getController{ | ||
Dao: &mockDb, | ||
} | ||
|
||
actualResponse, err := controller.Call(context.TODO(), &mockRequest) | ||
require.Nil(t, err) | ||
|
||
parsedResponse := &response.LeaseResponse{} | ||
err = json.Unmarshal([]byte(actualResponse.Body), parsedResponse) | ||
require.Nil(t, err) | ||
|
||
require.Equal(t, expectedLeaseResponse, parsedResponse, "Returns a single lease.") | ||
require.Equal(t, actualResponse.StatusCode, 200, "Returns a 200.") | ||
}) | ||
|
||
t.Run("When the query fails", func(t *testing.T) { | ||
expectedError := errors.New("Error") | ||
mockDb := mocks.DBer{} | ||
mockDb.On("GetLeaseByID", "unique-id").Return(nil, expectedError) | ||
mockRequest := events.APIGatewayProxyRequest{HTTPMethod: http.MethodGet, Path: "/accounts/unique-id"} | ||
|
||
controller := getController{ | ||
Dao: &mockDb, | ||
} | ||
|
||
actualResponse, err := controller.Call(context.TODO(), &mockRequest) | ||
require.Nil(t, err) | ||
|
||
require.Equal(t, actualResponse.StatusCode, 500, "Returns a 500.") | ||
require.Equal(t, actualResponse.Body, "{\"error\":{\"code\":\"ServerError\",\"message\":\"Failed Get on Lease unique-id\"}}", "Returns an error response.") | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.