Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Wojciech Sadowy <wojciech.sadowy@freedomfi.com>
  • Loading branch information
Wojciech Sadowy committed Apr 1, 2022
1 parent 548adcc commit be2d1de
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
11 changes: 4 additions & 7 deletions dp/cloud/go/services/dp/obsidian/cbsd/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"fmt"
"net/http"
"strconv"
"strings"

"github.com/go-openapi/strfmt"
"github.com/golang/glog"
Expand All @@ -40,9 +39,8 @@ const (
DpPath = obsidian.V1Root + Dp
ManageNetworkPath = DpPath + obsidian.UrlSep + ":network_id"

ManageCbsdsPath = ManageNetworkPath + obsidian.UrlSep + "cbsds"
ManageCbsdPath = ManageCbsdsPath + obsidian.UrlSep + ":cbsd_id"
duplicateRecordMsg = "duplicate key value violates unique constraint"
ManageCbsdsPath = ManageNetworkPath + obsidian.UrlSep + "cbsds"
ManageCbsdPath = ManageCbsdsPath + obsidian.UrlSep + ":cbsd_id"
)

const baseWrongValMsg = "'%s' is not a proper value for %s"
Expand Down Expand Up @@ -217,10 +215,9 @@ func getHttpError(err error) error {
switch s, _ := status.FromError(err); s.Code() {
case codes.NotFound:
return echo.NewHTTPError(http.StatusNotFound, err)
case codes.AlreadyExists:
return echo.NewHTTPError(http.StatusConflict, err)
default:
if strings.Contains(fmt.Sprintf("%s", err), duplicateRecordMsg) == true {
return echo.NewHTTPError(http.StatusBadRequest, err)
}
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
}
Expand Down
2 changes: 2 additions & 0 deletions dp/cloud/go/services/dp/servicers/cbsd_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ func makeErr(err error, wrap string) error {
code := codes.Internal
if err == merrors.ErrNotFound {
code = codes.NotFound
} else if err == merrors.ErrAlreadyExists {
code = codes.AlreadyExists
}
return status.Error(code, e.Error())
}
8 changes: 4 additions & 4 deletions dp/cloud/python/magma/test_runner/tests/test_dp_with_orc8r.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,18 @@ def test_activity_status(self):

self.delete_cbsd(cbsd_id)

def test_creating_cbsd_with_the_same_unique_fields_returns_400(self):
def test_creating_cbsd_with_the_same_unique_fields_returns_409(self):
self.when_cbsd_is_created(self.get_cbsd_post_data())
self.when_cbsd_is_created(self.get_cbsd_post_data(), expected_status=HTTPStatus.BAD_REQUEST)
self.when_cbsd_is_created(self.get_cbsd_post_data(), expected_status=HTTPStatus.CONFLICT)

def test_updating_cbsd_returns_400_when_setting_existing_serial_num(self):
def test_updating_cbsd_returns_409_when_setting_existing_serial_num(self):
self.when_cbsd_is_created(self.get_cbsd_post_data(serial_number="foo")) # cbsd_id = 1
self.when_cbsd_is_created(self.get_cbsd_post_data(serial_number="bar")) # cbsd_id = 2
self.when_cbsd_is_updated(
cbsd_id=2,
fcc_id=SOME_FCC_ID,
serial_number="foo",
expected_status=HTTPStatus.BAD_REQUEST,
expected_status=HTTPStatus.CONFLICT,
)

def test_fetching_logs_with_custom_filters(self):
Expand Down

0 comments on commit be2d1de

Please sign in to comment.