mirror of
http://ghproxy.cn/https://github.com/multunus/onemdm-server
synced 2025-12-06 02:04:57 +00:00
API for app usage tracking
This commit is contained in:
25
app/controllers/app_usages_controller.rb
Normal file
25
app/controllers/app_usages_controller.rb
Normal file
@@ -0,0 +1,25 @@
|
||||
class AppUsagesController < ApplicationController
|
||||
before_filter :authenticate_device
|
||||
respond_to :json
|
||||
|
||||
def create
|
||||
app_usage = AppUsage.new(app_usage_params)
|
||||
if app_usage.save
|
||||
render json: {},
|
||||
status: :created
|
||||
else
|
||||
logger.warn "Error while saving App Usage #{app_usage.errors.full_messages}"
|
||||
render json: {},
|
||||
status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
private
|
||||
|
||||
def app_usage_params
|
||||
params.require(:app_usage).
|
||||
permit(:package_name,
|
||||
:usage_duration_in_seconds,
|
||||
:used_on)
|
||||
|
||||
end
|
||||
end
|
||||
3
app/models/app_usage.rb
Normal file
3
app/models/app_usage.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
class AppUsage < ActiveRecord::Base
|
||||
validates :package_name, :usage_duration_in_seconds, :used_on, presence: true
|
||||
end
|
||||
@@ -1,4 +1,6 @@
|
||||
Rails.application.routes.draw do
|
||||
post 'app_usages/create', to: 'app_usages#create', :defaults => { :format => :json }
|
||||
|
||||
devise_for :admin_users, ActiveAdmin::Devise.config
|
||||
|
||||
post 'heartbeats', to: 'heartbeats#create', :defaults => { :format => :json }
|
||||
|
||||
11
db/migrate/20160204094618_create_app_usages.rb
Normal file
11
db/migrate/20160204094618_create_app_usages.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
class CreateAppUsages < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :app_usages do |t|
|
||||
t.string :package_name, null: false, index: false
|
||||
t.integer :usage_duration_in_seconds, null: false, index: false
|
||||
t.date :used_on, null: false, index: false
|
||||
|
||||
t.timestamps null: false
|
||||
end
|
||||
end
|
||||
end
|
||||
10
db/schema.rb
10
db/schema.rb
@@ -11,7 +11,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20151118101616) do
|
||||
ActiveRecord::Schema.define(version: 20160204094618) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
@@ -49,6 +49,14 @@ ActiveRecord::Schema.define(version: 20151118101616) do
|
||||
add_index "admin_users", ["email"], name: "index_admin_users_on_email", unique: true, using: :btree
|
||||
add_index "admin_users", ["reset_password_token"], name: "index_admin_users_on_reset_password_token", unique: true, using: :btree
|
||||
|
||||
create_table "app_usages", force: :cascade do |t|
|
||||
t.string "package_name", null: false
|
||||
t.integer "usage_duration_in_seconds", null: false
|
||||
t.date "used_on", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
create_table "apps", force: :cascade do |t|
|
||||
t.string "name"
|
||||
t.string "package_name"
|
||||
|
||||
28
spec/controllers/app_usages_controller_spec.rb
Normal file
28
spec/controllers/app_usages_controller_spec.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe AppUsagesController, type: :controller do
|
||||
|
||||
describe "POST #create" do
|
||||
describe "Authorized" do
|
||||
let(:device){create(:device)}
|
||||
before(:each) do
|
||||
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(device.access_token)
|
||||
end
|
||||
it "Valid params" do
|
||||
post :create, app_usage: attributes_for(:app_usage), format: :json
|
||||
expect(response).to have_http_status(:success)
|
||||
end
|
||||
|
||||
it "InValid Params" do
|
||||
post :create, app_usage: attributes_for(:invalid_app_usage), format: :json
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
end
|
||||
end
|
||||
describe "POST #create" do
|
||||
it "Unauthorized" do
|
||||
post :create, app_usage: attributes_for(:app_usage), format: :json
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
13
spec/factories/app_usages.rb
Normal file
13
spec/factories/app_usages.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
FactoryGirl.define do
|
||||
factory :app_usage do
|
||||
package_name "com.facebook.katana"
|
||||
usage_duration_in_seconds 7000
|
||||
used_on Date.today - 1.day
|
||||
end
|
||||
factory :invalid_app_usage, class: AppUsage do
|
||||
package_name ""
|
||||
usage_duration_in_seconds 7000
|
||||
used_on Date.today - 1.day
|
||||
end
|
||||
|
||||
end
|
||||
9
spec/models/app_usage_spec.rb
Normal file
9
spec/models/app_usage_spec.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe AppUsage, type: :model do
|
||||
let!(:app_usage) { create(:app_usage) }
|
||||
|
||||
it { should validate_presence_of :package_name }
|
||||
it { should validate_presence_of :usage_duration_in_seconds }
|
||||
it { should validate_presence_of :used_on }
|
||||
end
|
||||
Reference in New Issue
Block a user