From 3f7ee48ae8a9a44c641270b37489b2166a9702ae Mon Sep 17 00:00:00 2001 From: Simone Basso Date: Wed, 14 Dec 2022 09:13:11 +0100 Subject: [PATCH] cleanup(probeservices): short-circuit check_report_id Originally included in https://github.com/ooni/probe-cli/pull/997. Reference issue: https://github.com/ooni/probe/issues/2380. --- .../engine/probeservices/checkreportid.go | 28 ++++++------------- .../probeservices/checkreportid_test.go | 25 ----------------- 2 files changed, 8 insertions(+), 45 deletions(-) diff --git a/internal/engine/probeservices/checkreportid.go b/internal/engine/probeservices/checkreportid.go index 09ed20a5e5..bf98a9abfd 100644 --- a/internal/engine/probeservices/checkreportid.go +++ b/internal/engine/probeservices/checkreportid.go @@ -1,26 +1,14 @@ package probeservices -import ( - "context" - "net/url" - - "github.com/ooni/probe-cli/v3/internal/httpx" - "github.com/ooni/probe-cli/v3/internal/model" -) +import "context" // CheckReportID checks whether the given ReportID exists. func (c Client) CheckReportID(ctx context.Context, reportID string) (bool, error) { - query := url.Values{} - query.Add("report_id", reportID) - var response model.OOAPICheckReportIDResponse - err := (&httpx.APIClientTemplate{ - BaseURL: c.BaseURL, - HTTPClient: c.HTTPClient, - Logger: c.Logger, - UserAgent: c.UserAgent, - }).WithBodyLogging().Build().GetJSONWithQuery(ctx, "/api/_/check_report_id", query, &response) - if err != nil { - return false, err - } - return response.Found, nil + // The API has been returning true for some time now. So, it does not make + // sense for us to actually issue the API call. Let's short circuit it. + // + // See https://github.com/ooni/api/blob/80913ffd446e7a46761c4c8fdf3e42174f0ce645/newapi/ooniapi/private.py#L208 + // + // TODO(https://github.com/ooni/probe/issues/2389): remove this code. + return true, nil } diff --git a/internal/engine/probeservices/checkreportid_test.go b/internal/engine/probeservices/checkreportid_test.go index 102f36d4cb..d26693a636 100644 --- a/internal/engine/probeservices/checkreportid_test.go +++ b/internal/engine/probeservices/checkreportid_test.go @@ -2,7 +2,6 @@ package probeservices import ( "context" - "errors" "net/http" "testing" @@ -37,27 +36,3 @@ func TestCheckReportIDWorkingAsIntended(t *testing.T) { t.Fatal("unexpected found value") } } - -func TestCheckReportIDWorkingWithCancelledContext(t *testing.T) { - client := Client{ - APIClientTemplate: httpx.APIClientTemplate{ - BaseURL: "https://api.ooni.io/", - HTTPClient: http.DefaultClient, - Logger: log.Log, - UserAgent: "miniooni/0.1.0-dev", - }, - LoginCalls: &atomicx.Int64{}, - RegisterCalls: &atomicx.Int64{}, - StateFile: NewStateFile(&kvstore.Memory{}), - } - reportID := `20201209T052225Z_urlgetter_IT_30722_n1_E1VUhMz08SEkgYFU` - ctx, cancel := context.WithCancel(context.Background()) - cancel() // fail immediately - found, err := client.CheckReportID(ctx, reportID) - if !errors.Is(err, context.Canceled) { - t.Fatalf("not the error we expected: %+v", err) - } - if found != false { - t.Fatal("unexpected found value") - } -}