-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
609d4dd
commit 12b9df0
Showing
7 changed files
with
192 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package sitemap | ||
|
||
import ( | ||
"bytes" | ||
"embed" | ||
"encoding/json" | ||
"encoding/xml" | ||
"jobs" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"utils" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/jmoiron/sqlx" | ||
"github.com/tj/assert" | ||
) | ||
|
||
//go:embed templates/career-email-template.html | ||
var templateFS embed.FS | ||
|
||
var repo *SitemapRepository | ||
var careerRepo *jobs.CareerRepository | ||
var err error | ||
var testDB *sqlx.DB | ||
var testRequests []utils.TestRequest | ||
|
||
const ( | ||
GET_SITEMAP_API_URL = "/sitemap?baseUrl=http://localhost:8080" | ||
) | ||
|
||
func Test_init(t *testing.T) { | ||
repo, err = initializeRepo() | ||
assert.Nil(t, err) | ||
testRequests = []utils.TestRequest{ | ||
{ | ||
Url: GET_SITEMAP_API_URL, | ||
Method: "GET", | ||
Headers: nil, | ||
Body: nil, | ||
ResponseCode: http.StatusOK, | ||
ResponseTypeArray: false, | ||
ExpectedData: expectedSitemapData(), | ||
}, | ||
} | ||
} | ||
|
||
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 | ||
|
||
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) | ||
|
||
var urlset URLset | ||
|
||
err = xml.Unmarshal(w.Body.Bytes(), &urlset) | ||
|
||
assert.Equal(t, testData.ExpectedData, urlset) | ||
|
||
} | ||
} | ||
|
||
func initializeRepo() (*SitemapRepository, error) { | ||
|
||
testDB, err = utils.TestDB() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
careerRepo = jobs.New(testDB, templateFS) | ||
repo = New(careerRepo) | ||
|
||
return repo, err | ||
} | ||
|
||
func setUpRouter(engine *gin.Engine) { | ||
engine.GET("/sitemap", repo.GenerateSitemap) | ||
} | ||
|
||
func expectedSitemapData() URLset { | ||
sitemap := URLset{} | ||
|
||
sitemap.XMLName.Space = "http://www.sitemaps.org/schemas/sitemap/0.9" | ||
sitemap.XMLName.Local = "urlset" | ||
sitemap.XMLNS = "http://www.sitemaps.org/schemas/sitemap/0.9" | ||
sitemap.URL = append(sitemap.URL, expectedURLData()...) | ||
|
||
return sitemap | ||
} | ||
|
||
func expectedURLData() []URL { | ||
|
||
sitemapUrls := []URL{ | ||
{Loc: "http://localhost:8080", Priority: `1`}, | ||
{Loc: "http://localhost:8080/jobs", Priority: `1`}, | ||
{Loc: "http://localhost:8080/contact", Priority: `0.9`}, | ||
{Loc: "https://blog.canopas.com", Priority: `0.8`}, | ||
{Loc: "http://localhost:8080/jobs/ios-developer-a9b45f34-a1a5-419f-b536-b7c290925d6d", Priority: `0.9`}, | ||
{Loc: "http://localhost:8080/jobs/apply/ios-developer-a9b45f34-a1a5-419f-b536-b7c290925d6d", Priority: `0.9`}, | ||
} | ||
|
||
for i := range sitemapUrls { | ||
sitemapUrls[i].XMLName.Space = "http://www.sitemaps.org/schemas/sitemap/0.9" | ||
sitemapUrls[i].XMLName.Local = "url" | ||
sitemapUrls[i].ChangeFreq = "monthly" | ||
sitemapUrls[i].LastMod = "2022-03-01T00:00:00.000Z" | ||
} | ||
|
||
return sitemapUrls | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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><strong>Application: </strong> </span> | ||
<span class="title-span">{{.JobTitle}} - {{.Name}}</span> | ||
</p> | ||
<p> | ||
<span><strong>Message: </strong> </span> | ||
<span>{{.Message}}</span> | ||
</p> | ||
<p> | ||
<span><strong>Email: </strong> </span> | ||
<span>{{.Email}}</span> | ||
</p> | ||
<p> | ||
<span><strong>Phone:</strong> </span> | ||
<span>{{.Phone}}</span> | ||
</p> | ||
<p> | ||
<span><strong>City: </strong></span> | ||
<span>{{.Place}}</span> | ||
</p> | ||
<p> | ||
<span><strong>Reference: </strong></span> | ||
<span>{{.References}}</span> | ||
</p> | ||
</div> | ||
</body> | ||
|
||
</html> |