fix error tests
This commit is contained in:
parent
a611486a08
commit
3bfec2211f
2 changed files with 21 additions and 8 deletions
18
profile.go
18
profile.go
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -44,6 +45,7 @@ type user struct {
|
||||||
Result struct {
|
Result struct {
|
||||||
RestID string `json:"rest_id"`
|
RestID string `json:"rest_id"`
|
||||||
Legacy legacyUser `json:"legacy"`
|
Legacy legacyUser `json:"legacy"`
|
||||||
|
Message string `json:"message"`
|
||||||
} `json:"result"`
|
} `json:"result"`
|
||||||
} `json:"user"`
|
} `json:"user"`
|
||||||
} `json:"data"`
|
} `json:"data"`
|
||||||
|
|
@ -91,11 +93,17 @@ func (s *Scraper) GetProfile(username string) (Profile, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(jsn.Errors) > 0 && jsn.Data.User.Result.RestID == "" {
|
if len(jsn.Errors) > 0 && jsn.Data.User.Result.RestID == "" {
|
||||||
|
if strings.Contains(jsn.Errors[0].Message, "Missing LdapGroup(visibility-custom-suspension)") {
|
||||||
|
return Profile{}, fmt.Errorf("user is suspended")
|
||||||
|
}
|
||||||
return Profile{}, fmt.Errorf("%s", jsn.Errors[0].Message)
|
return Profile{}, fmt.Errorf("%s", jsn.Errors[0].Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
if jsn.Data.User.Result.RestID == "" {
|
if jsn.Data.User.Result.RestID == "" {
|
||||||
return Profile{}, fmt.Errorf("rest_id not found")
|
if jsn.Data.User.Result.Message == "User is suspended" {
|
||||||
|
return Profile{}, fmt.Errorf("user is suspended")
|
||||||
|
}
|
||||||
|
return Profile{}, fmt.Errorf("user not found")
|
||||||
}
|
}
|
||||||
jsn.Data.User.Result.Legacy.IDStr = jsn.Data.User.Result.RestID
|
jsn.Data.User.Result.Legacy.IDStr = jsn.Data.User.Result.RestID
|
||||||
|
|
||||||
|
|
@ -142,11 +150,17 @@ func (s *Scraper) GetProfileByID(userID string) (Profile, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(jsn.Errors) > 0 && jsn.Data.User.Result.RestID == "" {
|
if len(jsn.Errors) > 0 && jsn.Data.User.Result.RestID == "" {
|
||||||
|
if strings.Contains(jsn.Errors[0].Message, "Missing LdapGroup(visibility-custom-suspension)") {
|
||||||
|
return Profile{}, fmt.Errorf("user is suspended")
|
||||||
|
}
|
||||||
return Profile{}, fmt.Errorf("%s", jsn.Errors[0].Message)
|
return Profile{}, fmt.Errorf("%s", jsn.Errors[0].Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
if jsn.Data.User.Result.RestID == "" {
|
if jsn.Data.User.Result.RestID == "" {
|
||||||
return Profile{}, fmt.Errorf("rest_id not found")
|
if jsn.Data.User.Result.Message == "User is suspended" {
|
||||||
|
return Profile{}, fmt.Errorf("user is suspended")
|
||||||
|
}
|
||||||
|
return Profile{}, fmt.Errorf("user not found")
|
||||||
}
|
}
|
||||||
jsn.Data.User.Result.Legacy.IDStr = jsn.Data.User.Result.RestID
|
jsn.Data.User.Result.Legacy.IDStr = jsn.Data.User.Result.RestID
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package twitterscraper_test
|
package twitterscraper_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -116,15 +115,15 @@ func TestGetProfileErrorSuspended(t *testing.T) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("Expected Error, got success")
|
t.Error("Expected Error, got success")
|
||||||
} else {
|
} else {
|
||||||
if !strings.Contains(err.Error(), "Missing LdapGroup(visibility-custom-suspension)") {
|
if !strings.Contains(err.Error(), "suspended") {
|
||||||
t.Error("Expected error to contain 'Missing LdapGroup(visibility-custom-suspension)', got", err)
|
t.Error("Expected error to contain 'suspended', got", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetProfileErrorNotFound(t *testing.T) {
|
func TestGetProfileErrorNotFound(t *testing.T) {
|
||||||
neUser := "sample3123131"
|
neUser := "sample3123131"
|
||||||
expectedError := fmt.Sprintf("User '%s' not found", neUser)
|
expectedError := "user not found"
|
||||||
_, err := testScraper.GetProfile(neUser)
|
_, err := testScraper.GetProfile(neUser)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("Expected Error, got success")
|
t.Error("Expected Error, got success")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue