diff --git a/lib/go-tc/profiles.go b/lib/go-tc/profiles.go index eff7516954..73d97c6d17 100644 --- a/lib/go-tc/profiles.go +++ b/lib/go-tc/profiles.go @@ -92,6 +92,12 @@ type Profile struct { Parameters []ParameterNullable `json:"params,omitempty"` } +// ProfilesResponseV5 is a list of profiles returned by GET requests. +type ProfilesResponseV5 struct { + Response []ProfileV5 `json:"response"` + Alerts +} + // A ProfileV5 represents a set of configuration for a server or Delivery Service // which may be reused to allow sharing configuration across the objects to // which it is assigned. Note: Field LastUpdated represents RFC3339 diff --git a/traffic_ops/testing/api/v5/cdn_locks_test.go b/traffic_ops/testing/api/v5/cdn_locks_test.go index 583d22cb41..b461c7a4e0 100644 --- a/traffic_ops/testing/api/v5/cdn_locks_test.go +++ b/traffic_ops/testing/api/v5/cdn_locks_test.go @@ -616,7 +616,7 @@ func TestCDNLocks(t *testing.T) { } }, "PROFILE POST": func(t *testing.T) { - profile := tc.Profile{} + profile := tc.ProfileV5{} err = json.Unmarshal(dat, &profile) assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err) alerts, reqInf, err := testCase.ClientSession.CreateProfile(profile, testCase.RequestOpts) @@ -625,7 +625,7 @@ func TestCDNLocks(t *testing.T) { } }, "PROFILE PUT": func(t *testing.T) { - profile := tc.Profile{} + profile := tc.ProfileV5{} err = json.Unmarshal(dat, &profile) assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err) alerts, reqInf, err := testCase.ClientSession.UpdateProfile(testCase.EndpointID(), profile, testCase.RequestOpts) diff --git a/traffic_ops/testing/api/v5/profiles_test.go b/traffic_ops/testing/api/v5/profiles_test.go index f5006a0523..b5845e5bd8 100644 --- a/traffic_ops/testing/api/v5/profiles_test.go +++ b/traffic_ops/testing/api/v5/profiles_test.go @@ -37,7 +37,7 @@ func TestProfiles(t *testing.T) { currentTimeRFC := currentTime.Format(time.RFC1123) tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123) - methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.Profile]{ + methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.ProfileV5]{ "GET": { "NOT MODIFIED when NO CHANGES made": { ClientSession: TOSession, @@ -110,7 +110,7 @@ func TestProfiles(t *testing.T) { "POST": { "BAD REQUEST when NAME has SPACES": { ClientSession: TOSession, - RequestBody: tc.Profile{ + RequestBody: tc.ProfileV5{ CDNID: GetCDNID(t, "cdn1")(), Description: "name has spaces test", Name: "name has space", @@ -120,12 +120,12 @@ func TestProfiles(t *testing.T) { }, "BAD REQUEST when MISSING ALL FIELDS": { ClientSession: TOSession, - RequestBody: tc.Profile{}, + RequestBody: tc.ProfileV5{}, Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)), }, "BAD REQUEST when INVALID CDN ID": { ClientSession: TOSession, - RequestBody: tc.Profile{ + RequestBody: tc.ProfileV5{ CDNID: 0, Description: "description", Name: "badprofile", @@ -135,7 +135,7 @@ func TestProfiles(t *testing.T) { }, "BAD REQUEST when MISSING DESCRIPTION FIELD": { ClientSession: TOSession, - RequestBody: tc.Profile{ + RequestBody: tc.ProfileV5{ CDNID: GetCDNID(t, "cdn1")(), Name: "missing_description", Type: tc.CacheServerProfileType, @@ -144,7 +144,7 @@ func TestProfiles(t *testing.T) { }, "BAD REQUEST when MISSING NAME FIELD": { ClientSession: TOSession, - RequestBody: tc.Profile{ + RequestBody: tc.ProfileV5{ CDNID: GetCDNID(t, "cdn1")(), Description: "missing name", Type: tc.CacheServerProfileType, @@ -153,7 +153,7 @@ func TestProfiles(t *testing.T) { }, "BAD REQUEST when MISSING TYPE FIELD": { ClientSession: TOSession, - RequestBody: tc.Profile{ + RequestBody: tc.ProfileV5{ CDNID: GetCDNID(t, "cdn1")(), Description: "missing type", Name: "missing_type", @@ -165,7 +165,7 @@ func TestProfiles(t *testing.T) { "OK when VALID REQUEST": { EndpointID: GetProfileID(t, "EDGE2"), ClientSession: TOSession, - RequestBody: tc.Profile{ + RequestBody: tc.ProfileV5{ CDNID: GetCDNID(t, "cdn2")(), Description: "edge2 description updated", Name: "EDGE2UPDATED", @@ -181,7 +181,7 @@ func TestProfiles(t *testing.T) { EndpointID: GetProfileID(t, "CCR1"), ClientSession: TOSession, RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfUnmodifiedSince: {currentTimeRFC}}}, - RequestBody: tc.Profile{ + RequestBody: tc.ProfileV5{ CDNID: GetCDNID(t, "cdn1")(), Description: "cdn1 description", Name: "CCR1", @@ -193,7 +193,7 @@ func TestProfiles(t *testing.T) { "PRECONDITION FAILED when updating with IFMATCH ETAG Header": { EndpointID: GetProfileID(t, "CCR1"), ClientSession: TOSession, - RequestBody: tc.Profile{ + RequestBody: tc.ProfileV5{ CDNID: GetCDNID(t, "cdn1")(), Description: "cdn1 description", Name: "CCR1", @@ -248,7 +248,7 @@ func TestProfiles(t *testing.T) { func validateProfilesFields(expectedResp map[string]interface{}) utils.CkReqFunc { return func(t *testing.T, _ toclientlib.ReqInf, resp interface{}, _ tc.Alerts, _ error) { assert.RequireNotNil(t, resp, "Expected Profiles response to not be nil.") - profileResp := resp.([]tc.Profile) + profileResp := resp.([]tc.ProfileV5) for field, expected := range expectedResp { for _, profile := range profileResp { switch field { @@ -291,7 +291,7 @@ func validateProfilesUpdateCreateFields(name string, expectedResp map[string]int func validateProfilesPagination(paginationParam string) utils.CkReqFunc { return func(t *testing.T, _ toclientlib.ReqInf, resp interface{}, _ tc.Alerts, _ error) { - paginationResp := resp.([]tc.Profile) + paginationResp := resp.([]tc.ProfileV5) opts := client.NewRequestOptions() opts.QueryParameters.Set("orderby", "id") diff --git a/traffic_ops/traffic_ops_golang/dbhelpers/db_helpers_test.go b/traffic_ops/traffic_ops_golang/dbhelpers/db_helpers_test.go index 3f9d2123dd..7dd024239f 100644 --- a/traffic_ops/traffic_ops_golang/dbhelpers/db_helpers_test.go +++ b/traffic_ops/traffic_ops_golang/dbhelpers/db_helpers_test.go @@ -664,3 +664,54 @@ func TestDivisionExists(t *testing.T) { }) } } + +func TestProfileExists(t *testing.T) { + var testCases = []struct { + description string + id string + expectedError error + exists bool + }{ + { + description: "Success: Get valid Profile", + id: "1", + expectedError: nil, + exists: true, + }, + { + description: "Failure: Profile not in DB", + id: "5", + expectedError: sql.ErrNoRows, + exists: false, + }, + } + for _, testCase := range testCases { + t.Run(testCase.description, func(t *testing.T) { + mockDB, mock, err := sqlmock.New() + if err != nil { + t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) + } + defer mockDB.Close() + + db := sqlx.NewDb(mockDB, "sqlmock") + defer db.Close() + + mock.ExpectBegin() + rows := sqlmock.NewRows([]string{"EXISTS"}) + if testCase.exists { + rows = rows.AddRow(1) + } + mock.ExpectQuery("SELECT").WillReturnRows(rows) + mock.ExpectCommit() + + profileExists, err := ASNExists(db.MustBegin().Tx, testCase.id) + if testCase.exists != profileExists { + t.Errorf("Expected return exists: %t, actual %t", testCase.exists, profileExists) + } + + if !errors.Is(err, testCase.expectedError) { + t.Errorf("getSCInfo expected: %s, actual: %s", testCase.expectedError, err) + } + }) + } +} diff --git a/traffic_ops/traffic_ops_golang/profile/profiles.go b/traffic_ops/traffic_ops_golang/profile/profiles.go index 58b409d5ae..6cb1f8bd8c 100644 --- a/traffic_ops/traffic_ops_golang/profile/profiles.go +++ b/traffic_ops/traffic_ops_golang/profile/profiles.go @@ -436,7 +436,7 @@ func Read(w http.ResponseWriter, r *http.Request) { profileInterfaces = append(profileInterfaces, profile) } - api.WriteResp(w, r, profileList) + api.WriteResp(w, r, profileInterfaces) return } diff --git a/traffic_ops/v5-client/profile.go b/traffic_ops/v5-client/profile.go index 7410348af4..09b18b4d15 100644 --- a/traffic_ops/v5-client/profile.go +++ b/traffic_ops/v5-client/profile.go @@ -78,8 +78,8 @@ func (to *Session) GetParametersByProfileName(profileName string, opts RequestOp } // GetProfiles returns all Profiles stored in Traffic Ops. -func (to *Session) GetProfiles(opts RequestOptions) (tc.ProfilesResponse, toclientlib.ReqInf, error) { - var data tc.ProfilesResponse +func (to *Session) GetProfiles(opts RequestOptions) (tc.ProfilesResponseV5, toclientlib.ReqInf, error) { + var data tc.ProfilesResponseV5 reqInf, err := to.get(apiProfiles, opts, &data) return data, reqInf, err }