Skip to content

Commit

Permalink
Add unit test for contact apis
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-shruti-s committed Mar 7, 2022
1 parent 4d26d52 commit fb4af68
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 10 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
- name: Run API test
run: |
go test jobs
go test contact
env:
DB_HOST: localhost
DB_NAME: website_admin_test
Expand Down
18 changes: 9 additions & 9 deletions contact/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ const (
)

type ContactDetails struct {
Name string `json:"name"`
Designation string `json:"designation"`
DesignationInfo string `json:"designation_info"`
SocialMediaLinks map[string]string `json:"social_media_links"`
Idea string `json:"idea"`
Reason string `json:"reason"`
Email string `json:"email"`
Message string `json:"message"`
ContactType string `json:"contact_type"`
Name string `json:"name"`
Designation string `json:"designation"`
DesignationInfo string `json:"designation_info"`
SocialMediaLinks map[string]interface{} `json:"social_media_links"`
Idea string `json:"idea"`
Reason string `json:"reason"`
Email string `json:"email"`
Message string `json:"message"`
ContactType string `json:"contact_type"`
}

type Template struct {
Expand Down
125 changes: 125 additions & 0 deletions contact/contact_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package contact

import (
"bytes"
"embed"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"utils"

"github.com/gin-gonic/gin"
"github.com/tj/assert"
)

//go:embed templates/contact-email-template.html
var templateFS embed.FS

var repo *Template
var err error
var testRequests []utils.TestRequest

const (
SEND_CONTACT_MAIL = "/api/send-contact-mail"
)

var contactDetailInput = ContactDetails{
Name: "shruti",
Designation: "I am individual entrepreneur running my own business.",
DesignationInfo: "I am an owner of the business and I run canopas business. For more information about my business, you can visit the following links.",
SocialMediaLinks: map[string]interface{}{
"website": "https://canopas.com/",
"facebook": "https://www.facebook.com/canopassoftware",
"twitter": "https://twitter.com/canopassoftware",
"instagram": "https://www.instagram.com/canopassoftware/",
},
Idea: "I have an idea for my business that I want to implement with your help.",
Reason: "I have a rough design for the product I want to develop.",
Email: "[email protected]",
Message: "i'm very interested work with this company",
ContactType: "Chat or Email",
}

func Test_init(t *testing.T) {
repo, err = initializeRepo()
assert.Nil(t, err)
testRequests = []utils.TestRequest{
{
Url: SEND_CONTACT_MAIL,
Method: "POST",
Headers: nil,
Body: contactDetailInput,
ResponseCode: http.StatusOK,
ResponseTypeArray: false,
ExpectedData: expectedContactData(),
},
}
}

func TestAllAPIs(t *testing.T) {

asserts := assert.New(t)
engine := gin.New()

setUpRouter(engine)

for _, testData := range testRequests {

w := httptest.NewRecorder()
var req *http.Request
var got interface{}

if testData.Body != nil {
requestByte, _ := json.Marshal(testData.Body)
reqBodyData := bytes.NewReader(requestByte)
req, err = http.NewRequest(testData.Method, testData.Url, reqBodyData)
} else {
req, err = http.NewRequest(testData.Method, testData.Url, nil)
}

asserts.NoError(err)

engine.ServeHTTP(w, req)
assert.Equal(t, testData.ResponseCode, w.Code)
if testData.ResponseTypeArray {
got = utils.GotArrayData(w, t)
} else {
got = utils.GotData(w, t)
}

assert.Equal(t, testData.ExpectedData, got)

}
}

func initializeRepo() (*Template, error) {

repo = New(templateFS)

return repo, err
}

// configure api you want to test
func setUpRouter(engine *gin.Engine) {
engine.POST(SEND_CONTACT_MAIL, repo.SendContactMail)
}

func expectedContactData() map[string]interface{} {
contact := make(map[string]interface{})
contact["name"] = "shruti"
contact["designation"] = "I am individual entrepreneur running my own business."
contact["designation_info"] = "I am an owner of the business and I run canopas business. For more information about my business, you can visit the following links."
contact["social_media_links"] = map[string]interface{}{
"facebook": "https://www.facebook.com/canopassoftware",
"instagram": "https://www.instagram.com/canopassoftware/",
"twitter": "https://twitter.com/canopassoftware",
"website": "https://canopas.com/",
}
contact["idea"] = "I have an idea for my business that I want to implement with your help."
contact["reason"] = "I have a rough design for the product I want to develop."
contact["email"] = "[email protected]"
contact["message"] = "i'm very interested work with this company"
contact["contact_type"] = "Chat or Email"
return contact
}
10 changes: 10 additions & 0 deletions contact/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,37 @@ go 1.17

replace utils => ../utils

replace db => ../db

require (
db v0.0.0 // indirect
github.com/aws/aws-sdk-go v1.43.5
github.com/gin-gonic/gin v1.7.7
github.com/sirupsen/logrus v1.8.1
github.com/tj/assert v0.0.3
utils v0.0.0-00010101000000-000000000000
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/golang/protobuf v1.3.3 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jmoiron/sqlx v1.3.4 // indirect
github.com/json-iterator/go v1.1.9 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.6.1 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c // indirect
)
18 changes: 17 additions & 1 deletion contact/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,28 @@ github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD87
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/jmoiron/sqlx v1.3.4 h1:wv+0IJZfL5z0uZoUjlpKgHkgaFSYD+r9CfrXjEXsO7w=
github.com/jmoiron/sqlx v1.3.4/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
Expand All @@ -40,8 +49,11 @@ github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tj/assert v0.0.3 h1:Df/BlaZ20mq6kuai7f5z2TvPFiwC3xaWJSDQNiIS3Rk=
github.com/tj/assert v0.0.3/go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pvk=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
Expand All @@ -65,6 +77,10 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/guregu/null.v3 v3.5.0/go.mod h1:E4tX2Qe3h7QdL+uZ3a0vqvYwKQsRSQKM5V4YltdgH9Y=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c h1:grhR+C34yXImVGp7EzNk+DTIk+323eIUWOmEevy6bDo=
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
40 changes: 40 additions & 0 deletions contact/templates/contact-email-template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="HandheldFriendly" content="true" />
</head>
<body>
<div class="review-application-div">
<p>
<span>Hello, My name is </span
><span class="title-span"><b>{{.Name}}</b></span>
</p>
<p>
<span class="apply-title">{{.Designation}}</span>
</p>
<p>
<span>{{.DesignationInfo}}</span>
</p>
<p>
Website : {{.SocialMediaLinks.website}} <br />
Facebook : {{.SocialMediaLinks.facebook}} <br />
Twitter : {{.SocialMediaLinks.twitter}} <br />
Instagram : {{.SocialMediaLinks.instagram}} <br />
Other : {{.SocialMediaLinks.other}} <br />
</p>
<p>
<span><b>Here are my preferences:</b></span> <br />
<span class="title-span"> {{.Idea}}</span><br />
<span class="title-span"> {{.Reason}}</span>
</p>

<p>
<span><i>Email : </i></span><span class="title-span"> {{.Email}}</span>
</p>
<p><span class="title-span">Description : {{.Message}}</span></p>
</div>
</body>
</html>

0 comments on commit fb4af68

Please sign in to comment.