Skip to content

Commit

Permalink
add first the and move error handling some domain
Browse files Browse the repository at this point in the history
  • Loading branch information
leonlatsch committed Apr 8, 2024
1 parent ea34496 commit 8541a1e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
6 changes: 4 additions & 2 deletions internal/service/godaddy_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ type GodaddyService struct {
LastIp string
}

func (self *GodaddyService) PrintDomainDetail() {
func (self *GodaddyService) PrintDomainDetail() error {
domainDetail, err := self.GodaddyApi.GetDomainDetail()
if err != nil {
log.Println("Could not load domain detail for " + self.Config.Domain)
log.Fatalln(err)
return err
}

log.Println(
Expand All @@ -34,6 +34,8 @@ func (self *GodaddyService) PrintDomainDetail() {
domainDetail.ContactAdmin.Email,
),
)

return nil
}

func (self *GodaddyService) ObserveAndUpdateDns() {
Expand Down
48 changes: 48 additions & 0 deletions internal/service/gogaddy_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package service_test

import (
"encoding/json"
"errors"
"testing"

"github.com/leonlatsch/go-resolve/internal/api"
"github.com/leonlatsch/go-resolve/internal/http"
"github.com/leonlatsch/go-resolve/internal/models"
"github.com/leonlatsch/go-resolve/internal/service"
)

func TestPrintDomainDetails(t *testing.T) {
fakeHttpClient := http.FakeHttpClient{}

service := service.GodaddyService{
GodaddyApi: api.GodaddyApi{
HttpClient: &fakeHttpClient,
},
}

t.Run("Get domain details does not crash with correct json response", func(t *testing.T) {
fakeDomainDetail := models.DomainDetail{
Domain: "somedomain.com",
ContactAdmin: models.DomainContact{
Email: "someemail@asdf.com",
FirstName: "FirstName",
LastName: "LastName",
},
}

fakeJson, _ := json.Marshal(fakeDomainDetail)
fakeHttpClient.RespBody = string(fakeJson)

if err := service.PrintDomainDetail(); err != nil {
t.Fatal(err)
}
})

t.Run("Get domain details crash if http returns an error", func(t *testing.T) {
fakeHttpClient.Error = errors.New("Some http error")

if err := service.PrintDomainDetail(); err == nil {
t.Fatal("Was expected to return error but did not")
}
})
}
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func main() {
IpApi: api.IpApi{HttpClient: &httpClient},
}

godaddyService.PrintDomainDetail()
if err := godaddyService.PrintDomainDetail(); err != nil {
log.Fatalln(err)
}
godaddyService.ObserveAndUpdateDns()
}

0 comments on commit 8541a1e

Please sign in to comment.