55bdebe8b2
* internal/engine/ooapi: auto-generated API client * feat: introduce the callers abstraction * feat: implement API caching on disk * feat: implement cloneWithToken when we require login * feat: implement login * fix: do not cache all APIs * feat: start making space for more tests * feat: implement caching policy * feat: write tests for caching layer * feat: add integration tests and fix some minor issues * feat: write much more unit tests * feat: add some more easy unit tests * feat: add tests that use a local server While there, make sure many fields we care about are OK. * doc: write basic documentation * fix: tweak sentence * doc: improve ooapi documentation * doc(ooapi): other documentation improvements * fix(ooapi): remove caching for most APIs We discussed this topic yesterday with @FedericoCeratto. The only place where we want LRU caching is MeasurementMeta. * feat(ooapi): improve handling of errors during login This was also discussed yesterday with @FedericoCeratto * fix(swaggerdiff_test.go): temporarily disable Before I work on this, I need to tend onto other tasks. * fix(ootest): add one more test case We're going towards 100% coverage of this package, as it ought to be. * feat(ooapi): test cases for when the probe clock is off * fix(ooapi): change test to have 100% unittest coverage * feat: sync server and client APIs definition Companion PR: https://github.com/ooni/api/pull/218 * fix(ooapi): start testing again against API * fix(ooapi): only generate each file once * chore: set version to 3.7.0-alpha While there, make sure we don't always skip a currently failing riseupvpn test, and slightly clarify the readme. * fix(kvstore): less scoped error message
205 lines
4.4 KiB
Go
205 lines
4.4 KiB
Go
package ooapi
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/ooapi/apimodel"
|
|
)
|
|
|
|
type VerboseHTTPClient struct {
|
|
t *testing.T
|
|
}
|
|
|
|
func (c *VerboseHTTPClient) Do(req *http.Request) (*http.Response, error) {
|
|
c.t.Logf("> %s %s", req.Method, req.URL.String())
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
c.t.Logf("< %s", err.Error())
|
|
return nil, err
|
|
}
|
|
c.t.Logf("< %d", resp.StatusCode)
|
|
return resp, nil
|
|
}
|
|
|
|
func TestWithRealServerDoCheckIn(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skip test in short mode")
|
|
}
|
|
req := &apimodel.CheckInRequest{
|
|
Charging: true,
|
|
OnWiFi: true,
|
|
Platform: "android",
|
|
ProbeASN: "AS12353",
|
|
ProbeCC: "IT",
|
|
RunType: "timed",
|
|
SoftwareName: "ooniprobe-android",
|
|
SoftwareVersion: "2.7.1",
|
|
WebConnectivity: apimodel.CheckInRequestWebConnectivity{
|
|
CategoryCodes: []string{"NEWS", "CULTR"},
|
|
},
|
|
}
|
|
httpClnt := &VerboseHTTPClient{t: t}
|
|
api := &CheckInAPI{
|
|
HTTPClient: httpClnt,
|
|
}
|
|
ctx := context.Background()
|
|
resp, err := api.Call(ctx, req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non nil pointer here")
|
|
}
|
|
for idx, url := range resp.Tests.WebConnectivity.URLs {
|
|
if idx >= 3 {
|
|
break
|
|
}
|
|
t.Logf("- %+v", url)
|
|
}
|
|
}
|
|
|
|
func TestWithRealServerDoCheckReportID(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skip test in short mode")
|
|
}
|
|
req := &apimodel.CheckReportIDRequest{
|
|
ReportID: "20210223T093606Z_ndt_JO_8376_n1_kDYToqrugDY54Soy",
|
|
}
|
|
api := &CheckReportIDAPI{}
|
|
ctx := context.Background()
|
|
resp, err := api.Call(ctx, req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non nil pointer here")
|
|
}
|
|
t.Logf("%+v", resp)
|
|
}
|
|
|
|
func TestWithRealServerDoMeasurementMeta(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skip test in short mode")
|
|
}
|
|
req := &apimodel.MeasurementMetaRequest{
|
|
ReportID: "20210223T093606Z_ndt_JO_8376_n1_kDYToqrugDY54Soy",
|
|
}
|
|
api := &MeasurementMetaAPI{}
|
|
ctx := context.Background()
|
|
resp, err := api.Call(ctx, req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non nil pointer here")
|
|
}
|
|
t.Logf("%+v", resp)
|
|
}
|
|
|
|
func TestWithRealServerDoOpenReport(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skip test in short mode")
|
|
}
|
|
req := &apimodel.OpenReportRequest{
|
|
DataFormatVersion: "0.2.0",
|
|
Format: "json",
|
|
ProbeASN: "AS137",
|
|
ProbeCC: "IT",
|
|
SoftwareName: "miniooni",
|
|
SoftwareVersion: "0.1.0-dev",
|
|
TestName: "example",
|
|
TestStartTime: "2018-11-01 15:33:20",
|
|
TestVersion: "0.1.0",
|
|
}
|
|
api := &OpenReportAPI{}
|
|
ctx := context.Background()
|
|
resp, err := api.Call(ctx, req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non nil pointer here")
|
|
}
|
|
t.Logf("%+v", resp)
|
|
}
|
|
|
|
func TestWithRealServerDoPsiphonConfig(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skip test in short mode")
|
|
}
|
|
req := &apimodel.PsiphonConfigRequest{}
|
|
httpClnt := &VerboseHTTPClient{t: t}
|
|
api := &PsiphonConfigAPIWithLogin{
|
|
API: &PsiphonConfigAPI{
|
|
HTTPClient: httpClnt,
|
|
},
|
|
KVStore: &memkvstore{},
|
|
RegisterAPI: &RegisterAPI{
|
|
HTTPClient: httpClnt,
|
|
},
|
|
LoginAPI: &LoginAPI{
|
|
HTTPClient: httpClnt,
|
|
},
|
|
}
|
|
ctx := context.Background()
|
|
resp, err := api.Call(ctx, req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non nil pointer here")
|
|
}
|
|
t.Logf("%+v", resp != nil)
|
|
}
|
|
|
|
func TestWithRealServerDoTorTargets(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skip test in short mode")
|
|
}
|
|
req := &apimodel.TorTargetsRequest{}
|
|
httpClnt := &VerboseHTTPClient{t: t}
|
|
api := &TorTargetsAPIWithLogin{
|
|
API: &TorTargetsAPI{
|
|
HTTPClient: httpClnt,
|
|
},
|
|
KVStore: &memkvstore{},
|
|
RegisterAPI: &RegisterAPI{
|
|
HTTPClient: httpClnt,
|
|
},
|
|
LoginAPI: &LoginAPI{
|
|
HTTPClient: httpClnt,
|
|
},
|
|
}
|
|
ctx := context.Background()
|
|
resp, err := api.Call(ctx, req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non nil pointer here")
|
|
}
|
|
t.Logf("%+v", resp != nil)
|
|
}
|
|
|
|
func TestWithRealServerDoURLs(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skip test in short mode")
|
|
}
|
|
req := &apimodel.URLsRequest{
|
|
CountryCode: "IT",
|
|
Limit: 3,
|
|
}
|
|
api := &URLsAPI{}
|
|
ctx := context.Background()
|
|
resp, err := api.Call(ctx, req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non nil pointer here")
|
|
}
|
|
t.Logf("%+v", resp)
|
|
}
|