From 28fdf6dad81435708ed786312be0b276de35bbfc Mon Sep 17 00:00:00 2001 From: Juan Arias Date: Tue, 7 Nov 2023 20:50:49 -0300 Subject: [PATCH] feat(Groups): Tests and re-generate JSON file --- api/spec/api/groups_spec.rb | 277 +++++++++++++++++++++ api/swagger/v1/swagger.json | 483 +++++++++++++++++++++++++++++++----- 2 files changed, 701 insertions(+), 59 deletions(-) create mode 100644 api/spec/api/groups_spec.rb diff --git a/api/spec/api/groups_spec.rb b/api/spec/api/groups_spec.rb new file mode 100644 index 00000000..1c3920d3 --- /dev/null +++ b/api/spec/api/groups_spec.rb @@ -0,0 +1,277 @@ +require 'swagger_helper' + +RSpec.describe 'groups', type: :request do + # Authorization + let(:user) { create :user } + let(:headers) do + post user_session_path, params: { user: { email: user.email, password: user.password } } + + response.headers + end + + # Index + path '/groups' do + get 'Returns all groups that match the filters' do + tags 'Groups' + produces 'application/json' + parameter name: :name, in: :query, type: :string, value: 'Name' + parameter name: :subject_id, in: :query, type: :string, value: '1' + parameter name: :my_groups, in: :query, type: :string, value: 'true' + parameter name: :time_preferences, in: :query, type: :string, value: 'night,afternoon' + parameter name: 'Authorization', in: :header, type: :string, value: 'Bearer ' + + response '200', 'Success' do + let!(:group_one) do + create :group, + name: 'Foo', + subject: subject_two, + time_preferences: { 'Monday' => 'Night', 'Tuesday' => 'Afternoon', 'Wednesday' => 'Night' } + end + let!(:group_two) do + create :group, + name: 'Bar', + subject: subject_one, + time_preferences: { 'Monday' => 'Morning' } + end + let!(:group_three) do + create :group, + name: 'Baz', + subject: subject_one, + time_preferences: { 'Monday' => 'Night' } + end + + let(:subject_one) { create :subject } + let(:subject_two) { create :subject } + + let(:name) { 'Ba' } + let(:subject_id) { subject_one.id.to_s } + let(:my_groups) { 'true' } + let(:time_preferences) { 'night,afternoon' } + + let('Authorization') do + headers['Authorization'] + end + + before do + create :member, user:, group: group_three + end + + generate_swagger_response('index_groups') + run_test! do |response| + json_response = JSON.parse(response.body) + + expect(json_response[0]['id']).to eq(group_three.id) + expect(json_response[0]['name']).to eq(group_three.name) + expect(json_response[0]['description']).to eq(group_three.description) + expect(json_response[0]['size']).to eq(group_three.size) + expect(json_response[0]['time_preferences']).to eq(group_three.time_preferences) + expect(json_response[0]['subject_id']).to eq(group_three.subject_id) + expect(json_response[0]['subject_name']).to eq(group_three.subject.name) + end + end + end + end + + # Create + path '/groups' do + post 'Creates a group' do + tags 'Groups' + consumes 'application/json' + produces 'application/json' + parameter name: :group, in: :body, schema: { + type: :object, + properties: { + name: { type: :string }, + description: { type: :string }, + size: { type: :integer }, + subject_id: { type: :integer }, + time_preferences: { type: :object } + }, + required: ['group'] + } + parameter name: 'Authorization', in: :header, type: :string, value: 'Bearer ' + + response '201', 'Success' do + let(:subject) { create :subject } + let(:name) { 'Name' } + let(:description) { 'Description' } + let(:size) { 5 } + let(:subject_id) { subject.id } + let(:time_preferences) do + { + 'Monday' => 'Morning', + 'Tuesday' => 'Afternoon', + 'Wednesday' => 'Night', + 'Thursday' => 'None', + 'Friday' => 'Morning', + 'Saturday' => 'Afternoon', + 'Sunday' => 'Night' + } + end + let(:group) do + { + name:, + description:, + size:, + subject_id:, + time_preferences: + } + end + + let('Authorization') do + headers['Authorization'] + end + + generate_swagger_response('create_groups') + run_test! do |response| + json_response = JSON.parse(response.body) + + expect(json_response['id']).to be_a(Integer) + expect(json_response['name']).to eq(name) + expect(json_response['description']).to eq(description) + expect(json_response['size']).to eq(size) + expect(json_response['subject_id']).to eq(subject_id) + expect(json_response['time_preferences']).to eq(time_preferences) + end + end + end + end + + # Show + path '/groups/{id}' do + get 'Returns a group' do + tags 'Groups' + produces 'application/json' + parameter name: :id, in: :path, type: :integer, required: true, description: 'Group ID' + parameter name: 'Authorization', in: :header, type: :string, value: 'Bearer ' + + response '200', 'Success' do + let(:group) { create :group } + let(:id) { group.id } + let('Authorization') do + headers['Authorization'] + end + + generate_swagger_response('show_groups') + run_test! do |response| + json_response = JSON.parse(response.body) + + expect(json_response['id']).to eq(group.id) + expect(json_response['name']).to eq(group.name) + expect(json_response['description']).to eq(group.description) + expect(json_response['size']).to eq(group.size) + expect(json_response['time_preferences']).to eq(group.time_preferences) + expect(json_response['subject_id']).to eq(group.subject_id) + expect(json_response['subject_name']).to eq(group.subject.name) + expect(json_response['user_ids']).to eq(group.users.pluck(:id)) + expect(json_response['admin_ids']).to eq(group.admins.pluck(:id)) + expect(json_response['requests']).to be_an(Array) + expect(json_response['sessions']).to be_an(Array) + end + end + end + end + + # Update + path '/groups/{id}' do + patch 'Updates a group' do + tags 'Groups' + consumes 'application/json' + produces 'application/json' + parameter name: :id, in: :path, type: :integer, required: true, description: 'Group ID' + parameter name: :group, in: :body, schema: { + type: :object, + properties: { + name: { type: :string }, + description: { type: :string }, + size: { type: :integer }, + subject_id: { type: :integer }, + time_preferences: { type: :object } + }, + required: ['group'] + } + parameter name: 'Authorization', in: :header, type: :string, value: 'Bearer ' + + response '200', 'Success' do + let(:group_to_update) { create :group } + let(:id) { group_to_update.id } + + let(:subject) { create :subject } + let(:name) { 'New Name' } + let(:description) { 'New Description' } + let(:size) { 5 } + let(:subject_id) { subject.id } + let(:time_preferences) do + { + 'Monday' => 'Morning', + 'Tuesday' => 'Afternoon', + 'Wednesday' => 'Night', + 'Thursday' => 'None', + 'Friday' => 'Morning', + 'Saturday' => 'Afternoon', + 'Sunday' => 'Night' + } + end + let(:group) do + { + name:, + description:, + size:, + subject_id:, + time_preferences: + } + end + + let('Authorization') do + headers['Authorization'] + end + + before do + create :member, user:, group: group_to_update + end + + generate_swagger_response('update_groups') + run_test! do |response| + json_response = JSON.parse(response.body) + + expect(json_response['id']).to eq(group_to_update.id) + expect(json_response['name']).to eq(name) + expect(json_response['description']).to eq(description) + expect(json_response['size']).to eq(size) + expect(json_response['time_preferences']).to eq(time_preferences) + expect(json_response['subject_id']).to eq(subject_id) + expect(json_response['subject_name']).to eq(subject.name) + expect(json_response['user_ids']).to eq(group_to_update.users.pluck(:id)) + expect(json_response['admin_ids']).to eq(group_to_update.admins.pluck(:user_id)) + expect(json_response['requests']).to be_an(Array) + expect(json_response['sessions']).to be_an(Array) + end + end + end + end + + # Destroy + path '/groups/{id}' do + delete 'Deletes a group' do + tags 'Groups' + produces 'application/json' + parameter name: :id, in: :path, type: :integer, required: true, description: 'Group ID' + parameter name: 'Authorization', in: :header, type: :string, value: 'Bearer ' + + response '204', 'Success' do + let(:group) { create :group } + let(:id) { group.id } + + let('Authorization') do + headers['Authorization'] + end + + before do + create :member, user:, group: + end + + run_test! + end + end + end +end diff --git a/api/swagger/v1/swagger.json b/api/swagger/v1/swagger.json index 8b39cd32..e65eba58 100644 --- a/api/swagger/v1/swagger.json +++ b/api/swagger/v1/swagger.json @@ -38,10 +38,10 @@ "examples": { "update_attendances": { "value": { - "id": 2033, + "id": 2379, "status": "accepted", - "session_id": 1082, - "member_id": 3175 + "session_id": 1267, + "member_id": 3735 } } } @@ -93,7 +93,7 @@ "index_careers": { "value": [ { - "id": 358, + "id": 415, "name": "Career_1", "code": "1", "approved_on": "2023", @@ -104,7 +104,7 @@ ] }, { - "id": 359, + "id": 416, "name": "Career_2", "code": "2", "approved_on": "2023", @@ -123,6 +123,371 @@ } } }, + "/groups": { + "get": { + "summary": "Returns all groups that match the filters", + "tags": [ + "Groups" + ], + "parameters": [ + { + "name": "name", + "in": "query", + "value": "Name", + "schema": { + "type": "string" + } + }, + { + "name": "subject_id", + "in": "query", + "value": "1", + "schema": { + "type": "string" + } + }, + { + "name": "my_groups", + "in": "query", + "value": "true", + "schema": { + "type": "string" + } + }, + { + "name": "time_preferences", + "in": "query", + "value": "night,afternoon", + "schema": { + "type": "string" + } + }, + { + "name": "Authorization", + "in": "header", + "value": "Bearer ", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "Success", + "content": { + "application/json": { + "examples": { + "index_groups": { + "value": [ + { + "id": 2688, + "name": "Baz", + "description": "Description", + "size": 3, + "time_preferences": { + "Monday": "Night" + }, + "subject_id": 3206, + "subject_name": "Subject_4", + "user_ids": [ + 5272 + ], + "admin_ids": [ + 5272 + ], + "requests": [ + + ], + "sessions": [ + + ] + } + ] + } + } + } + } + } + } + }, + "post": { + "summary": "Creates a group", + "tags": [ + "Groups" + ], + "parameters": [ + { + "name": "Authorization", + "in": "header", + "value": "Bearer ", + "schema": { + "type": "string" + } + } + ], + "responses": { + "201": { + "description": "Success", + "content": { + "application/json": { + "examples": { + "create_groups": { + "value": { + "id": 2689, + "name": "Name", + "description": "Description", + "size": 5, + "time_preferences": { + "Monday": "Morning", + "Tuesday": "Afternoon", + "Wednesday": "Night", + "Thursday": "None", + "Friday": "Morning", + "Saturday": "Afternoon", + "Sunday": "Night" + }, + "subject_id": 3207, + "subject_name": "Subject_5", + "user_ids": [ + 5273 + ], + "admin_ids": [ + 5273 + ], + "requests": [ + + ], + "sessions": [ + + ] + } + } + } + } + } + } + }, + "requestBody": { + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "description": { + "type": "string" + }, + "size": { + "type": "integer" + }, + "subject_id": { + "type": "integer" + }, + "time_preferences": { + "type": "object" + } + }, + "required": [ + "group" + ] + } + } + } + } + } + }, + "/groups/{id}": { + "get": { + "summary": "Returns a group", + "tags": [ + "Groups" + ], + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "description": "Group ID", + "schema": { + "type": "integer" + } + }, + { + "name": "Authorization", + "in": "header", + "value": "Bearer ", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "Success", + "content": { + "application/json": { + "examples": { + "show_groups": { + "value": { + "id": 2690, + "name": "Name_3", + "description": "Description", + "size": 3, + "time_preferences": { + "Monday": "Morning", + "Tuesday": "Afternoon" + }, + "subject_id": 3208, + "subject_name": "Subject_6", + "user_ids": [ + + ], + "admin_ids": [ + + ], + "requests": [ + + ], + "sessions": [ + + ] + } + } + } + } + } + } + } + }, + "patch": { + "summary": "Updates a group", + "tags": [ + "Groups" + ], + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "description": "Group ID", + "schema": { + "type": "integer" + } + }, + { + "name": "Authorization", + "in": "header", + "value": "Bearer ", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "Success", + "content": { + "application/json": { + "examples": { + "update_groups": { + "value": { + "id": 2691, + "name": "New Name", + "description": "New Description", + "size": 5, + "time_preferences": { + "Monday": "Morning", + "Tuesday": "Afternoon", + "Wednesday": "Night", + "Thursday": "None", + "Friday": "Morning", + "Saturday": "Afternoon", + "Sunday": "Night" + }, + "subject_id": 3210, + "subject_name": "Subject_8", + "user_ids": [ + 5275 + ], + "admin_ids": [ + 5275 + ], + "requests": [ + + ], + "sessions": [ + + ] + } + } + } + } + } + } + }, + "requestBody": { + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "description": { + "type": "string" + }, + "size": { + "type": "integer" + }, + "subject_id": { + "type": "integer" + }, + "time_preferences": { + "type": "object" + } + }, + "required": [ + "group" + ] + } + } + } + } + }, + "delete": { + "summary": "Deletes a group", + "tags": [ + "Groups" + ], + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "description": "Group ID", + "schema": { + "type": "integer" + } + }, + { + "name": "Authorization", + "in": "header", + "value": "Bearer ", + "schema": { + "type": "string" + } + } + ], + "responses": { + "204": { + "description": "Success" + } + } + } + }, "/": { "get": { "summary": "Returns application status", @@ -261,25 +626,25 @@ "examples": { "create_sessions": { "value": { - "id": 1083, + "id": 1268, "name": "Name", "description": "Description", "location": "Location", "meeting_link": "Meeting Link", "start_time": "2023-11-07T00:00:00.000Z", "end_time": "2023-11-07T02:00:00.000Z", - "group_id": 2255, - "creator_user_id": 4494, + "group_id": 2695, + "creator_user_id": 5281, "attendances": [ { - "id": 2034, - "session_id": 1083, + "id": 2380, + "session_id": 1268, "status": "pending", - "created_at": "2023-11-07T23:01:37.395Z", - "updated_at": "2023-11-07T23:01:37.395Z", - "member_id": 3183, - "member_name": "Name_9", - "user_id": 4494 + "created_at": "2023-11-07T23:49:06.507Z", + "updated_at": "2023-11-07T23:49:06.507Z", + "member_id": 3747, + "member_name": "Name_14", + "user_id": 5281 } ] } @@ -359,25 +724,25 @@ "examples": { "show_sessions": { "value": { - "id": 1084, + "id": 1269, "name": "Session_2", "description": "Description", "location": "Location", "meeting_link": "https://zoom.us/meeting_link", - "start_time": "2023-11-08T23:01:37.426Z", - "end_time": "2023-11-09T01:01:37.426Z", - "group_id": 2256, - "creator_user_id": 4495, + "start_time": "2023-11-08T23:49:06.581Z", + "end_time": "2023-11-09T01:49:06.581Z", + "group_id": 2696, + "creator_user_id": 5282, "attendances": [ { - "id": 2035, - "session_id": 1084, + "id": 2381, + "session_id": 1269, "status": "pending", - "created_at": "2023-11-07T23:01:37.432Z", - "updated_at": "2023-11-07T23:01:37.432Z", - "member_id": 3184, - "member_name": "Name_10", - "user_id": 4495 + "created_at": "2023-11-07T23:49:06.588Z", + "updated_at": "2023-11-07T23:49:06.588Z", + "member_id": 3748, + "member_name": "Name_15", + "user_id": 5282 } ] } @@ -420,25 +785,25 @@ "examples": { "update_sessions": { "value": { - "id": 1085, + "id": 1270, "name": "New Name", "description": "New Description", "location": "New Location", "meeting_link": "New Meeting Link", "start_time": "2023-11-07T00:00:00.000Z", "end_time": "2023-11-07T02:00:00.000Z", - "group_id": 2257, - "creator_user_id": 4496, + "group_id": 2697, + "creator_user_id": 5283, "attendances": [ { - "id": 2036, - "session_id": 1085, + "id": 2382, + "session_id": 1270, "status": "pending", - "created_at": "2023-11-07T23:01:37.495Z", - "updated_at": "2023-11-07T23:01:37.495Z", - "member_id": 3185, - "member_name": "Name_11", - "user_id": 4496 + "created_at": "2023-11-07T23:49:06.641Z", + "updated_at": "2023-11-07T23:49:06.641Z", + "member_id": 3749, + "member_name": "Name_16", + "user_id": 5283 } ] } @@ -540,9 +905,9 @@ "index_subjects": { "value": [ { - "id": 2704, - "name": "Subject_9", - "code": "9", + "id": 3218, + "name": "Subject_16", + "code": "16", "credits": 10, "group_ids": [ @@ -552,9 +917,9 @@ ] }, { - "id": 2705, - "name": "Subject_10", - "code": "10", + "id": 3219, + "name": "Subject_17", + "code": "17", "credits": 10, "group_ids": [ @@ -605,9 +970,9 @@ "examples": { "show_subjects": { "value": { - "id": 2706, - "name": "Subject_11", - "code": "11", + "id": 3220, + "name": "Subject_18", + "code": "18", "credits": 10, "group_ids": [ @@ -657,9 +1022,9 @@ "examples": { "show_users": { "value": { - "id": 4500, - "email": "email_15@email.com", - "name": "Name_15", + "id": 5287, + "email": "email_20@email.com", + "name": "Name_20", "bio": null, "social_networks": { "facebook": "foo", @@ -671,26 +1036,26 @@ ], "careers": [ { - "id": 360, + "id": 417, "code": "3", "name": "Career_3" }, { - "id": 361, + "id": 418, "code": "4", "name": "Career_4" } ], "subjects": [ { - "id": 2707, - "code": "12", - "name": "Subject_12" + "id": 3221, + "code": "19", + "name": "Subject_19" }, { - "id": 2708, - "code": "13", - "name": "Subject_13" + "id": 3222, + "code": "20", + "name": "Subject_20" } ] } @@ -733,8 +1098,8 @@ "examples": { "update_users": { "value": { - "id": 4501, - "email": "email_16@email.com", + "id": 5288, + "email": "email_21@email.com", "name": "New Name", "bio": "New Bio", "social_networks": {