-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
73 lines (56 loc) · 2.19 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"encoding/json"
"loqi/messaging/structs"
h "loqi/messaging/testing"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func SetUpRouter() *gin.Engine {
router := gin.Default()
return router
}
// Initialization
var unitTestCollection string = "chats/TEST/01-LEC/room/messages"
var testingRoute string = "/api/messaging"
func TestReadWriteAndDeleteFireStoreHandler(t *testing.T) {
//Set Up
var expectedReadResponseFromAPI structs.ReadResponseBody
// Initialization
json.Unmarshal([]byte(`{"author":"Testing Script", "authorPhotoURL":"https://test.com", "content":"This is to test read functionality", "firstCreated": 12312312}`), &expectedReadResponseFromAPI)
// Setting up Routes
r := SetUpRouter()
r.GET(testingRoute, ReadFireStoreHandler)
r.POST(testingRoute, WriteFireStoreHandler)
r.DELETE(testingRoute, DeleteFireStoreHandler)
w := httptest.NewRecorder()
// Testing POST Functionalities
postRequestBody, _ := json.Marshal(map[string]string{
"collectionPath": unitTestCollection,
"author": "Testing Script",
"authorPhotoURL": "https://test.com",
"content": "This is to test read functionality",
})
testMessageRefPath := h.MakePostRequest(r, w, postRequestBody)
assert.Equal(t, http.StatusOK, w.Code)
// Testing GET Functionalities
getRequestBody, _ := json.Marshal(map[string]string{
"fullMessagePath": testMessageRefPath,
})
readResponseFromAPI := h.MakeGetRequest(r, w, getRequestBody)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, expectedReadResponseFromAPI.Author, readResponseFromAPI.Author)
assert.Equal(t, expectedReadResponseFromAPI.AuthorPhotoURL, readResponseFromAPI.AuthorPhotoURL)
assert.Equal(t, expectedReadResponseFromAPI.Content, readResponseFromAPI.Content)
// Testing DELETE Functionalities
expectedDeleteResponseFromAPI := testMessageRefPath
deleteRequestBody, _ := json.Marshal(map[string]string{
"fullMessagePath": testMessageRefPath,
})
deleteResponseFromAPI := h.MakeDeleteRequest(r, w, deleteRequestBody)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, expectedDeleteResponseFromAPI, deleteResponseFromAPI.RemovedFullMessagePath)
}