Skip to content

Commit

Permalink
test create new post
Browse files Browse the repository at this point in the history
  • Loading branch information
vinhut committed Jun 29, 2020
1 parent 61ce0f1 commit 779f8c9
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
mocks_models "github.com/vinhut/posted/mocks_models"
mocks_services "github.com/vinhut/posted/mocks_services"

"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
"time"
Expand Down Expand Up @@ -80,3 +82,36 @@ func TestGetPost(t *testing.T) {
assert.Equal(t, 200, w.Code)

}

func TestCreatePost(t *testing.T) {

now := time.Now()
token := "852a37a34b727c0e0b331806-7af4bdfdcc60990d427f383efecc8529289d040dd67e0753b9e2ee5a1e938402186f28324df23f6faa4e2bbf43f584ae228c55b00143866215d6e92805d470a1cc2a096dcca4d43527598122313be412e17fbefdcdab2fae02e06a405791d936862d4fba688b3c7fd784d4"
user_data := "{\"uid\": \"1\", \"email\": \"[email protected]\", \"role\": \"standard\", \"created\": \"" + now.Format("2006-01-02T15:04:05") + "\"}"
image_url := "http://localhost/img.png"
caption := "test caption"

os.Setenv("KEY", "12345678901234567890123456789012")
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mock_post := mocks_models.NewMockPostDatabase(ctrl)
mock_auth := mocks_services.NewMockAuthService(ctrl)

mock_auth.EXPECT().Check(gomock.Any(), gomock.Any()).Return(user_data, nil)
mock_post.EXPECT().Create(gomock.Any()).Return(true, nil)

router := setupRouter(mock_post, mock_auth)

var param = url.Values{}
param.Set("img_url", image_url)
param.Set("post_caption", caption)
var payload = bytes.NewBufferString(param.Encode())

w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", SERVICE_NAME+"/post", payload)
req.Header.Set("Cookie", "token="+token+";")
router.ServeHTTP(w, req)

assert.Equal(t, 200, w.Code)

}

0 comments on commit 779f8c9

Please sign in to comment.