Skip to content

Commit

Permalink
add schema validation to profiles controller specs and tweak schema
Browse files Browse the repository at this point in the history
  • Loading branch information
npauzenga committed Jan 30, 2016
1 parent 3d4b408 commit cd3f9cf
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 34 deletions.
10 changes: 2 additions & 8 deletions config/schema/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,9 @@
"description": "unique identifier of user",
"readOnly": true,
"type": [
"integer"
"string"
],
"example": 1294
"example": "1294"
},
"first_name": {
"description": "first name of user",
Expand Down Expand Up @@ -827,9 +827,6 @@
},
"country": {
"$ref": "#/definitions/user/definitions/country"
},
"password": {
"$ref": "#/definitions/user/definitions/password"
}
}
}
Expand Down Expand Up @@ -953,9 +950,6 @@
},
"country": {
"$ref": "#/definitions/user/definitions/country"
},
"password": {
"$ref": "#/definitions/user/definitions/password"
}
}
}
Expand Down
24 changes: 11 additions & 13 deletions config/schema/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ The User resource for the API
| **country** | *string* | country of user | `"USA"` |
| **created_at** | *date-time* | when user was created | `"2015-01-01T12:00:00Z"` |
| **first_name** | *string* | first name of user | `"Nate"` |
| **id** | *integer* | unique identifier of user | `1294` |
| **id** | *string* | unique identifier of user | `"1294"` |
| **last_name** | *string* | last name of user | `"Pauzenga"` |
| **state** | *string* | state of user | `"Maryland"` |
| **updated_at** | *date-time* | when user was updated | `"2015-01-01T12:00:00Z"` |
Expand All @@ -511,7 +511,7 @@ POST /users
| **country** | *string* | country of user | `"USA"` |
| **email** | *string* | unique email of user | `"test@admin.com"` |
| **first_name** | *string* | first name of user | `"Nate"` |
| **id** | *integer* | unique identifier of user | `1294` |
| **id** | *string* | unique identifier of user | `"1294"` |
| **last_name** | *string* | last name of user | `"Pauzenga"` |
| **password** | *string* | password of user | `"helloworld"` |
| **state** | *string* | state of user | `"Maryland"` |
Expand All @@ -523,7 +523,7 @@ POST /users
```bash
$ curl -n -X POST http://localhost:3000/users \
-d '{
"id": 1294,
"id": "1294",
"first_name": "Nate",
"last_name": "Pauzenga",
"email": "test@admin.com",
Expand Down Expand Up @@ -575,7 +575,7 @@ HTTP/1.1 200 OK
```json
{
"created_at": "2015-01-01T12:00:00Z",
"id": 1294,
"id": "1294",
"first_name": "Nate",
"updated_at": "2015-01-01T12:00:00Z",
"last_name": "Pauzenga",
Expand Down Expand Up @@ -610,14 +610,13 @@ HTTP/1.1 200 OK

```json
{
"id": 1294,
"id": "1294",
"first_name": "Nate",
"last_name": "Pauzenga",
"email": "test@admin.com",
"city": "Annapolis",
"state": "Maryland",
"country": "USA",
"password": "helloworld"
"country": "USA"
}
```

Expand Down Expand Up @@ -647,7 +646,7 @@ HTTP/1.1 200 OK
```json
[
{
"id": 1294,
"id": "1294",
"first_name": "Nate",
"last_name": "Pauzenga",
"email": "test@admin.com",
Expand Down Expand Up @@ -675,7 +674,7 @@ PATCH /users/{user_id_or_email}
| **country** | *string* | country of user | `"USA"` |
| **email** | *string* | unique email of user | `"test@admin.com"` |
| **first_name** | *string* | first name of user | `"Nate"` |
| **id** | *integer* | unique identifier of user | `1294` |
| **id** | *string* | unique identifier of user | `"1294"` |
| **last_name** | *string* | last name of user | `"Pauzenga"` |
| **password** | *string* | password of user | `"helloworld"` |
| **state** | *string* | state of user | `"Maryland"` |
Expand All @@ -687,7 +686,7 @@ PATCH /users/{user_id_or_email}
```bash
$ curl -n -X PATCH http://localhost:3000/users/$USER_ID_OR_EMAIL \
-d '{
"id": 1294,
"id": "1294",
"first_name": "Nate",
"last_name": "Pauzenga",
"email": "test@admin.com",
Expand All @@ -709,14 +708,13 @@ HTTP/1.1 200 OK

```json
{
"id": 1294,
"id": "1294",
"first_name": "Nate",
"last_name": "Pauzenga",
"email": "test@admin.com",
"city": "Annapolis",
"state": "Maryland",
"country": "USA",
"password": "helloworld"
"country": "USA"
}
```

Expand Down
12 changes: 5 additions & 7 deletions config/schema/schemata/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"description": "unique identifier of user",
"readOnly": true,
"type": [
"integer"
"string"
],
"example": 1294
"example": "1294"
},
"first_name": {
"description": "first name of user",
Expand Down Expand Up @@ -148,7 +148,7 @@
"method": "GET",
"rel": "self",
"http_header": {
"Authorization": "Bearer: iOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0NTM5MzkwMjYsImF1ZCI6bnVsbCwic3ViIjoxfQ.TSgJie1vAWxR_ym_uazjKs43rpQ50OKmb2KZ2N6ALs4"
"Authorization": "Bearer: iOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0NTM5MzkwMjYsImF1ZCI6bnVsbCwic3ViIjoxfQ.TSgJie1vAWxR_ym_uazjKs43rpQ50OKmb2KZ2N6ALs4"
},
"title": "Info",
"targetSchema": {
Expand All @@ -160,8 +160,7 @@
"email": { "$ref": "/schemata/user#/definitions/email" },
"city": { "$ref": "/schemata/user#/definitions/city" },
"state": { "$ref": "/schemata/user#/definitions/state" },
"country": { "$ref": "/schemata/user#/definitions/country" },
"password": { "$ref": "/schemata/user#/definitions/password" }
"country": { "$ref": "/schemata/user#/definitions/country" }
}
}
},
Expand Down Expand Up @@ -223,8 +222,7 @@
"email": { "$ref": "/schemata/user#/definitions/email" },
"city": { "$ref": "/schemata/user#/definitions/city" },
"state": { "$ref": "/schemata/user#/definitions/state" },
"country": { "$ref": "/schemata/user#/definitions/country" },
"password": { "$ref": "/schemata/user#/definitions/password" }
"country": { "$ref": "/schemata/user#/definitions/country" }
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions spec/acceptance/profiles_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
Knock::AuthToken.new(payload: { sub: authenticated_user.id }).token
end

def schema_path
"./config/schema/api.json"
end

shared_context "user parameters" do
parameter "first_name", <<-DESC, scope: :user, required: true
The first name of the user.
Expand Down
7 changes: 4 additions & 3 deletions spec/acceptance/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
Knock::AuthToken.new(payload: { sub: authenticated_user.id }).token
end

def schema_path
"./config/schema/schemata/user.json"
end

shared_context "user parameters" do
parameter "first_name", <<-DESC, scope: :user, required: true
The first name of the user.
Expand Down Expand Up @@ -64,9 +68,6 @@

header "Authorization", :auth_token

# Currently update will only update the current user, no matter the id
let(:id) { authenticated_user.id }

example_request "PATCH /user" do
expect(status).to eq 200
user = JSON.parse(response_body)
Expand Down
23 changes: 20 additions & 3 deletions spec/controllers/profiles_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
include Committee::Test::Methods

RSpec.describe ProfilesController do
let(:schema_path) { "#{Rails.root}/config/schema/api.json" }
let(:last_response) { response }
let(:last_request) { request }

describe "GET #show" do
let(:user) { create(:confirmed_user) }
let(:params) { { id: user.id } }
let(:show_profile_input) { { id: params.fetch(:id).to_s } }
let(:show_profile_input) { { id: params.fetch(:id).to_s} }

let(:show_profile_context) do
Interactor::Context.new(errors: :val, user: user)
Expand Down Expand Up @@ -32,6 +38,11 @@
get :show, params
expect(serialize(user)).to eq(response.body)
end

it "conforms to JSON schema" do
get :show, params
assert_schema_conform
end
end

context "when ShowProfile fails" do
Expand Down Expand Up @@ -82,8 +93,14 @@

it "render the profiles as JSON" do
get :index
expect(response.body).
to eq([serialize(user), serialize(user2), serialize(user3)])
expect(json["data"][0].has_value?(user.id.to_s)).to be_truthy
expect(json["data"][1].has_value?(user2.id.to_s)).to be_truthy
expect(json["data"][2].has_value?(user3.id.to_s)).to be_truthy
end

it "conforms to JSON schema" do
get :index
assert_schema_conform
end
end

Expand Down

0 comments on commit cd3f9cf

Please sign in to comment.