Skip to content

Commit

Permalink
Merge pull request #15 from Linesmerrill/fix/add-communityID-check
Browse files Browse the repository at this point in the history
add check for null community id
  • Loading branch information
Linesmerrill authored Mar 26, 2022
2 parents b0e6687 + c9bff04 commit 87000d4
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 3 deletions.
2 changes: 1 addition & 1 deletion api/handlers/civilian.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (c Civilian) CiviliansByUserIDHandler(w http.ResponseWriter, r *http.Reques
// Likewise, if the user is not in a community, then we will display only the civilians
// that are not in a community
var err error
if activeCommunityID != "" {
if activeCommunityID != "" && activeCommunityID != "null" && activeCommunityID != "undefined" {
dbResp, err = c.DB.Find(context.TODO(), bson.M{
"civilian.userID": userID,
"civilian.activeCommunityID": activeCommunityID,
Expand Down
48 changes: 48 additions & 0 deletions api/handlers/civilian_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,54 @@ func TestCivilian_CiviliansByUserIDHandlerSuccessWithActiveCommunityID(t *testin
assert.Equal(t, "5fc51f36c72ff10004dca381", testCivilian[0].ID)
}

func TestCivilian_CiviliansByUserIDHandlerSuccessWithNullCommunityID(t *testing.T) {
req, err := http.NewRequest("GET", "/api/v1/civilians/user/61be0ebf22cfea7e7550f00e?active_community_id=null", nil)
if err != nil {
t.Fatal(err)
}

req.Header.Set("Authorization", "Bearer abc123")

var db databases.DatabaseHelper
var client databases.ClientHelper
var conn databases.CollectionHelper
var singleResultHelper databases.SingleResultHelper

db = &MockDatabaseHelper{} // can be used as db = &mocks.DatabaseHelper{}
client = &mocks.ClientHelper{}
conn = &mocks.CollectionHelper{}
singleResultHelper = &mocks.SingleResultHelper{}

client.(*mocks.ClientHelper).On("StartSession").Return(nil, errors.New("mocked-error"))
db.(*MockDatabaseHelper).On("Client").Return(client)
singleResultHelper.(*mocks.SingleResultHelper).On("Decode", mock.Anything).Return(nil).Run(func(args mock.Arguments) {
arg := args.Get(0).(*[]models.Civilian)
*arg = []models.Civilian{{ID: "5fc51f36c72ff10004dca381"}}

})
conn.(*mocks.CollectionHelper).On("Find", mock.Anything, mock.Anything).Return(singleResultHelper)
db.(*MockDatabaseHelper).On("Collection", "civilians").Return(conn)

civilianDatabase := databases.NewCivilianDatabase(db)
u := handlers.Civilian{
DB: civilianDatabase,
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(u.CiviliansByUserIDHandler)

handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusBadRequest)
}

var testCivilian []models.Civilian
_ = json.Unmarshal(rr.Body.Bytes(), &testCivilian)

assert.Equal(t, "5fc51f36c72ff10004dca381", testCivilian[0].ID)
}

func TestCivilian_CiviliansByUserIDHandlerEmptyResponse(t *testing.T) {
req, err := http.NewRequest("GET", "/api/v1/civilians/user/1234", nil)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion api/handlers/firearm.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (v Firearm) FirearmsByUserIDHandler(w http.ResponseWriter, r *http.Request)
// Likewise, if the user is not in a community, then we will display only the firearms
// that are not in a community
var err error
if activeCommunityID != "" {
if activeCommunityID != "" && activeCommunityID != "null" && activeCommunityID != "undefined" {
dbResp, err = v.DB.Find(context.TODO(), bson.M{
"firearm.userID": userID,
"firearm.activeCommunityID": activeCommunityID,
Expand Down
48 changes: 48 additions & 0 deletions api/handlers/firearm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,54 @@ func TestFirearm_FirearmsByUserIDHandlerSuccessWithActiveCommunityID(t *testing.
assert.Equal(t, "5fc51f36c72ff10004dca381", testFirearm[0].ID)
}

func TestFirearm_FirearmsByUserIDHandlerSuccessWithNullCommunityID(t *testing.T) {
req, err := http.NewRequest("GET", "/api/v1/firearms/user/61be0ebf22cfea7e7550f00e?active_community_id=null", nil)
if err != nil {
t.Fatal(err)
}

req.Header.Set("Authorization", "Bearer abc123")

var db databases.DatabaseHelper
var client databases.ClientHelper
var conn databases.CollectionHelper
var singleResultHelper databases.SingleResultHelper

db = &MockDatabaseHelper{} // can be used as db = &mocks.DatabaseHelper{}
client = &mocks.ClientHelper{}
conn = &mocks.CollectionHelper{}
singleResultHelper = &mocks.SingleResultHelper{}

client.(*mocks.ClientHelper).On("StartSession").Return(nil, errors.New("mocked-error"))
db.(*MockDatabaseHelper).On("Client").Return(client)
singleResultHelper.(*mocks.SingleResultHelper).On("Decode", mock.Anything).Return(nil).Run(func(args mock.Arguments) {
arg := args.Get(0).(*[]models.Firearm)
*arg = []models.Firearm{{ID: "5fc51f36c72ff10004dca381"}}

})
conn.(*mocks.CollectionHelper).On("Find", mock.Anything, mock.Anything).Return(singleResultHelper)
db.(*MockDatabaseHelper).On("Collection", "firearms").Return(conn)

firearmDatabase := databases.NewFirearmDatabase(db)
u := handlers.Firearm{
DB: firearmDatabase,
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(u.FirearmsByUserIDHandler)

handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusBadRequest)
}

var testFirearm []models.Firearm
_ = json.Unmarshal(rr.Body.Bytes(), &testFirearm)

assert.Equal(t, "5fc51f36c72ff10004dca381", testFirearm[0].ID)
}

func TestFirearm_FirearmsByUserIDHandlerEmptyResponse(t *testing.T) {
req, err := http.NewRequest("GET", "/api/v1/firearms/user/1234", nil)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion api/handlers/vehicle.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (v Vehicle) VehiclesByUserIDHandler(w http.ResponseWriter, r *http.Request)
// Likewise, if the user is not in a community, then we will display only the vehicles
// that are not in a community
var err error
if activeCommunityID != "" {
if activeCommunityID != "" && activeCommunityID != "null" && activeCommunityID != "undefined" {
dbResp, err = v.DB.Find(context.TODO(), bson.M{
"vehicle.userID": userID,
"vehicle.activeCommunityID": activeCommunityID,
Expand Down
48 changes: 48 additions & 0 deletions api/handlers/vehicle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,54 @@ func TestVehicle_VehiclesByUserIDHandlerSuccessWithActiveCommunityID(t *testing.
assert.Equal(t, "5fc51f36c72ff10004dca381", testVehicle[0].ID)
}

func TestVehicle_VehiclesByUserIDHandlerSuccessWithNullCommunityID(t *testing.T) {
req, err := http.NewRequest("GET", "/api/v1/vehicles/user/61be0ebf22cfea7e7550f00e?active_community_id=null", nil)
if err != nil {
t.Fatal(err)
}

req.Header.Set("Authorization", "Bearer abc123")

var db databases.DatabaseHelper
var client databases.ClientHelper
var conn databases.CollectionHelper
var singleResultHelper databases.SingleResultHelper

db = &MockDatabaseHelper{} // can be used as db = &mocks.DatabaseHelper{}
client = &mocks.ClientHelper{}
conn = &mocks.CollectionHelper{}
singleResultHelper = &mocks.SingleResultHelper{}

client.(*mocks.ClientHelper).On("StartSession").Return(nil, errors.New("mocked-error"))
db.(*MockDatabaseHelper).On("Client").Return(client)
singleResultHelper.(*mocks.SingleResultHelper).On("Decode", mock.Anything).Return(nil).Run(func(args mock.Arguments) {
arg := args.Get(0).(*[]models.Vehicle)
*arg = []models.Vehicle{{ID: "5fc51f36c72ff10004dca381"}}

})
conn.(*mocks.CollectionHelper).On("Find", mock.Anything, mock.Anything).Return(singleResultHelper)
db.(*MockDatabaseHelper).On("Collection", "vehicles").Return(conn)

vehicleDatabase := databases.NewVehicleDatabase(db)
u := handlers.Vehicle{
DB: vehicleDatabase,
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(u.VehiclesByUserIDHandler)

handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusBadRequest)
}

var testVehicle []models.Vehicle
_ = json.Unmarshal(rr.Body.Bytes(), &testVehicle)

assert.Equal(t, "5fc51f36c72ff10004dca381", testVehicle[0].ID)
}

func TestVehicle_VehiclesByUserIDHandlerEmptyResponse(t *testing.T) {
req, err := http.NewRequest("GET", "/api/v1/vehicles/user/1234", nil)
if err != nil {
Expand Down

0 comments on commit 87000d4

Please sign in to comment.