2020-11-30 15:15:07 +00:00
package apiclient
import (
"context"
"fmt"
"net/http"
"net/url"
"testing"
2023-06-22 13:01:34 +00:00
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2024-01-04 16:10:36 +00:00
"github.com/crowdsecurity/go-cs-lib/cstest"
2023-07-28 14:35:08 +00:00
"github.com/crowdsecurity/go-cs-lib/ptr"
"github.com/crowdsecurity/go-cs-lib/version"
2023-05-23 08:52:47 +00:00
2020-11-30 15:15:07 +00:00
"github.com/crowdsecurity/crowdsec/pkg/models"
2023-02-06 13:06:14 +00:00
"github.com/crowdsecurity/crowdsec/pkg/modelscapi"
2020-11-30 15:15:07 +00:00
)
func TestDecisionsList ( t * testing . T ) {
log . SetLevel ( log . DebugLevel )
mux , urlx , teardown := setup ( )
defer teardown ( )
mux . HandleFunc ( "/decisions" , func ( w http . ResponseWriter , r * http . Request ) {
testMethod ( t , r , "GET" )
if r . URL . RawQuery == "ip=1.2.3.4" {
2023-12-14 13:54:11 +00:00
assert . Equal ( t , "ip=1.2.3.4" , r . URL . RawQuery )
assert . Equal ( t , "ixu" , r . Header . Get ( "X-Api-Key" ) )
2020-11-30 15:15:07 +00:00
w . WriteHeader ( http . StatusOK )
2021-01-14 15:27:45 +00:00
w . Write ( [ ] byte ( ` [ { "duration":"3h59m55.756182786s","id":4,"origin":"cscli","scenario":"manual 'ban' from '82929df7ee394b73b81252fe3b4e50203yaT2u6nXiaN7Ix9'","scope":"Ip","type":"ban","value":"1.2.3.4"}] ` ) )
2020-11-30 15:15:07 +00:00
} else {
w . WriteHeader ( http . StatusOK )
w . Write ( [ ] byte ( ` null ` ) )
//no results
}
} )
2024-01-04 16:10:36 +00:00
2020-11-30 15:15:07 +00:00
apiURL , err := url . Parse ( urlx + "/" )
2024-01-04 16:10:36 +00:00
require . NoError ( t , err )
2020-11-30 15:15:07 +00:00
//ok answer
auth := & APIKeyTransport {
APIKey : "ixu" ,
}
newcli , err := NewDefaultClient ( apiURL , "v1" , "toto" , auth . Client ( ) )
2024-01-04 16:10:36 +00:00
require . NoError ( t , err )
2020-11-30 15:15:07 +00:00
expected := & models . GetDecisionsResponse {
& models . Decision {
2024-01-04 16:10:36 +00:00
Duration : ptr . Of ( "3h59m55.756182786s" ) ,
2020-11-30 15:15:07 +00:00
ID : 4 ,
2024-01-04 16:10:36 +00:00
Origin : ptr . Of ( "cscli" ) ,
Scenario : ptr . Of ( "manual 'ban' from '82929df7ee394b73b81252fe3b4e50203yaT2u6nXiaN7Ix9'" ) ,
Scope : ptr . Of ( "Ip" ) ,
Type : ptr . Of ( "ban" ) ,
Value : ptr . Of ( "1.2.3.4" ) ,
2020-11-30 15:15:07 +00:00
} ,
}
2024-01-04 16:10:36 +00:00
// OK decisions
decisionsFilter := DecisionsListOpts { IPEquals : ptr . Of ( "1.2.3.4" ) }
2020-11-30 15:15:07 +00:00
decisions , resp , err := newcli . Decisions . List ( context . Background ( ) , decisionsFilter )
2024-01-04 16:10:36 +00:00
require . NoError ( t , err )
assert . Equal ( t , http . StatusOK , resp . Response . StatusCode )
assert . Equal ( t , * expected , * decisions )
2020-11-30 15:15:07 +00:00
//Empty return
2024-01-04 16:10:36 +00:00
decisionsFilter = DecisionsListOpts { IPEquals : ptr . Of ( "1.2.3.5" ) }
2020-11-30 15:15:07 +00:00
decisions , resp , err = newcli . Decisions . List ( context . Background ( ) , decisionsFilter )
2021-03-17 11:36:47 +00:00
require . NoError ( t , err )
2024-01-04 16:10:36 +00:00
assert . Equal ( t , http . StatusOK , resp . Response . StatusCode )
2023-12-14 13:54:11 +00:00
assert . Empty ( t , * decisions )
2020-11-30 15:15:07 +00:00
}
func TestDecisionsStream ( t * testing . T ) {
log . SetLevel ( log . DebugLevel )
mux , urlx , teardown := setup ( )
defer teardown ( )
mux . HandleFunc ( "/decisions/stream" , func ( w http . ResponseWriter , r * http . Request ) {
2023-12-14 13:54:11 +00:00
assert . Equal ( t , "ixu" , r . Header . Get ( "X-Api-Key" ) )
2022-09-06 11:55:03 +00:00
testMethod ( t , r , http . MethodGet )
if r . Method == http . MethodGet {
2020-11-30 15:15:07 +00:00
if r . URL . RawQuery == "startup=true" {
w . WriteHeader ( http . StatusOK )
2021-01-14 15:27:45 +00:00
w . Write ( [ ] byte ( ` { "deleted":null,"new":[ { "duration":"3h59m55.756182786s","id":4,"origin":"cscli","scenario":"manual 'ban' from '82929df7ee394b73b81252fe3b4e50203yaT2u6nXiaN7Ix9'","scope":"Ip","type":"ban","value":"1.2.3.4"}]} ` ) )
2020-11-30 15:15:07 +00:00
} else {
w . WriteHeader ( http . StatusOK )
w . Write ( [ ] byte ( ` { "deleted":null,"new":null} ` ) )
}
}
} )
2024-01-04 16:10:36 +00:00
2020-11-30 15:15:07 +00:00
mux . HandleFunc ( "/decisions" , func ( w http . ResponseWriter , r * http . Request ) {
2023-12-14 13:54:11 +00:00
assert . Equal ( t , "ixu" , r . Header . Get ( "X-Api-Key" ) )
2022-09-06 11:55:03 +00:00
testMethod ( t , r , http . MethodDelete )
if r . Method == http . MethodDelete {
2020-11-30 15:15:07 +00:00
w . WriteHeader ( http . StatusOK )
}
} )
apiURL , err := url . Parse ( urlx + "/" )
2024-01-04 16:10:36 +00:00
require . NoError ( t , err )
2020-11-30 15:15:07 +00:00
//ok answer
auth := & APIKeyTransport {
APIKey : "ixu" ,
}
newcli , err := NewDefaultClient ( apiURL , "v1" , "toto" , auth . Client ( ) )
2024-01-04 16:10:36 +00:00
require . NoError ( t , err )
2020-11-30 15:15:07 +00:00
expected := & models . DecisionsStreamResponse {
New : models . GetDecisionsResponse {
& models . Decision {
2024-01-04 16:10:36 +00:00
Duration : ptr . Of ( "3h59m55.756182786s" ) ,
2020-11-30 15:15:07 +00:00
ID : 4 ,
2024-01-04 16:10:36 +00:00
Origin : ptr . Of ( "cscli" ) ,
Scenario : ptr . Of ( "manual 'ban' from '82929df7ee394b73b81252fe3b4e50203yaT2u6nXiaN7Ix9'" ) ,
Scope : ptr . Of ( "Ip" ) ,
Type : ptr . Of ( "ban" ) ,
Value : ptr . Of ( "1.2.3.4" ) ,
2020-11-30 15:15:07 +00:00
} ,
} ,
}
2022-03-16 13:37:42 +00:00
decisions , resp , err := newcli . Decisions . GetStream ( context . Background ( ) , DecisionsStreamOpts { Startup : true } )
2021-03-17 11:36:47 +00:00
require . NoError ( t , err )
2024-01-04 16:10:36 +00:00
assert . Equal ( t , http . StatusOK , resp . Response . StatusCode )
assert . Equal ( t , * expected , * decisions )
2020-11-30 15:15:07 +00:00
//and second call, we get empty lists
2022-03-16 13:37:42 +00:00
decisions , resp , err = newcli . Decisions . GetStream ( context . Background ( ) , DecisionsStreamOpts { Startup : false } )
2021-03-17 11:36:47 +00:00
require . NoError ( t , err )
2024-01-04 16:10:36 +00:00
assert . Equal ( t , http . StatusOK , resp . Response . StatusCode )
2023-12-14 13:54:11 +00:00
assert . Empty ( t , decisions . New )
assert . Empty ( t , decisions . Deleted )
2020-11-30 15:15:07 +00:00
//delete stream
resp , err = newcli . Decisions . StopStream ( context . Background ( ) )
2021-03-17 11:36:47 +00:00
require . NoError ( t , err )
2024-01-04 16:10:36 +00:00
assert . Equal ( t , http . StatusOK , resp . Response . StatusCode )
2020-11-30 15:15:07 +00:00
}
2023-02-06 13:06:14 +00:00
func TestDecisionsStreamV3Compatibility ( t * testing . T ) {
log . SetLevel ( log . DebugLevel )
mux , urlx , teardown := setupWithPrefix ( "v3" )
defer teardown ( )
mux . HandleFunc ( "/decisions/stream" , func ( w http . ResponseWriter , r * http . Request ) {
2023-12-14 13:54:11 +00:00
assert . Equal ( t , "ixu" , r . Header . Get ( "X-Api-Key" ) )
2023-02-06 13:06:14 +00:00
testMethod ( t , r , http . MethodGet )
if r . Method == http . MethodGet {
if r . URL . RawQuery == "startup=true" {
w . WriteHeader ( http . StatusOK )
w . Write ( [ ] byte ( ` { "deleted":[ { "scope":"ip","decisions":["1.2.3.5"]}],"new":[ { "scope":"ip", "scenario": "manual 'ban' from '82929df7ee394b73b81252fe3b4e50203yaT2u6nXiaN7Ix9'", "decisions":[ { "duration":"3h59m55.756182786s","value":"1.2.3.4"}]}]} ` ) )
} else {
w . WriteHeader ( http . StatusOK )
w . Write ( [ ] byte ( ` { "deleted":null,"new":null} ` ) )
}
}
} )
apiURL , err := url . Parse ( urlx + "/" )
2024-01-04 16:10:36 +00:00
require . NoError ( t , err )
2023-02-06 13:06:14 +00:00
//ok answer
auth := & APIKeyTransport {
APIKey : "ixu" ,
}
newcli , err := NewDefaultClient ( apiURL , "v3" , "toto" , auth . Client ( ) )
2024-01-04 16:10:36 +00:00
require . NoError ( t , err )
2023-02-06 13:06:14 +00:00
torigin := "CAPI"
tscope := "ip"
ttype := "ban"
expected := & models . DecisionsStreamResponse {
New : models . GetDecisionsResponse {
& models . Decision {
2024-01-04 16:10:36 +00:00
Duration : ptr . Of ( "3h59m55.756182786s" ) ,
2023-02-06 13:06:14 +00:00
Origin : & torigin ,
2024-01-04 16:10:36 +00:00
Scenario : ptr . Of ( "manual 'ban' from '82929df7ee394b73b81252fe3b4e50203yaT2u6nXiaN7Ix9'" ) ,
2023-02-06 13:06:14 +00:00
Scope : & tscope ,
Type : & ttype ,
2024-01-04 16:10:36 +00:00
Value : ptr . Of ( "1.2.3.4" ) ,
2023-02-06 13:06:14 +00:00
} ,
} ,
Deleted : models . GetDecisionsResponse {
& models . Decision {
2024-01-04 16:10:36 +00:00
Duration : ptr . Of ( "1h" ) ,
2023-02-06 13:06:14 +00:00
Origin : & torigin ,
2024-01-04 16:10:36 +00:00
Scenario : ptr . Of ( "deleted" ) ,
2023-02-06 13:06:14 +00:00
Scope : & tscope ,
Type : & ttype ,
2024-01-04 16:10:36 +00:00
Value : ptr . Of ( "1.2.3.5" ) ,
2023-02-06 13:06:14 +00:00
} ,
} ,
}
// GetStream is supposed to consume v3 payload and return v2 response
decisions , resp , err := newcli . Decisions . GetStream ( context . Background ( ) , DecisionsStreamOpts { Startup : true } )
require . NoError ( t , err )
2024-01-04 16:10:36 +00:00
assert . Equal ( t , http . StatusOK , resp . Response . StatusCode )
assert . Equal ( t , * expected , * decisions )
2023-02-06 13:06:14 +00:00
}
func TestDecisionsStreamV3 ( t * testing . T ) {
log . SetLevel ( log . DebugLevel )
mux , urlx , teardown := setupWithPrefix ( "v3" )
defer teardown ( )
mux . HandleFunc ( "/decisions/stream" , func ( w http . ResponseWriter , r * http . Request ) {
2023-12-14 13:54:11 +00:00
assert . Equal ( t , "ixu" , r . Header . Get ( "X-Api-Key" ) )
2023-02-06 13:06:14 +00:00
testMethod ( t , r , http . MethodGet )
if r . Method == http . MethodGet {
w . WriteHeader ( http . StatusOK )
w . Write ( [ ] byte ( ` { "deleted" : [ { "scope" : "ip" , "decisions" : [ "1.2.3.5" ] } ] ,
"new" : [ { "scope" : "ip" , "scenario" : "manual 'ban' from '82929df7ee394b73b81252fe3b4e50203yaT2u6nXiaN7Ix9'" , "decisions" : [ { "duration" : "3h59m55.756182786s" , "value" : "1.2.3.4" } ] } ] ,
"links" : { "blocklists" : [ { "name" : "blocklist1" , "url" : "/v3/blocklist" , "scope" : "ip" , "remediation" : "ban" , "duration" : "24h" } ] } } ` ) )
}
} )
apiURL , err := url . Parse ( urlx + "/" )
2024-01-04 16:10:36 +00:00
require . NoError ( t , err )
2023-02-06 13:06:14 +00:00
//ok answer
auth := & APIKeyTransport {
APIKey : "ixu" ,
}
newcli , err := NewDefaultClient ( apiURL , "v3" , "toto" , auth . Client ( ) )
2024-01-04 16:10:36 +00:00
require . NoError ( t , err )
2023-02-06 13:06:14 +00:00
tscope := "ip"
expected := & modelscapi . GetDecisionsStreamResponse {
New : modelscapi . GetDecisionsStreamResponseNew {
& modelscapi . GetDecisionsStreamResponseNewItem {
Decisions : [ ] * modelscapi . GetDecisionsStreamResponseNewItemDecisionsItems0 {
{
2024-01-04 16:10:36 +00:00
Duration : ptr . Of ( "3h59m55.756182786s" ) ,
Value : ptr . Of ( "1.2.3.4" ) ,
2023-02-06 13:06:14 +00:00
} ,
} ,
2024-01-04 16:10:36 +00:00
Scenario : ptr . Of ( "manual 'ban' from '82929df7ee394b73b81252fe3b4e50203yaT2u6nXiaN7Ix9'" ) ,
2023-02-06 13:06:14 +00:00
Scope : & tscope ,
} ,
} ,
Deleted : modelscapi . GetDecisionsStreamResponseDeleted {
& modelscapi . GetDecisionsStreamResponseDeletedItem {
Scope : & tscope ,
Decisions : [ ] string {
2024-01-04 16:10:36 +00:00
"1.2.3.5" ,
2023-02-06 13:06:14 +00:00
} ,
} ,
} ,
Links : & modelscapi . GetDecisionsStreamResponseLinks {
Blocklists : [ ] * modelscapi . BlocklistLink {
{
2024-01-04 16:10:36 +00:00
Duration : ptr . Of ( "24h" ) ,
Name : ptr . Of ( "blocklist1" ) ,
Remediation : ptr . Of ( "ban" ) ,
Scope : ptr . Of ( "ip" ) ,
URL : ptr . Of ( "/v3/blocklist" ) ,
2023-02-06 13:06:14 +00:00
} ,
} ,
} ,
}
// GetStream is supposed to consume v3 payload and return v2 response
decisions , resp , err := newcli . Decisions . GetStreamV3 ( context . Background ( ) , DecisionsStreamOpts { Startup : true } )
require . NoError ( t , err )
2024-01-04 16:10:36 +00:00
assert . Equal ( t , http . StatusOK , resp . Response . StatusCode )
assert . Equal ( t , * expected , * decisions )
2023-02-06 13:06:14 +00:00
}
func TestDecisionsFromBlocklist ( t * testing . T ) {
log . SetLevel ( log . DebugLevel )
mux , urlx , teardown := setupWithPrefix ( "v3" )
defer teardown ( )
mux . HandleFunc ( "/blocklist" , func ( w http . ResponseWriter , r * http . Request ) {
testMethod ( t , r , http . MethodGet )
2024-01-04 16:10:36 +00:00
2023-02-13 14:06:14 +00:00
if r . Header . Get ( "If-Modified-Since" ) == "Sun, 01 Jan 2023 01:01:01 GMT" {
w . WriteHeader ( http . StatusNotModified )
2024-01-04 16:10:36 +00:00
2023-02-13 14:06:14 +00:00
return
}
2024-01-04 16:10:36 +00:00
2023-02-06 13:06:14 +00:00
if r . Method == http . MethodGet {
w . WriteHeader ( http . StatusOK )
w . Write ( [ ] byte ( "1.2.3.4\r\n1.2.3.5" ) )
}
} )
apiURL , err := url . Parse ( urlx + "/" )
2024-01-04 16:10:36 +00:00
require . NoError ( t , err )
2023-02-06 13:06:14 +00:00
//ok answer
auth := & APIKeyTransport {
APIKey : "ixu" ,
}
newcli , err := NewDefaultClient ( apiURL , "v3" , "toto" , auth . Client ( ) )
2024-01-04 16:10:36 +00:00
require . NoError ( t , err )
2023-02-06 13:06:14 +00:00
tdurationBlocklist := "24h"
tnameBlocklist := "blocklist1"
tremediationBlocklist := "ban"
tscopeBlocklist := "ip"
2023-02-13 14:06:14 +00:00
turlBlocklist := urlx + "/v3/blocklist"
2023-02-06 13:06:14 +00:00
torigin := "lists"
expected := [ ] * models . Decision {
{
Duration : & tdurationBlocklist ,
2024-01-04 16:10:36 +00:00
Value : ptr . Of ( "1.2.3.4" ) ,
2023-02-06 13:06:14 +00:00
Scenario : & tnameBlocklist ,
Scope : & tscopeBlocklist ,
Type : & tremediationBlocklist ,
Origin : & torigin ,
} ,
{
Duration : & tdurationBlocklist ,
2024-01-04 16:10:36 +00:00
Value : ptr . Of ( "1.2.3.5" ) ,
2023-02-06 13:06:14 +00:00
Scenario : & tnameBlocklist ,
Scope : & tscopeBlocklist ,
Type : & tremediationBlocklist ,
Origin : & torigin ,
} ,
}
2023-02-13 14:06:14 +00:00
decisions , isModified , err := newcli . Decisions . GetDecisionsFromBlocklist ( context . Background ( ) , & modelscapi . BlocklistLink {
2023-02-06 13:06:14 +00:00
URL : & turlBlocklist ,
Scope : & tscopeBlocklist ,
Remediation : & tremediationBlocklist ,
Name : & tnameBlocklist ,
Duration : & tdurationBlocklist ,
2023-02-13 14:06:14 +00:00
} , nil )
2023-02-06 13:06:14 +00:00
require . NoError ( t , err )
2023-02-13 14:06:14 +00:00
assert . True ( t , isModified )
2023-02-06 13:06:14 +00:00
log . Infof ( "decision1: %+v" , decisions [ 0 ] )
log . Infof ( "expected1: %+v" , expected [ 0 ] )
log . Infof ( "decisions: %s, %s, %s, %s, %s, %s" , * decisions [ 0 ] . Value , * decisions [ 0 ] . Duration , * decisions [ 0 ] . Scenario , * decisions [ 0 ] . Scope , * decisions [ 0 ] . Type , * decisions [ 0 ] . Origin )
log . Infof ( "expected : %s, %s, %s, %s, %s" , * expected [ 0 ] . Value , * expected [ 0 ] . Duration , * expected [ 0 ] . Scenario , * expected [ 0 ] . Scope , * expected [ 0 ] . Type )
log . Infof ( "decisions: %s, %s, %s, %s, %s" , * decisions [ 1 ] . Value , * decisions [ 1 ] . Duration , * decisions [ 1 ] . Scenario , * decisions [ 1 ] . Scope , * decisions [ 1 ] . Type )
2024-01-04 16:10:36 +00:00
assert . Equal ( t , expected , decisions )
2023-02-13 14:06:14 +00:00
// test cache control
_ , isModified , err = newcli . Decisions . GetDecisionsFromBlocklist ( context . Background ( ) , & modelscapi . BlocklistLink {
URL : & turlBlocklist ,
Scope : & tscopeBlocklist ,
Remediation : & tremediationBlocklist ,
Name : & tnameBlocklist ,
Duration : & tdurationBlocklist ,
2023-05-25 08:08:52 +00:00
} , ptr . Of ( "Sun, 01 Jan 2023 01:01:01 GMT" ) )
2024-01-04 16:10:36 +00:00
2023-02-13 14:06:14 +00:00
require . NoError ( t , err )
assert . False ( t , isModified )
2024-01-04 16:10:36 +00:00
2023-02-13 14:06:14 +00:00
_ , isModified , err = newcli . Decisions . GetDecisionsFromBlocklist ( context . Background ( ) , & modelscapi . BlocklistLink {
URL : & turlBlocklist ,
Scope : & tscopeBlocklist ,
Remediation : & tremediationBlocklist ,
Name : & tnameBlocklist ,
Duration : & tdurationBlocklist ,
2023-05-25 08:08:52 +00:00
} , ptr . Of ( "Mon, 02 Jan 2023 01:01:01 GMT" ) )
2024-01-04 16:10:36 +00:00
2023-02-13 14:06:14 +00:00
require . NoError ( t , err )
assert . True ( t , isModified )
2023-02-06 13:06:14 +00:00
}
2020-11-30 15:15:07 +00:00
func TestDeleteDecisions ( t * testing . T ) {
mux , urlx , teardown := setup ( )
mux . HandleFunc ( "/watchers/login" , func ( w http . ResponseWriter , r * http . Request ) {
w . WriteHeader ( http . StatusOK )
w . Write ( [ ] byte ( ` { "code": 200, "expire": "2030-01-02T15:04:05Z", "token": "oklol"} ` ) )
} )
2024-01-04 16:10:36 +00:00
2020-11-30 15:15:07 +00:00
mux . HandleFunc ( "/decisions" , func ( w http . ResponseWriter , r * http . Request ) {
testMethod ( t , r , "DELETE" )
2023-12-14 13:54:11 +00:00
assert . Equal ( t , "ip=1.2.3.4" , r . URL . RawQuery )
2020-11-30 15:15:07 +00:00
w . WriteHeader ( http . StatusOK )
w . Write ( [ ] byte ( ` { "nbDeleted":"1"} ` ) )
//w.Write([]byte(`{"message":"0 deleted alerts"}`))
} )
2024-01-04 16:10:36 +00:00
2020-11-30 15:15:07 +00:00
log . Printf ( "URL is %s" , urlx )
2024-01-04 16:10:36 +00:00
2020-11-30 15:15:07 +00:00
apiURL , err := url . Parse ( urlx + "/" )
2024-01-04 16:10:36 +00:00
require . NoError ( t , err )
2020-11-30 15:15:07 +00:00
client , err := NewClient ( & Config {
MachineID : "test_login" ,
Password : "test_password" ,
2023-05-23 08:52:47 +00:00
UserAgent : fmt . Sprintf ( "crowdsec/%s" , version . String ( ) ) ,
2020-11-30 15:15:07 +00:00
URL : apiURL ,
VersionPrefix : "v1" ,
} )
2024-01-04 16:10:36 +00:00
require . NoError ( t , err )
2020-11-30 15:15:07 +00:00
filters := DecisionsDeleteOpts { IPEquals : new ( string ) }
* filters . IPEquals = "1.2.3.4"
2023-12-14 13:54:11 +00:00
2024-01-04 16:10:36 +00:00
deleted , _ , err := client . Decisions . Delete ( context . Background ( ) , filters )
require . NoError ( t , err )
2020-11-30 15:15:07 +00:00
assert . Equal ( t , "1" , deleted . NbDeleted )
defer teardown ( )
}
2022-03-16 13:37:42 +00:00
func TestDecisionsStreamOpts_addQueryParamsToURL ( t * testing . T ) {
baseURLString := "http://localhost:8080/v1/decisions/stream"
2023-12-14 13:54:11 +00:00
2022-03-16 13:37:42 +00:00
type fields struct {
Startup bool
Scopes string
ScenariosContaining string
ScenariosNotContaining string
}
2024-01-04 16:10:36 +00:00
2022-03-16 13:37:42 +00:00
tests := [ ] struct {
2024-01-04 16:10:36 +00:00
name string
fields fields
expected string
expectedErr string
2022-03-16 13:37:42 +00:00
} {
{
2024-01-04 16:10:36 +00:00
name : "no filter" ,
expected : baseURLString + "?" ,
2022-03-16 13:37:42 +00:00
} ,
{
name : "startup=true" ,
fields : fields {
Startup : true ,
} ,
2024-01-04 16:10:36 +00:00
expected : baseURLString + "?startup=true" ,
2022-03-16 13:37:42 +00:00
} ,
{
name : "set all params" ,
fields : fields {
Startup : true ,
Scopes : "ip,range" ,
ScenariosContaining : "ssh" ,
ScenariosNotContaining : "bf" ,
} ,
2024-01-04 16:10:36 +00:00
expected : baseURLString + "?scenarios_containing=ssh&scenarios_not_containing=bf&scopes=ip%2Crange&startup=true" ,
2022-03-16 13:37:42 +00:00
} ,
}
2023-12-14 13:54:11 +00:00
2022-03-16 13:37:42 +00:00
for _ , tt := range tests {
2022-10-10 08:48:26 +00:00
tt := tt
2022-03-16 13:37:42 +00:00
t . Run ( tt . name , func ( t * testing . T ) {
o := & DecisionsStreamOpts {
Startup : tt . fields . Startup ,
Scopes : tt . fields . Scopes ,
ScenariosContaining : tt . fields . ScenariosContaining ,
ScenariosNotContaining : tt . fields . ScenariosNotContaining ,
}
2024-01-04 16:10:36 +00:00
2022-03-16 13:37:42 +00:00
got , err := o . addQueryParamsToURL ( baseURLString )
2024-01-04 16:10:36 +00:00
cstest . RequireErrorContains ( t , err , tt . expectedErr )
if tt . expectedErr != "" {
2022-03-16 13:37:42 +00:00
return
}
gotURL , err := url . Parse ( got )
2024-01-04 16:10:36 +00:00
require . NoError ( t , err )
2022-03-16 13:37:42 +00:00
2024-01-04 16:10:36 +00:00
expectedURL , err := url . Parse ( tt . expected )
require . NoError ( t , err )
2022-03-16 13:37:42 +00:00
2024-01-04 16:10:36 +00:00
assert . Equal ( t , * expectedURL , * gotURL )
2022-03-16 13:37:42 +00:00
} )
}
}
2020-11-30 15:15:07 +00:00
// func TestDeleteOneDecision(t *testing.T) {
// mux, urlx, teardown := setup()
// mux.HandleFunc("/watchers/login", func(w http.ResponseWriter, r *http.Request) {
// w.WriteHeader(http.StatusOK)
// w.Write([]byte(`{"code": 200, "expire": "2030-01-02T15:04:05Z", "token": "oklol"}`))
// })
// mux.HandleFunc("/decisions/1", func(w http.ResponseWriter, r *http.Request) {
// testMethod(t, r, "DELETE")
// w.WriteHeader(http.StatusOK)
// w.Write([]byte(`{"nbDeleted":"1"}`))
// })
// log.Printf("URL is %s", urlx)
// apiURL, err := url.Parse(urlx + "/")
// if err != nil {
2022-10-13 08:42:46 +00:00
// t.Fatalf("parsing api url: %s", apiURL)
2020-11-30 15:15:07 +00:00
// }
// client, err := NewClient(&Config{
// MachineID: "test_login",
// Password: "test_password",
// UserAgent: fmt.Sprintf("crowdsec/%s", cwversion.VersionStr()),
// URL: apiURL,
// VersionPrefix: "v1",
// })
// if err != nil {
2022-11-29 08:16:07 +00:00
// t.Fatalf("new api client: %s", err)
2020-11-30 15:15:07 +00:00
// }
// filters := DecisionsDeleteOpts{IPEquals: new(string)}
// *filters.IPEquals = "1.2.3.4"
// deleted, _, err := client.Decisions.Delete(context.Background(), filters)
// if err != nil {
// t.Fatalf("unexpected err : %s", err)
// }
// assert.Equal(t, "1", deleted.NbDeleted)
// defer teardown()
// }