diff --git a/fedilogue/retrieve.go b/fedilogue/retrieve.go index 6594945..a452aea 100644 --- a/fedilogue/retrieve.go +++ b/fedilogue/retrieve.go @@ -162,16 +162,22 @@ func check_activity(uri string) { } -func check_actor(uri string) { +/* Test: TestCheck_actor */ +func check_actor(uri string) int { var actorjson ActorJson + + if len(uri) <= 7 { + return 400 // Bad actor + } + endslash := strings.Index(uri[8:], "/") if endslash == -1 { - return + return 400 // Bad actor } actorjson.instance = uri[8 : endslash+8] for _, banned := range settings.Banned { if strings.Index(uri, "https://"+banned+"/") == 0 { - return + return 401 // Banned actor } } @@ -180,14 +186,14 @@ func check_actor(uri string) { o.recentactors.Mu.Lock() if o.recentactors.Add(uri) == true { o.recentactors.Mu.Unlock() - return + return 402 // Actor in recent queue, good! } o.recentactors.Mu.Unlock() selectRet := pool.QueryRow(context.Background(), "SELECT FROM actors WHERE document->>'id' = $1", uri) err := selectRet.Scan() if err == nil { - return + return 403 // Actor already in database, good! } req, _ := http.NewRequest("GET", uri, nil) @@ -201,7 +207,7 @@ func check_actor(uri string) { if err != nil { if tries > 10 { logErr("Unable to connect to "+uri+" attempt 10/10, giving up.") - return + return 404 // Unable to connect to host after 10 attempts } logWarn("Unable to connect to "+uri+", attempt ",tries+1,"+/10 sleeping for 30 seconds.") time.Sleep(time.Second * 30) @@ -213,7 +219,7 @@ func check_actor(uri string) { body, err := ioutil.ReadAll(resp.Body) if err != nil { - return + return 405 // Unable to read body of message } resp.Body.Close() @@ -221,13 +227,14 @@ func check_actor(uri string) { err = json.Unmarshal(body, &actorjson) if err != nil { - return + return 406 // Unable to unmarshal body of message } _, err = pool.Exec(context.Background(), "INSERT INTO actors (document, instance) VALUES($1, $2)", jsondocument, actorjson.instance) if err != nil { logWarn("Error inserting %s into `actors`: "+uri, err) - return + return 407 // Unable to insert actor } + return 0 // Successful } diff --git a/fedilogue/retrieve_test.go b/fedilogue/retrieve_test.go new file mode 100644 index 0000000..bdeb8a9 --- /dev/null +++ b/fedilogue/retrieve_test.go @@ -0,0 +1,37 @@ +package main + +import ( + "testing" +) + +func TestCheck_actor(t *testing.T) { + defer func() { + }() + + // Start of Setup + settings.Banned = append(settings.Banned, "banneddomain.com") + o, _ := GetRunner("validdomain.com") + + q := o.recentactors.Add("https://validdomain.com/users/validuser") + AssertEqual(t, q, false) // A setup test + // End of Setup + + // initialize haves + have1 := check_actor("meaninglessvalue") + have0 := check_actor("") + have2 := check_actor("https://banneddomain.com/users/banneduser") + have3 := check_actor("https://validdomain.com/users/validuser") + + // test wants + // Short user + AssertEqual(t, have0, 400) + + // Invalid user + AssertEqual(t, have1, 400) + + // Banned instance + AssertEqual(t, have2, 401) + + // User already present + AssertEqual(t, have3, 402) +}