// Function to mock a domain object func newDomain() model.Domain { var domain model.Domain domain.FQDN = "rafael.net.br" domain.Nameservers = []model.Nameserver{ { Host: "ns1.rafael.net.br", IPv4: net.ParseIP("127.0.0.1"), IPv6: net.ParseIP("::1"), }, { Host: "ns2.rafael.net.br", IPv4: net.ParseIP("127.0.0.2"), }, } domain.DSSet = []model.DS{ { Keytag: 1234, Algorithm: model.DSAlgorithmRSASHA1, Digest: "A790A11EA430A85DA77245F091891F73AA740483", }, } owner, _ := mail.ParseAddress("*****@*****.**") domain.Owners = []model.Owner{ { Email: owner, Language: "pt-BR", }, } return domain }
func TestToDomainResponse(t *testing.T) { email, err := mail.ParseAddress("*****@*****.**") if err != nil { t.Fatal(err) } domain := model.Domain{ FQDN: "xn--exmpl-4qa6c.com.br.", Nameservers: []model.Nameserver{ { Host: "ns1.example.com.br.", LastStatus: model.NameserverStatusTimeout, }, }, DSSet: []model.DS{ { Keytag: 41674, Algorithm: 5, Digest: "EAA0978F38879DB70A53F9FF1ACF21D046A98B5C", DigestType: 2, LastStatus: model.DSStatusTimeout, }, }, Owners: []model.Owner{ { Email: email, Language: fmt.Sprintf("%s-%s", model.LanguageTypePT, model.RegionTypeBR), }, }, } domainResponse := ToDomainResponse(domain, true) if domainResponse.FQDN != "exâmplé.com.br." { t.Error("Fail to convert FQDN") } if len(domainResponse.Nameservers) != 1 { t.Error("Fail to convert nameservers") } if len(domainResponse.DSSet) != 1 { t.Error("Fail to convert the DS set") } if len(domainResponse.Owners) != 1 || domainResponse.Owners[0].Email != "*****@*****.**" || domainResponse.Owners[0].Language != "pt-BR" { t.Error("Fail to convert owners") } if len(domainResponse.Links) != 1 { t.Error("Wrong number of links") } domainResponse = ToDomainResponse(domain, false) if len(domainResponse.Links) != 0 { t.Error("Shouldn't return links when the object doesn't exist in the system") } // Testing case problem domain.FQDN = "XN--EXMPL-4QA6C.COM.BR." domainResponse = ToDomainResponse(domain, false) if domainResponse.FQDN != "exâmplé.com.br." { t.Errorf("Should convert to unicode even in upper case. "+ "Expected '%s' and got '%s'", "exâmplé.com.br.", domainResponse.FQDN) } // Testing an invalid FQDN ACE format domain.FQDN = "xn--x1x2x3x4x5.com.br." domainResponse = ToDomainResponse(domain, false) if domainResponse.FQDN != "xn--x1x2x3x4x5.com.br." { t.Errorf("Should keep the ACE format when there's an error converting to unicode. "+ "Expected '%s' and got '%s'", "xn--x1x2x3x4x5.com.br.", domainResponse.FQDN) } }
// Merge is used to merge a domain request object sent by the user into a domain object of // the database. It can return errors related to merge problems that are problem caused by // data format of the user input func Merge(domain model.Domain, domainRequest DomainRequest) (model.Domain, error) { var err error if domainRequest.FQDN, err = model.NormalizeDomainName(domainRequest.FQDN); err != nil { return domain, err } // Detect when the domain object is empty, that is the case when we are creating a new // domain in the Shelter project if len(domain.FQDN) == 0 { domain.FQDN = domainRequest.FQDN } else { // Cannot merge domains with different FQDNs if domain.FQDN != domainRequest.FQDN { return domain, ErrDomainsFQDNDontMatch } } nameservers, err := toNameserversModel(domainRequest.Nameservers) if err != nil { return domain, err } for index, userNameserver := range nameservers { for _, nameserver := range domain.Nameservers { if nameserver.Host == userNameserver.Host { // Found the same nameserver in the user domain object, maybe the user updated the // IP addresses nameserver.IPv4 = userNameserver.IPv4 nameserver.IPv6 = userNameserver.IPv6 nameservers[index] = nameserver break } } } domain.Nameservers = nameservers dsSet, err := toDSSetModel(domainRequest.DSSet) if err != nil { return domain, err } dnskeysDSSet, err := dnskeysRequestsToDSSetModel(domain.FQDN, domainRequest.DNSKEYS) if err != nil { return domain, err } dsSet = append(dsSet, dnskeysDSSet...) for index, userDS := range dsSet { for _, ds := range domain.DSSet { if ds.Keytag == userDS.Keytag { // Found the same DS in the user domain object ds.Algorithm = userDS.Algorithm ds.Digest = userDS.Digest ds.DigestType = userDS.DigestType dsSet[index] = ds break } } } domain.DSSet = dsSet // We can replace the whole structure of the e-mail every time that a new UPDATE arrives // because there's no extra information in server side that we need to keep domain.Owners, err = toOwnersModel(domainRequest.Owners) if err != nil { return domain, err } return domain, nil }