Skip to content

Commit

Permalink
Test improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufozturk committed Jan 25, 2022
1 parent 3b509c7 commit c7c5fcc
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 0 deletions.
5 changes: 5 additions & 0 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func GetPublicKey(client *Client) (string, error) {
// Close Response
defer resp.Body.Close()

// Check Response Code
if resp.StatusCode != 200 {
return "", errors.New(resp.Status)
}

// Read Response Body
respBody, err := io.ReadAll(resp.Body)
if err != nil {
Expand Down
109 changes: 109 additions & 0 deletions auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,115 @@ import (
"testing"
)

func TestAuthenticate_OK(t *testing.T) {
// Test Response
authResponse := &SangforAuthResponse{}
authResponse.Data.Access.Token.ID = "Test"

// Mock Database
db := MockDatabase{}
db.AddToDatabase("", authResponse)

// Get Client
client := GetAPIClient("host")
client.Client.Transport = NewMockTransport(200, map[string]string{}, db.Database)

// Authenticate
err := client.Authenticate("", "")

// Check Errors
if err != nil {
t.Fatal(err)
return
}
}

func TestAuthenticate_NoToken(t *testing.T) {
// Test Response
authResponse := &SangforAuthResponse{}
authResponse.Data.Access.Token.ID = ""

// Mock Database
db := MockDatabase{}
db.AddToDatabase("", authResponse)

// Get Client
client := GetAPIClient("host")
client.Client.Transport = NewMockTransport(200, map[string]string{}, db.Database)

// Authenticate
err := client.Authenticate("", "")

// Check Errors
if err == nil {
t.Fatal(err)
}
}

func TestGetPublicKey_OK(t *testing.T) {
// Test Response
sangforPK := SangforPK{}
sangforPK.Data.PublicKey = "PublicKey"

// Mock Database
db := MockDatabase{}
db.AddToDatabase("", sangforPK)

// Get Client
client := GetAPIClient("host")
client.Client.Transport = NewMockTransport(200, map[string]string{}, db.Database)

// Get Public Key
publicKey, err := GetPublicKey(client)

// Check Public Key
if publicKey == "PublicKey" {
return
}

// Check Errors
if err == nil {
t.Fatal("connection is failed")
return
}

t.Fatal(err)
}

func TestGetPublicKey_Error(t *testing.T) {
// Get Client
client := GetAPIClient("host")
client.Client.Transport = NewMockTransport(500, map[string]string{}, map[string][]byte{})

// Get Public Key
_, err := GetPublicKey(client)

// Check Errors
if err == nil {
t.Fatal("connection is failed")
return
}
}

func TestGetPublicKey_JSONError(t *testing.T) {
// Test Response
sangforPK := SangforPK{}
sangforPK.Data.PublicKey = "PublicKey"

// Get Client
client := GetAPIClient("host")
client.Client.Transport = NewMockTransport(200, map[string]string{}, map[string][]byte{})

// Get Public Key
_, err := GetPublicKey(client)

// Check Errors
if err == nil {
t.Fatal("json validation is failed")
return
}
}

func TestGetEncryptedPassword(t *testing.T) {
// Public Key
publicKey := "D9905621B5800B5BEC903FB5E96C42DF23B6B0ABC9878C7310A62254DD0F8B54C6027C5A0C0511" +
Expand Down
123 changes: 123 additions & 0 deletions test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package main

import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
)

type MockTransport struct {
Response MockResponse
Error error
}

func NewMockTransport(statusCode int, headers map[string]string, body map[string][]byte) *MockTransport {
return &MockTransport{
Response: NewMockResponse(statusCode, headers, body),
}
}

// RoundTrip receives HTTP requests and routes them to the appropriate
// responder. It is required to implement the http.RoundTripper
// interface. You will not interact with this directly, instead the
// *http.Client you are using will call it for you.
func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if m.Error != nil {
return nil, m.Error
}

return m.Response.MakeResponse(req), nil
}

type MockResponse struct {
StatusCode int
HeadersMap map[string]string
Body map[string][]byte
}

func NewMockResponse(statusCode int, headers map[string]string, body map[string][]byte) MockResponse {
if headers == nil {
headers = map[string]string{}
}

if body == nil {
body = map[string][]byte{}
}

return MockResponse{StatusCode: statusCode, HeadersMap: headers, Body: body}
}

func (r *MockResponse) MakeResponse(req *http.Request) *http.Response {
// Get Status
status := strconv.Itoa(r.StatusCode) + " " + http.StatusText(r.StatusCode)

// Get HTTP Headers
header := http.Header{}
for name, value := range r.HeadersMap {
header.Set(name, value)
}

// Get HTTP Response
httpResponse := r.Body[req.URL.String()]
if httpResponse == nil {
for _, response := range r.Body {
httpResponse = response
break
}
}

// Set Content Length
contentLength := len(httpResponse)
header.Set("Content-Length", strconv.Itoa(contentLength))

// Set HTTP Response
res := &http.Response{
Status: status,
StatusCode: r.StatusCode,
Proto: "HTTP/1.0",
ProtoMajor: 1,
ProtoMinor: 0,
Header: header,
Body: ioutil.NopCloser(bytes.NewReader(httpResponse)),
ContentLength: int64(contentLength),
TransferEncoding: []string{},
Close: false,
Uncompressed: false,
Trailer: nil,
Request: req,
TLS: nil,
}

// Should no set Content-Length header when 204 or 304
if r.StatusCode == http.StatusNoContent || r.StatusCode == http.StatusNotModified {
if res.ContentLength != 0 {
res.Body = ioutil.NopCloser(bytes.NewReader([]byte{}))
res.ContentLength = 0
}
header.Del("Content-Length")
}

return res
}

type MockDatabase struct {
Database map[string][]byte
}

func (d *MockDatabase) AddToDatabase(key string, value interface{}) {
// Convert to JSON
jsonResponse, err := json.Marshal(value)
if err != nil {
return
}

// Check Database
if d.Database == nil {
d.Database = make(map[string][]byte)
}

// Add to Database
d.Database[key] = jsonResponse
}

0 comments on commit c7c5fcc

Please sign in to comment.