1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00
Ivan Milošević ee262b9647 MF-417 - Implement SDK tests (#438)
* Add SKD tests for creating channel

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Add SDK tests for Channel function

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Add list channels over SDK tests

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Add SDK tests for updating channel

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Add SDK tests for deleting channel

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Add users service SDK tests

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* SDK things tests

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Add SDK test for connecting and disconnecting things from channel

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* testing SDK sending messages

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* add tests for SDK func SetContentType

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* add all test cases for sending messages

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Add handling StatusBadRequest when deleting thing over SDK

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Update error responses when deleting channel and thing

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Removed unused Unauthorized response when creating user

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* update testing CreateChannel, tests if response is some string

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Add bad request case in testing CreateToken

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Remove response error conflict from things service

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Fix definition of sdk.ErrFailedDisconnect and return error in test cases for disconnecting things

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Add handling errors and formatting code

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Defined new ErrFailedPublish error in SDK

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Add SDK test for version

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Delete unused http response status in sdk.DeleteChannel

Signed-off-by: Ivan Milošević <iva@blokovi.com>
2018-11-19 21:27:01 +01:00

81 lines
1.4 KiB
Go

//
// Copyright (c) 2018
// Mainflux
//
// SPDX-License-Identifier: Apache-2.0
//
package sdk
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
)
func (sdk mfSDK) CreateUser(user User) error {
data, err := json.Marshal(user)
if err != nil {
return ErrInvalidArgs
}
url := createURL(sdk.url, sdk.usersPrefix, "users")
resp, err := sdk.client.Post(url, string(CTJSON), bytes.NewReader(data))
if err != nil {
return err
}
if resp.StatusCode != http.StatusCreated {
switch resp.StatusCode {
case http.StatusBadRequest:
return ErrInvalidArgs
case http.StatusConflict:
return ErrConflict
default:
return ErrFailedCreation
}
}
return nil
}
func (sdk mfSDK) CreateToken(user User) (string, error) {
data, err := json.Marshal(user)
if err != nil {
return "", ErrInvalidArgs
}
url := createURL(sdk.url, sdk.usersPrefix, "tokens")
resp, err := sdk.client.Post(url, string(CTJSON), bytes.NewReader(data))
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusCreated {
switch resp.StatusCode {
case http.StatusBadRequest:
return "", ErrInvalidArgs
case http.StatusForbidden:
return "", ErrUnauthorized
default:
return "", ErrFailedCreation
}
}
var t tokenRes
if err := json.Unmarshal(body, &t); err != nil {
return "", err
}
return t.Token, nil
}