Skip to content

Commit

Permalink
ran prepare-for-codereview
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanieslamb committed Sep 18, 2024
1 parent d65ef03 commit 2ba5989
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 118 deletions.
208 changes: 104 additions & 104 deletions cmd/api/src/api/v2/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,122 +243,122 @@ func TestResources_ListAuditLogs_Filtered(t *testing.T) {
}

func TestResources_ListAuditLogs_SkipAndOffset(t *testing.T) {
var (
mockCtrl = gomock.NewController(t)
mockDB = mocks.NewMockDatabase(mockCtrl)
resources = v2.Resources{DB: mockDB}
)
defer mockCtrl.Finish()

mockDB.EXPECT().ListAuditLogs(gomock.Any(), gomock.Any(), gomock.Any(), 10, gomock.Any(), "", model.SQLFilter{}).Return(model.AuditLogs{}, 1000, nil)

endpoint := "/api/v2/audit"

if req, err := http.NewRequest("GET", endpoint, nil); err != nil {
t.Fatal(err)
} else {
q := url.Values{}
q.Add("skip", "10")
q.Add("offset", "20")

req.Header.Set(headers.ContentType.String(), mediatypes.ApplicationJson.String())
req.URL.RawQuery = q.Encode()

router := mux.NewRouter()
router.HandleFunc(endpoint, resources.ListAuditLogs).Methods("GET")

response := httptest.NewRecorder()
router.ServeHTTP(response, req)
require.Equal(t, http.StatusOK, response.Code)
}
var (
mockCtrl = gomock.NewController(t)
mockDB = mocks.NewMockDatabase(mockCtrl)
resources = v2.Resources{DB: mockDB}
)
defer mockCtrl.Finish()

mockDB.EXPECT().ListAuditLogs(gomock.Any(), gomock.Any(), gomock.Any(), 10, gomock.Any(), "", model.SQLFilter{}).Return(model.AuditLogs{}, 1000, nil)

endpoint := "/api/v2/audit"

if req, err := http.NewRequest("GET", endpoint, nil); err != nil {
t.Fatal(err)
} else {
q := url.Values{}
q.Add("skip", "10")
q.Add("offset", "20")

req.Header.Set(headers.ContentType.String(), mediatypes.ApplicationJson.String())
req.URL.RawQuery = q.Encode()

router := mux.NewRouter()
router.HandleFunc(endpoint, resources.ListAuditLogs).Methods("GET")

response := httptest.NewRecorder()
router.ServeHTTP(response, req)
require.Equal(t, http.StatusOK, response.Code)
}
}

func TestResources_ListAuditLogs_OnlyOffset(t *testing.T) {
var (
mockCtrl = gomock.NewController(t)
mockDB = mocks.NewMockDatabase(mockCtrl)
resources = v2.Resources{DB: mockDB}
)
defer mockCtrl.Finish()
var (
mockCtrl = gomock.NewController(t)
mockDB = mocks.NewMockDatabase(mockCtrl)
resources = v2.Resources{DB: mockDB}
)
defer mockCtrl.Finish()

mockDB.EXPECT().ListAuditLogs(gomock.Any(), gomock.Any(), gomock.Any(), 20, gomock.Any(), "", model.SQLFilter{}).Return(model.AuditLogs{}, 1000, nil)
mockDB.EXPECT().ListAuditLogs(gomock.Any(), gomock.Any(), gomock.Any(), 20, gomock.Any(), "", model.SQLFilter{}).Return(model.AuditLogs{}, 1000, nil)

endpoint := "/api/v2/audit"
endpoint := "/api/v2/audit"

if req, err := http.NewRequest("GET", endpoint, nil); err != nil {
t.Fatal(err)
} else {
q := url.Values{}
q.Add("offset", "20")
if req, err := http.NewRequest("GET", endpoint, nil); err != nil {
t.Fatal(err)
} else {
q := url.Values{}
q.Add("offset", "20")

req.Header.Set(headers.ContentType.String(), mediatypes.ApplicationJson.String())
req.URL.RawQuery = q.Encode()
req.Header.Set(headers.ContentType.String(), mediatypes.ApplicationJson.String())
req.URL.RawQuery = q.Encode()

router := mux.NewRouter()
router.HandleFunc(endpoint, resources.ListAuditLogs).Methods("GET")
router := mux.NewRouter()
router.HandleFunc(endpoint, resources.ListAuditLogs).Methods("GET")

response := httptest.NewRecorder()
router.ServeHTTP(response, req)
require.Equal(t, http.StatusOK, response.Code)
}
response := httptest.NewRecorder()
router.ServeHTTP(response, req)
require.Equal(t, http.StatusOK, response.Code)
}
}

func TestResources_ListAuditLogs_OnlySkip(t *testing.T) {
var (
mockCtrl = gomock.NewController(t)
mockDB = mocks.NewMockDatabase(mockCtrl)
resources = v2.Resources{DB: mockDB}
)
defer mockCtrl.Finish()

// Expect skip to be 5 (from "skip" parameter)
mockDB.EXPECT().ListAuditLogs(gomock.Any(), gomock.Any(), gomock.Any(), 5, gomock.Any(), gomock.Any(), gomock.Any()).Return(model.AuditLogs{}, 1000, nil)

endpoint := "/api/v2/audit"

if req, err := http.NewRequest("GET", endpoint, nil); err != nil {
t.Fatal(err)
} else {
q := url.Values{}
q.Add("skip", "5")

req.Header.Set(headers.ContentType.String(), mediatypes.ApplicationJson.String())
req.URL.RawQuery = q.Encode()

router := mux.NewRouter()
router.HandleFunc(endpoint, resources.ListAuditLogs).Methods("GET")

response := httptest.NewRecorder()
router.ServeHTTP(response, req)
require.Equal(t, http.StatusOK, response.Code)
}
var (
mockCtrl = gomock.NewController(t)
mockDB = mocks.NewMockDatabase(mockCtrl)
resources = v2.Resources{DB: mockDB}
)
defer mockCtrl.Finish()

// Expect skip to be 5 (from "skip" parameter)
mockDB.EXPECT().ListAuditLogs(gomock.Any(), gomock.Any(), gomock.Any(), 5, gomock.Any(), gomock.Any(), gomock.Any()).Return(model.AuditLogs{}, 1000, nil)

endpoint := "/api/v2/audit"

if req, err := http.NewRequest("GET", endpoint, nil); err != nil {
t.Fatal(err)
} else {
q := url.Values{}
q.Add("skip", "5")

req.Header.Set(headers.ContentType.String(), mediatypes.ApplicationJson.String())
req.URL.RawQuery = q.Encode()

router := mux.NewRouter()
router.HandleFunc(endpoint, resources.ListAuditLogs).Methods("GET")

response := httptest.NewRecorder()
router.ServeHTTP(response, req)
require.Equal(t, http.StatusOK, response.Code)
}
}

func TestResources_ListAuditLogs_InvalidSkip(t *testing.T) {
var (
mockCtrl = gomock.NewController(t)
mockDB = mocks.NewMockDatabase(mockCtrl)
resources = v2.Resources{DB: mockDB}
)
defer mockCtrl.Finish()

endpoint := "/api/v2/audit"

if req, err := http.NewRequest("GET", endpoint, nil); err != nil {
t.Fatal(err)
} else {
q := url.Values{}
q.Add("skip", "invalid")

req.Header.Set(headers.ContentType.String(), mediatypes.ApplicationJson.String())
req.URL.RawQuery = q.Encode()

router := mux.NewRouter()
router.HandleFunc(endpoint, resources.ListAuditLogs).Methods("GET")

response := httptest.NewRecorder()
router.ServeHTTP(response, req)
require.Equal(t, http.StatusBadRequest, response.Code)
require.Contains(t, response.Body.String(), "query parameter \\\"skip\\\" is malformed")
}
}
var (
mockCtrl = gomock.NewController(t)
mockDB = mocks.NewMockDatabase(mockCtrl)
resources = v2.Resources{DB: mockDB}
)
defer mockCtrl.Finish()

endpoint := "/api/v2/audit"

if req, err := http.NewRequest("GET", endpoint, nil); err != nil {
t.Fatal(err)
} else {
q := url.Values{}
q.Add("skip", "invalid")

req.Header.Set(headers.ContentType.String(), mediatypes.ApplicationJson.String())
req.URL.RawQuery = q.Encode()

router := mux.NewRouter()
router.HandleFunc(endpoint, resources.ListAuditLogs).Methods("GET")

response := httptest.NewRecorder()
router.ServeHTTP(response, req)
require.Equal(t, http.StatusBadRequest, response.Code)
require.Contains(t, response.Body.String(), "query parameter \\\"skip\\\" is malformed")
}
}
18 changes: 9 additions & 9 deletions cmd/api/src/api/v2/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ func ParseSkipQueryParameter(params url.Values, defaultValue int) (int, error) {
}

func ParseSkipQueryParameterWithKey(params url.Values, key string, defaultValue int) (int, error) {
if param := params.Get(key); param == "" {
return defaultValue, nil
} else if skip, err := strconv.Atoi(param); err != nil {
return 0, fmt.Errorf("error converting %s value %v to int: %v", key, param, err)
} else if skip < 0 {
return 0, fmt.Errorf(utils.ErrorInvalidSkip, skip)
} else {
return skip, nil
}
if param := params.Get(key); param == "" {
return defaultValue, nil
} else if skip, err := strconv.Atoi(param); err != nil {
return 0, fmt.Errorf("error converting %s value %v to int: %v", key, param, err)
} else if skip < 0 {
return 0, fmt.Errorf(utils.ErrorInvalidSkip, skip)
} else {
return skip, nil
}
}

func ParseLimitQueryParameter(params url.Values, defaultValue int) (int, error) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/src/bootstrap/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Initializer[DBType database.Database, GraphType graph.Database] struct {
Configuration config.Configuration
PreMigrationDaemons InitializerLogic[DBType, GraphType]
Entrypoint InitializerLogic[DBType, GraphType]
DBConnector DatabaseConstructor[DBType, GraphType]
DBConnector DatabaseConstructor[DBType, GraphType]
}

func (s Initializer[DBType, GraphType]) Launch(parentCtx context.Context, handleSignals bool) error {
Expand Down
6 changes: 3 additions & 3 deletions cmd/api/src/cmd/bhapi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ func main() {
log.Fatalf("Unable to read configuration %s: %v", configFilePath, err)
} else {
initializer := bootstrap.Initializer[*database.BloodhoundDB, *graph.DatabaseSwitch]{
Configuration: cfg,
DBConnector: services.ConnectDatabases,
Configuration: cfg,
DBConnector: services.ConnectDatabases,
PreMigrationDaemons: services.PreMigrationDaemons,
Entrypoint: services.Entrypoint,
Entrypoint: services.Entrypoint,
}

if err := initializer.Launch(context.Background(), true); err != nil {
Expand Down
16 changes: 16 additions & 0 deletions cmd/api/src/database/parameters_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
// Copyright 2024 Specter Ops, Inc.
//
// Licensed under the Apache License, Version 2.0
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

package database_test

import (
Expand Down
16 changes: 16 additions & 0 deletions cmd/api/src/model/appcfg/parameter_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
// Copyright 2024 Specter Ops, Inc.
//
// Licensed under the Apache License, Version 2.0
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

package appcfg_test

import (
Expand Down
16 changes: 16 additions & 0 deletions cmd/api/src/utils/validation/duration_validator.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
// Copyright 2024 Specter Ops, Inc.
//
// Licensed under the Apache License, Version 2.0
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

package validation

import (
Expand Down
16 changes: 16 additions & 0 deletions cmd/api/src/utils/validation/duration_validator_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
// Copyright 2024 Specter Ops, Inc.
//
// Licensed under the Apache License, Version 2.0
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

package validation_test

import (
Expand Down
2 changes: 1 addition & 1 deletion packages/go/analysis/ad/esc4.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func PostADCSESC4(ctx context.Context, tx graph.Transaction, outC chan<- analysi
} else {

var (
enterpriseCAEnrollers = cache.GetEnterpriseCAEnrollers(enterpriseCA.ID)
enterpriseCAEnrollers = cache.GetEnterpriseCAEnrollers(enterpriseCA.ID)
certTemplateControllers = cache.GetCertTemplateControllers(certTemplate.ID)
)

Expand Down

0 comments on commit 2ba5989

Please sign in to comment.